Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the filename.
Omitting the file extension.
✗ Incorrect
The filename must be a string enclosed in single quotes, like 'output.txt'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' which is read-only mode.
Using 'a' which appends instead of overwriting.
✗ Incorrect
Use 'w' to open a file for writing, which creates or overwrites the file.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of %d and %s.
Using %f for integers.
✗ Incorrect
Use '%d' for integer and '%s' for string in the format specifier.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .txt extension instead of .csv.
Setting 'WriteRowNames' to true by mistake.
✗ Incorrect
Use 'data.csv' as filename and false to avoid writing row names.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d which is for integers.
Using %s which is for strings.
✗ Incorrect
Use '%.1f' to format the number with one decimal place.