Bird
Raised Fist0
MATLABdata~10 mins

Writing text files (writetable, fprintf) in MATLAB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to write a table to a text file using writetable.

MATLAB
T = table([1; 2; 3], {'A'; 'B'; 'C'}, 'VariableNames', {'ID', 'Letter'});
writetable(T, '[1]');
Drag options to blanks, or click blank then click option'
A'output.txt'
Boutput.txt
C'output'
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the filename.
Omitting the file extension.
2fill in blank
medium

Complete the code to open a file for writing using fopen.

MATLAB
fid = fopen('data.txt', '[1]');
Drag options to blanks, or click blank then click option'
A'rw'
B'w'
C'a'
D'r'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' which is read-only mode.
Using 'a' which appends instead of overwriting.
3fill in blank
hard

Fix the error in the fprintf statement to write a number and a string to the file.

MATLAB
fid = fopen('log.txt', 'w');
num = 10;
str = 'test';
fprintf(fid, '[1]', num, str);
fclose(fid);
Drag options to blanks, or click blank then click option'
A'%d %d\n'
B'%s %d\n'
C'%f %s\n'
D'%d %s\n'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of %d and %s.
Using %f for integers.
4fill in blank
hard

Fill both blanks to write a table to a CSV file without row names.

MATLAB
T = table([4; 5], {'X'; 'Y'}, 'VariableNames', {'Num', 'Char'});
writetable(T, '[1]', 'WriteRowNames', [2]);
Drag options to blanks, or click blank then click option'
A'data.csv'
Btrue
Cfalse
D'data.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .txt extension instead of .csv.
Setting 'WriteRowNames' to true by mistake.
5fill in blank
hard

Fill all three blanks to write formatted data to a file using fprintf.

MATLAB
fid = fopen('results.txt', 'w');
values = [1.5, 2.5, 3.5];
for i = 1:length(values)
    fprintf(fid, '[1]', values(i));
end
fclose(fid);
Drag options to blanks, or click blank then click option'
AValue: %.1f\n
BValue: %d\n
CValue: %f\n
DValue: %s\n
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d which is for integers.
Using %s which is for strings.

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

  1. Step 1: Understand the purpose of writetable

    writetable is designed to save MATLAB tables directly to files like CSV with minimal code.
  2. 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.
  3. Final Answer:

    writetable -> Option A
  4. 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

  1. 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.
  2. Step 2: Choose mode for writing

    To write fresh data, use 'w' mode to open file for writing.
  3. Final Answer:

    fid = fopen('data.txt', 'w'); -> Option A
  4. 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?
fid = fopen('output.txt', 'w');
fprintf(fid, 'Value: %d\n', 42);
fclose(fid);
medium
A. 42
B. Value: 42
C. Value: %d
D. Error: File not closed

Solution

  1. Step 1: Analyze fprintf formatting

    fprintf writes formatted text. '%d' is replaced by integer 42, '\n' adds a newline.
  2. Step 2: Check file closing

    fclose(fid) properly closes the file, so no error occurs.
  3. Final Answer:

    Value: 42 -> Option B
  4. Quick Check:

    Formatted integer output = Value: 42 [OK]
Hint: fprintf replaces %d with number, remember to fclose [OK]
Common Mistakes:
  • Forgetting to close the file
  • Expecting %d to appear literally
  • Confusing output with just the number
4. Identify the error in this MATLAB code snippet that writes numbers to a file:
fid = fopen('numbers.txt', 'w');
fprintf(fid, '%d\n', 1:5);
% Missing fclose(fid)
medium
A. Missing file open mode
B. Using range 1:5 is invalid
C. Incorrect format specifier in fprintf
D. Missing fclose to close the file

Solution

  1. Step 1: Check file opening

    fopen uses 'w' mode correctly, so file opens for writing.
  2. Step 2: Check file closing

    The code does not call fclose(fid), which is necessary to save and release the file.
  3. Final Answer:

    Missing fclose to close the file -> Option D
  4. 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?
hard
A. fid = fopen('filtered.csv', 'w'); fprintf(fid, '%s, %d\n', T.Name, T.Score); fclose(fid);
B. fprintf('filtered.csv', '%s, %d\n', T.Name, T.Score);
C. writetable(T(T.Score > 50, :), 'filtered.csv');
D. writetable(T, 'filtered.csv');

Solution

  1. Step 1: Filter rows where Score > 50

    Use logical indexing: T.Score > 50 selects rows with Score greater than 50.
  2. Step 2: Use writetable to save filtered table

    Pass filtered rows T(T.Score > 50, :) to writetable to save only those rows.
  3. Final Answer:

    writetable(T(T.Score > 50, :), 'filtered.csv'); -> Option C
  4. Quick Check:

    Filter rows then writetable = writetable(T(T.Score > 50, :), 'filtered.csv'); [OK]
Hint: Filter table rows before writetable to save subset [OK]
Common Mistakes:
  • Using fprintf without opening file
  • Not filtering rows before saving
  • Saving entire table without filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard