0
0
MATLABdata~20 mins

Reading text files (readtable, textscan) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Reading Text Files
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AA table with variables: ID (double), Age (double), Name (string) and 3 rows showing the data
BA table with variables: ID (double), Age (double), Name (cell) and 3 rows showing the data
CError: File not found
DA numeric matrix with values [1 25; 2 30; 3 22]
Attempts:
2 left
💡 Hint
readtable automatically detects variable types and reads text columns as strings or categorical by default.
Predict Output
intermediate
2: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);
AA 1x3 cell array: {1x2 cell of strings, [25 30], [180.5 165.2]}
BA 1x3 cell array: {1x2 cell of strings, [25;30], [180.5;165.2]}
CA numeric matrix with values [25 180.5; 30 165.2]
DError: Format specifier mismatch
Attempts:
2 left
💡 Hint
textscan returns a cell array with each cell containing a column of data matching the format specifiers.
🔧 Debug
advanced
2: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);
AError: Not enough input arguments for format specifiers
BError: File not found
CNo error, returns a cell array with 3 columns
DError: Invalid format specifier
Attempts:
2 left
💡 Hint
Check if the number of format specifiers matches the number of data columns in the file.
Predict Output
advanced
2: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);
ATable with zeros in place of missing numeric data
BTable with empty strings in Score and Age columns for missing data
CError: Missing data not handled
DTable with missing numeric values as NaN in Score and Age columns for Bob and Carol
Attempts:
2 left
💡 Hint
readtable with import options can treat empty fields as missing and convert them to NaN for numeric columns.
🧠 Conceptual
expert
2: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?
Atextscan cannot handle mixed data types, so readtable is better for any file with text and numbers
Breadtable is always faster and better for large files because it automatically detects formats
Ctextscan is preferred when you need fine control over reading format and want to process data line-by-line efficiently
Dreadtable requires manual format specification, while textscan automatically detects data types
Attempts:
2 left
💡 Hint
Consider flexibility and performance when reading large files with complex formats.