Reading and writing data lets your program get information from files and save results. This is how programs talk to the outside world.
Why reading and writing data is fundamental in MATLAB
fileID = fopen('filename.ext', 'mode'); data = fread(fileID); fclose(fileID); fileID = fopen('filename.ext', 'mode'); fwrite(fileID, data); fclose(fileID);
fopen opens a file. The 'mode' can be 'r' for reading or 'w' for writing.
fclose closes the file to save changes and free resources.
fileID = fopen('data.txt', 'r'); data = fread(fileID); fclose(fileID);
fileID = fopen('output.txt', 'w'); fwrite(fileID, 'Hello World'); fclose(fileID);
data = fileread('notes.txt');writematrix([1 2 3; 4 5 6], 'numbers.csv');
This program writes a short message to a file, then reads it back and shows it on the screen.
filename = 'example.txt'; % Write some text to the file fileID = fopen(filename, 'w'); fprintf(fileID, 'Hello, MATLAB!\nThis is a test file.'); fclose(fileID); % Read the text back from the file fileID = fopen(filename, 'r'); textData = fscanf(fileID, '%c'); fclose(fileID); % Display the read text fprintf('Content of the file:\n%s\n', textData);
Always close files after opening them to avoid errors and data loss.
Use the right mode ('r' for reading, 'w' for writing) to avoid overwriting or reading errors.
Reading and writing lets your program save work and use data from outside sources.
Reading and writing data connects your program to files and the outside world.
You use fopen, fread, fwrite, and fclose to handle files.
This lets you save results, load inputs, and share information easily.
Practice
Solution
Step 1: Understand the role of file operations
Reading and writing data lets MATLAB programs interact with files to save or load data.Step 2: Identify the correct purpose
Only It allows programs to save and load information from files. correctly states this purpose; others describe unrelated features.Final Answer:
It allows programs to save and load information from files. -> Option BQuick Check:
Reading and writing data = save/load files [OK]
- Confusing file operations with interface changes
- Thinking reading/writing speeds up loops
- Assuming it fixes code errors automatically
Solution
Step 1: Recall fopen syntax for reading
In MATLAB, fopen with mode 'r' opens a file for reading.Step 2: Check each option
fileID = fopen('data.txt', 'r'); uses correct syntax; B uses a non-existent function; C uses fread which reads data but does not open; D opens for writing, not reading.Final Answer:
fileID = fopen('data.txt', 'r'); -> Option DQuick Check:
fopen with 'r' = open for reading [OK]
- Using fopen with wrong mode like 'w' for reading
- Confusing fopen with fread
- Using non-existent open function
fileID = fopen('testfile.txt', 'w');
fwrite(fileID, 'Hello');
fclose(fileID);
fileID = fopen('testfile.txt', 'r');
data = fread(fileID, '*char')';
fclose(fileID);
disp(data);Solution
Step 1: Write 'Hello' to file
The code opens 'testfile.txt' for writing and writes the string 'Hello'.Step 2: Read the file content and display
It reopens the file for reading, reads the characters, and displays them.Final Answer:
Hello -> Option CQuick Check:
Write 'Hello' then read and display = Hello [OK]
- Forgetting to close file before reading
- Reading without specifying '*char' for characters
- Assuming file is empty after writing
fileID = fopen('data.txt', 'r');
data = fread(fileID);
fclose(fileID);
disp(data);Solution
Step 1: Check fread usage
fread without specifying data type returns numeric bytes, not readable characters.Step 2: Identify correct usage
To read text, fread should include '*char' to read characters properly.Final Answer:
fread missing data type specifier -> Option AQuick Check:
fread needs '*char' for text reading [OK]
- Not specifying '*char' in fread for text files
- Closing file before reading
- Assuming disp can't show numeric arrays
Solution
Step 1: Open file for writing and write matrix with type
Open with fileID = fopen('matrix.dat', 'w'); then fwrite(fileID, matrix, 'double'); fclose(fileID). Specify data type 'double'.Step 2: Open file for reading and read matrix with size and type
Open with fileID = fopen('matrix.dat', 'r'); then matrix2 = fread(fileID, [size(matrix,1), size(matrix,2)], 'double'); fclose(fileID).Final Answer:
fileID = fopen('matrix.dat', 'w'); fwrite(fileID, matrix, 'double'); fclose(fileID); fileID = fopen('matrix.dat', 'r'); matrix2 = fread(fileID, [size(matrix,1), size(matrix,2)], 'double'); fclose(fileID); -> Option AQuick Check:
Open-write-close, then open-read-close with types = correct [OK]
- Not assigning fopen result to fileID
- Using wrong file modes for reading/writing
- Omitting data type in fwrite or fread
- Calling fwrite without fileID
Ready to prove it?
Pass the test and this pattern moves unknown
