0
0
MATLABdata~20 mins

Writing text files (writetable, fprintf) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Text File Writing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this fprintf code?
Consider the following MATLAB code that writes formatted text to a file. What will be the content of the file after running this code?
MATLAB
fid = fopen('output.txt', 'w');
fprintf(fid, 'Value: %d\n', 42);
fclose(fid);
file_content = fileread('output.txt');
disp(file_content);
A
Value: %d
B24 :eulaV
C
Value: 42
DError: File not found
Attempts:
2 left
💡 Hint
Remember that fprintf with '%d' replaces the placeholder with the integer value and '\n' adds a new line.
Predict Output
intermediate
2:00remaining
What does writetable write to the file?
Given this MATLAB code, what will be the content of 'data.csv' after execution?
MATLAB
T = table([1;2;3], {'A';'B';'C'}, 'VariableNames', {'ID', 'Letter'});
writetable(T, 'data.csv');
content = fileread('data.csv');
disp(content);
AError: VariableNames not supported
B
ID,Letter
1,A
2,B
3,C
C
ID Letter
1 A
2 B
3 C
D
1,A
2,B
3,C
Attempts:
2 left
💡 Hint
writetable writes variable names as headers by default and separates values with commas in CSV format.
🔧 Debug
advanced
2:00remaining
Why does this fprintf code produce an error?
Examine the code below. Why does it cause an error when run?
MATLAB
fid = fopen('test.txt', 'w');
fprintf(fid, 'Number: %d %f\n', 5);
fclose(fid);
Afprintf expects two values for '%d' and '%f' but only one is provided, causing an error.
Bfopen failed to open the file, so fprintf errors out.
CThe format string is invalid because '%f' cannot be used with fprintf.
Dfclose is called before fprintf, causing an error.
Attempts:
2 left
💡 Hint
Check how many values fprintf expects based on the format string placeholders.
📝 Syntax
advanced
2:00remaining
Which option correctly writes a table without row names?
You want to write a table to a CSV file without including row names. Which code does this correctly?
MATLAB
T = table([10;20], {'X';'Y'}, 'VariableNames', {'Num', 'Char'});
Awritetable(T, 'file.csv', 'RowNames', false);
Bwritetable(T, 'file.csv', 'WriteRowNames', true);
Cwritetable(T, 'file.csv', 'IncludeRowNames', false);
Dwritetable(T, 'file.csv', 'WriteRowNames', false);
Attempts:
2 left
💡 Hint
Check the exact parameter name for controlling row names in writetable.
🚀 Application
expert
2:00remaining
How many lines are written by this fprintf loop?
This MATLAB code writes lines to a file. How many lines will the file contain after running?
MATLAB
fid = fopen('log.txt', 'w');
for i = 1:5
    fprintf(fid, 'Line %d\n', i);
end
fclose(fid);
content = fileread('log.txt');
lines = count(content, '\n');
A5
B6
C4
D1
Attempts:
2 left
💡 Hint
Each fprintf call writes one line ending with '\n'. Count how many times the loop runs.