Bird
0
0

You have a text file records.txt with mixed data: a name (string), age (integer), and height (float) on each line, separated by commas, like:

hard📝 Application Q15 of 15
MATLAB - File I/O
You have a text file records.txt with mixed data: a name (string), age (integer), and height (float) on each line, separated by commas, like:
John,25,5.9
Anna,30,5.5
Mike,22,6.1
Which MATLAB approach correctly reads this file into a table with proper data types?
AUse <code>readtable('records.txt', 'Delimiter', ',', 'Format', '%s%d%f')</code>
BUse <code>readtable('records.txt')</code> without specifying delimiter
CUse <code>textscan(fopen('records.txt', 'r'), '%s %d %f', 'Delimiter', ',')</code> and fclose after
DUse <code>textscan(fopen('records.txt'), '%s %d %f')</code> without closing file
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct reading method for mixed CSV data into table

    readtable automatically detects common delimiters like commas and loads data into a table with inferred types (string for names, double for numbers).
  2. Step 2: Compare options

    Use readtable('records.txt') without specifying delimiter: readtable('records.txt') auto-detects comma delimiter. Use readtable('records.txt', 'Delimiter', ',', 'Format', '%s%d%f') uses invalid 'Format' parameter (readtable lacks it). Use textscan(fopen('records.txt', 'r'), '%s %d %f', 'Delimiter', ',') and fclose after: textscan syntax invalid ('Delimiter' not for fid), returns cell array not table. Use textscan(fopen('records.txt'), '%s %d %f') without closing file: textscan fails to parse commas (default whitespace), no fclose.
  3. Final Answer:

    Use readtable('records.txt') without specifying delimiter -> Option B
  4. Quick Check:

    readtable = auto delimiter + table types [OK]
Quick Trick: readtable auto-detects CSV delimiters into tables [OK]
Common Mistakes:
  • Using textscan expecting table output
  • Adding invalid 'Format' to readtable
  • Omitting delimiter handling in textscan

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes