0
0
MATLABdata~30 mins

Writing text files (writetable, fprintf) in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a 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.