0
0
MATLABdata~10 mins

Why reading and writing data is fundamental in MATLAB - Test Your Understanding

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

Complete 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'
Ar
Bx
Ca
Dw
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.
2fill in blank
medium

Complete 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'
Ar+
Br
Cw
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' which only allows reading.
Using 'a' which appends instead of overwriting.
3fill in blank
hard

Fix 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'
Afclose
Bfopen
Copen
Dfcloseall
Attempts:
3 left
💡 Hint
Common Mistakes
Using open or fopen again instead of closing.
Using fcloseall which closes all files, not just this one.
4fill in blank
hard

Fill 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'
Ar
Bw
C%f
D%s
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens for writing, not reading.
Using '%s' which reads strings, not numbers.
5fill in blank
hard

Fill 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'
Aw
B'%d %d %d\n'
CfileID
D'%f %f %f\n'
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.