Writing Text Files with writetable and fprintf in MATLAB
📖 Scenario: You work in a small shop and want to save your sales data to a text file so you can share it with your manager.
🎯 Goal: Learn how to write data to text files in MATLAB using writetable and fprintf.
📋 What You'll Learn
Create a table with sales data
Set a filename variable for the output file
Write the table to a text file using writetable
Write formatted sales data lines using fprintf
Display confirmation message after writing files
💡 Why This Matters
🌍 Real World
Saving sales or inventory data to text files is common in shops and businesses to share reports or keep records.
💼 Career
Knowing how to write and format text files is useful for data reporting, exporting results, and communicating between software tools.
Progress0 / 4 steps
1
Create the sales data table
Create a table called salesData with these columns and values: Product with entries 'Apples', 'Bananas', 'Cherries' Quantity with values 10, 15, 7 Price with values 1.2, 0.5, 2.0
MATLAB
Hint
Use the table function with cell arrays for text and numeric arrays for numbers.
2
Set the filename for the output file
Create a variable called filename and set it to the string 'sales.txt'
MATLAB
Hint
Use single quotes for the string in MATLAB.
3
Write the table to a text file using writetable
Use writetable to write the salesData table to the file named by filename with the option 'Delimiter' set to ','
MATLAB
Hint
Use writetable with the 'Delimiter' option set to ',' to create a CSV style file.
4
Write formatted sales lines using fprintf and display confirmation
Open the file named by filename for writing using fopen, then use a for loop with index i from 1 to height of salesData to write each product line with fprintf in the format: Product: Quantity units at $Price each. Close the file with fclose. Finally, print 'Sales data saved to sales.txt' using fprintf.
MATLAB
Hint
Use fopen with 'w' to open for writing, fprintf inside a for loop to write each line, then fclose. Use fprintf without fileID to print confirmation.
Practice
(1/5)
1. Which MATLAB function is best suited to save a table as a CSV file quickly?
easy
A. writetable
B. fprintf
C. fopen
D. load
Solution
Step 1: Understand the purpose of writetable
writetable is designed to save MATLAB tables directly to files like CSV with minimal code.
Step 2: Compare with other functions
fprintf is for custom text formatting, fopen opens files but doesn't save tables, and load is for loading data, not saving.
Final Answer:
writetable -> Option A
Quick Check:
Save table fast = writetable [OK]
Hint: Use writetable to save tables directly as CSV files [OK]
Common Mistakes:
Confusing fprintf with writetable for saving tables
Using fopen alone to save data
Thinking load saves files
2. Which of the following is the correct syntax to open a file named 'data.txt' for writing in MATLAB?
easy
A. fid = fopen('data.txt', 'w');
B. fid = fopen('data.txt', 'r');
C. fid = fopen('data.txt', 'a');
D. fid = fopen('data.txt', 'x');
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 A
Quick Check:
Open file for writing = 'w' mode [OK]
Hint: 'w' mode in fopen opens file for writing (overwrite) [OK]
Common Mistakes:
Using 'r' mode which is read-only
Using 'a' when overwrite is needed
Confusing 'x' mode with 'w'
3. What will be the output file content after running this MATLAB code?
fopen uses 'w' mode correctly, so file opens for writing.
Step 2: Check file closing
The code does not call fclose(fid), which is necessary to save and release the file.
Final Answer:
Missing fclose to close the file -> Option D
Quick Check:
Always fclose after writing [OK]
Hint: Always fclose after writing to save file properly [OK]
Common Mistakes:
Forgetting fclose causes incomplete writes
Thinking fprintf needs different format for vectors
Assuming fopen mode is missing
5. You want to save a table T with columns 'Name' and 'Score' to a CSV file, but only include rows where Score > 50. Which MATLAB code correctly does this?