Challenge - 5 Problems
Master of Reading Text Files
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of readtable with mixed data types
What is the output of the following MATLAB code snippet when reading a text file with mixed numeric and text columns?
MATLAB
fid = fopen('data.txt','w'); fprintf(fid, 'ID Age Name\n1 25 Alice\n2 30 Bob\n3 22 Carol'); fclose(fid); T = readtable('data.txt'); disp(T);
Attempts:
2 left
💡 Hint
readtable automatically detects variable types and reads text columns as strings or categorical by default.
✗ Incorrect
readtable reads the file and creates a table with columns matching the data types. Numeric columns become double, text columns become string arrays.
❓ Predict Output
intermediate2:00remaining
Output of textscan with delimiter and format specifiers
What is the output of this MATLAB code reading a text file with textscan?
MATLAB
fid = fopen('data2.txt','w'); fprintf(fid, 'John,25,180.5\nJane,30,165.2\n'); fclose(fid); fid = fopen('data2.txt','r'); C = textscan(fid, '%s %d %f', 'Delimiter', ','); fclose(fid); disp(C);
Attempts:
2 left
💡 Hint
textscan returns a cell array with each cell containing a column of data matching the format specifiers.
✗ Incorrect
The format '%s %d %f' reads a string, an integer, and a float per line. The delimiter ',' splits the fields. textscan returns a cell array with each column as a separate cell.
🔧 Debug
advanced2:00remaining
Identify the error in textscan usage
What error does this MATLAB code produce when trying to read a file with textscan?
MATLAB
fid = fopen('data3.txt','w'); fprintf(fid, 'A 10\nB 20\n'); fclose(fid); fid = fopen('data3.txt','r'); C = textscan(fid, '%s %d %f'); fclose(fid);
Attempts:
2 left
💡 Hint
Check if the number of format specifiers matches the number of data columns in the file.
✗ Incorrect
The file has two columns per line, but the format string expects three columns (%s %d %f). This mismatch causes an error.
❓ Predict Output
advanced2:00remaining
Result of readtable with missing data and options
What is the output of this MATLAB code reading a file with missing values using readtable?
MATLAB
fid = fopen('data4.txt','w'); fprintf(fid, 'Name,Score,Age\nAlice,90,25\nBob,,30\nCarol,85,\n'); fclose(fid); opts = detectImportOptions('data4.txt'); opts = setvaropts(opts, 'Score', 'TreatAsMissing', ''); opts = setvaropts(opts, 'Age', 'TreatAsMissing', ''); T = readtable('data4.txt', opts); disp(T);
Attempts:
2 left
💡 Hint
readtable with import options can treat empty fields as missing and convert them to NaN for numeric columns.
✗ Incorrect
Setting 'TreatAsMissing' to empty string tells readtable to treat empty fields as missing values. Numeric missing values become NaN in the table.
🧠 Conceptual
expert2:00remaining
Choosing between readtable and textscan for large files
Which statement best explains when to prefer textscan over readtable for reading large text files in MATLAB?
Attempts:
2 left
💡 Hint
Consider flexibility and performance when reading large files with complex formats.
✗ Incorrect
textscan allows detailed control of format and can read data incrementally, which is useful for large files. readtable is convenient but may be slower and less flexible.