0
0
MATLABdata~10 mins

CSV file handling in MATLAB - Interactive Code Practice

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

Complete the code to read a CSV file named 'data.csv' into a table.

MATLAB
data = readtable([1]);
Drag options to blanks, or click blank then click option'
A'data.txt'
B'data.xlsx'
C'file.csv'
D'data.csv'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong file extension like '.txt' or '.xlsx'.
Not putting the filename inside quotes.
2fill in blank
medium

Complete the code to write the table 'data' to a CSV file named 'output.csv'.

MATLAB
writetable(data, [1]);
Drag options to blanks, or click blank then click option'
A'data.csv'
B'output.csv'
C'input.csv'
D'results.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a filename with the wrong extension.
Confusing input and output filenames.
3fill in blank
hard

Fix the error in the code to read only the first 5 rows from 'data.csv'.

MATLAB
opts = detectImportOptions([1]);
opts.DataLines = [2 6];
data = readtable([1], opts);
Drag options to blanks, or click blank then click option'
A'file.csv'
B'sample.csv'
C'data.csv'
D'data.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different filenames in detectImportOptions and readtable.
Using the wrong file extension.
4fill in blank
hard

Fill both blanks to create a CSV file 'summary.csv' with only columns 'Name' and 'Score' from the table 'data'.

MATLAB
summary = data(:, [1]);
writetable(summary, [2]);
Drag options to blanks, or click blank then click option'
A{'Name', 'Score'}
B'summary.csv'
C'data.csv'
D{'Age', 'Score'}
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting wrong columns like 'Age' instead of 'Name'.
Using the wrong output filename.
5fill in blank
hard

Fill all three blanks to read 'data.csv', filter rows where 'Score' > 80, and write to 'high_scores.csv'.

MATLAB
data = readtable([1]);
high_scores = data(data.[2] [3] 80, :);
writetable(high_scores, 'high_scores.csv');
Drag options to blanks, or click blank then click option'
A'data.csv'
BScore
C>
D'scores.csv'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong filename to read.
Using the wrong column name or operator for filtering.