Bird
0
0

What will be the output of this MATLAB code?

medium📝 Predict Output Q4 of 15
MATLAB - File I/O
What will be the output of this MATLAB code?
fileID = fopen('numbers.txt', 'w');
fprintf(fileID, '%d\n', [1 2 3]);
fclose(fileID);
fileID = fopen('numbers.txt', 'r');
data = fscanf(fileID, '%d');
fclose(fileID);
data
A[1 2 3]
B[1; 2; 3]
C[123]
DError: file not found
Step-by-Step Solution
Solution:
  1. Step 1: Write numbers to file with fprintf

    fprintf writes each number on a new line because of '\n'.
  2. Step 2: Read numbers with fscanf

    fscanf reads numbers into a column vector by default.
  3. Final Answer:

    [1; 2; 3] -> Option B
  4. Quick Check:

    fscanf reads numbers as column vector [OK]
Quick Trick: fscanf reads numeric data as column vector by default [OK]
Common Mistakes:
  • Expecting row vector output
  • Thinking fscanf reads all numbers as one integer
  • Assuming file does not exist after writing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes