We write data to text files to save results or share information with others. It helps keep data outside the program for later use.
Writing text files (writetable, fprintf) in MATLAB
writetable(T, filename) fprintf(fileID, formatSpec, A)
writetable saves tables directly to text files like CSV.
fprintf writes formatted text to a file opened with fopen.
T to a CSV file named 'data.csv'.writetable(T, 'data.csv')fileID = fopen('log.txt', 'w'); fprintf(fileID, 'Value: %.2f\n', 3.14); fclose(fileID);
This program creates a simple table, saves it as a CSV file using writetable, then writes the same data to a text file with tabs using fprintf. Finally, it reads and prints both files to show the saved content.
T = table([1; 2; 3], {'A'; 'B'; 'C'}, 'VariableNames', {'ID', 'Label'}); writetable(T, 'output.csv'); fileID = fopen('output.txt', 'w'); fprintf(fileID, 'ID\tLabel\n'); for i = 1:height(T) fprintf(fileID, '%d\t%s\n', T.ID(i), T.Label{i}); end fclose(fileID); % Read back the files to show content csvContent = fileread('output.csv'); txtContent = fileread('output.txt'); fprintf('CSV file content:\n%s\n', csvContent); fprintf('TXT file content:\n%s\n', txtContent);
Always close files with fclose after using fopen to avoid errors.
Use \n for new lines and \t for tabs in fprintf format strings.
writetable automatically handles commas and quotes for CSV files.
writetable is easy for saving tables directly to CSV files.
fprintf gives control to write formatted text line by line.
Always open files before writing and close them after to keep data safe.
Practice
Solution
Step 1: Understand the purpose of
writetablewritetableis designed to save MATLAB tables directly to files like CSV with minimal code.Step 2: Compare with other functions
fprintfis for custom text formatting,fopenopens files but doesn't save tables, andloadis for loading data, not saving.Final Answer:
writetable -> Option AQuick Check:
Save table fast = writetable [OK]
- Confusing fprintf with writetable for saving tables
- Using fopen alone to save data
- Thinking load saves files
Solution
Step 1: Understand fopen modes
'r' is read-only, 'w' is write (creates or overwrites), 'a' appends, 'x' creates new file but errors if exists.Step 2: Choose mode for writing
To write fresh data, use 'w' mode to open file for writing.Final Answer:
fid = fopen('data.txt', 'w'); -> Option AQuick Check:
Open file for writing = 'w' mode [OK]
- Using 'r' mode which is read-only
- Using 'a' when overwrite is needed
- Confusing 'x' mode with 'w'
fid = fopen('output.txt', 'w');
fprintf(fid, 'Value: %d\n', 42);
fclose(fid);Solution
Step 1: Analyze fprintf formatting
fprintfwrites formatted text. '%d' is replaced by integer 42, '\n' adds a newline.Step 2: Check file closing
fclose(fid)properly closes the file, so no error occurs.Final Answer:
Value: 42 -> Option BQuick Check:
Formatted integer output = Value: 42 [OK]
- Forgetting to close the file
- Expecting %d to appear literally
- Confusing output with just the number
fid = fopen('numbers.txt', 'w');
fprintf(fid, '%d\n', 1:5);
% Missing fclose(fid)Solution
Step 1: Check file opening
fopenuses 'w' mode correctly, so file opens for writing.Step 2: Check file closing
The code does not callfclose(fid), which is necessary to save and release the file.Final Answer:
Missing fclose to close the file -> Option DQuick Check:
Always fclose after writing [OK]
- Forgetting fclose causes incomplete writes
- Thinking fprintf needs different format for vectors
- Assuming fopen mode is missing
T with columns 'Name' and 'Score' to a CSV file, but only include rows where Score > 50. Which MATLAB code correctly does this?Solution
Step 1: Filter rows where Score > 50
Use logical indexing:T.Score > 50selects rows with Score greater than 50.Step 2: Use writetable to save filtered table
Pass filtered rowsT(T.Score > 50, :)towritetableto save only those rows.Final Answer:
writetable(T(T.Score > 50, :), 'filtered.csv'); -> Option CQuick Check:
Filter rows then writetable = writetable(T(T.Score > 50, :), 'filtered.csv'); [OK]
- Using fprintf without opening file
- Not filtering rows before saving
- Saving entire table without filtering
Ready to prove it?
Pass the test and this pattern moves unknown
