Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to read data from a file named 'data.txt'.
MATLAB
fileID = fopen('data.txt', '[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens the file for writing and erases content.
Using 'a' which opens the file for appending data.
✗ Incorrect
Use 'r' to open a file for reading in MATLAB.
2fill in blank
mediumComplete the code to write the variable 'data' to a file.
MATLAB
fileID = fopen('output.txt', '[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' which only allows reading.
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 code to properly close the file after reading.
MATLAB
fileID = fopen('data.txt', 'r'); data = fscanf(fileID, '%f'); [1](fileID);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using open or fopen again instead of closing.
Using fcloseall which closes all files, not just this one.
✗ Incorrect
Use fclose(fileID) to close the file and free system resources.
4fill in blank
hardFill both blanks to read numeric data from a file and store it in 'data'.
MATLAB
fileID = fopen('numbers.txt', '[1]'); data = fscanf(fileID, '[2]'); fclose(fileID);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens for writing, not reading.
Using '%s' which reads strings, not numbers.
✗ Incorrect
Open the file for reading with 'r' and use '%f' to read floating-point numbers.
5fill in blank
hardFill all three blanks to write a matrix to a file with formatted output.
MATLAB
fileID = fopen('matrix.txt', '[1]'); for i = 1:size(matrix, 1) fprintf(fileID, [2], matrix(i, :)); end fclose([3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%d' which formats integers instead of floats.
Forgetting to close the file or using wrong variable in fclose.
✗ Incorrect
Open the file for writing with 'w', use '%f %f %f\n' to format floats, and close with fclose(fileID).