Bird
Raised Fist0
MATLABdata~10 mins

CSV file handling in MATLAB - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. Which MATLAB function is best suited to read numeric data from a CSV file into a matrix?
easy
A. csvread
B. readtable
C. readmatrix
D. load

Solution

  1. Step 1: Understand the data type

    Numeric data is best handled as a matrix in MATLAB.
  2. Step 2: Choose the appropriate function

    readmatrix is designed to read numeric CSV data directly into a matrix.
  3. Final Answer:

    readmatrix -> Option C
  4. Quick Check:

    Numeric CSV data = readmatrix [OK]
Hint: Use readmatrix for numeric CSV files quickly [OK]
Common Mistakes:
  • Using readtable for purely numeric data
  • Using load which is for MAT files
  • Confusing csvread with readmatrix (csvread is older)
2. Which of the following is the correct syntax to write a matrix A to a CSV file named data.csv in MATLAB?
easy
A. writetable('data.csv', A);
B. writecsv(A, 'data.csv');
C. csvwrite(A, 'data.csv');
D. writematrix(A, 'data.csv');

Solution

  1. Step 1: Identify the correct function for matrix writing

    writematrix is the modern function to write numeric matrices to CSV files.
  2. Step 2: Check syntax correctness

    The syntax writematrix(A, 'data.csv'); correctly specifies the matrix and filename.
  3. Final Answer:

    writematrix(A, 'data.csv'); -> Option D
  4. Quick Check:

    Write matrix to CSV = writematrix [OK]
Hint: Use writematrix(matrix, filename) to save numeric CSV [OK]
Common Mistakes:
  • Using non-existent function writecsv
  • Using csvwrite which is outdated
  • Using writetable for numeric matrix without conversion
3. What will be the output of the following MATLAB code?
A = [1, 2; 3, 4];
writematrix(A, 'test.csv');
B = readmatrix('test.csv');
disp(B);
medium
A. [1 2; 3 4]
B. [1 2 3 4]
C. [1; 2; 3; 4]
D. Error: File not found

Solution

  1. Step 1: Write matrix to CSV

    The matrix A is saved as a 2x2 CSV file with rows [1,2] and [3,4].
  2. Step 2: Read matrix from CSV

    readmatrix reads the CSV back into the same 2x2 numeric matrix.
  3. Final Answer:

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

    Write and read matrix preserves shape [OK]
Hint: writematrix and readmatrix keep matrix shape intact [OK]
Common Mistakes:
  • Expecting a single row vector output
  • Confusing disp output format with array shape
  • Assuming file read error without saving first
4. Consider this MATLAB code snippet:
T = readtable('data.csv');
writetable(T, 'output.csv');

If data.csv contains mixed numeric and text data, what is a common error to watch for?
medium
A. Using csvwrite to write tables causes incorrect output
B. Using writetable on a numeric matrix causes syntax error
C. Forgetting to close the file after reading causes runtime error
D. Using readmatrix instead of readtable causes syntax error

Solution

  1. Step 1: Understand data type in CSV

    Mixed numeric and text data require table format to preserve all data correctly.
  2. Step 2: Identify correct write function

    csvwrite only supports numeric data and will fail or produce incorrect output for tables.
  3. Final Answer:

    Using csvwrite to write tables causes incorrect output -> Option A
  4. Quick Check:

    csvwrite not for tables = error [OK]
Hint: Use writetable for mixed data, not csvwrite [OK]
Common Mistakes:
  • Using readmatrix for mixed data losing text
  • Assuming file must be closed manually in MATLAB
  • Using writetable on numeric matrix without conversion
5. You have a CSV file with columns: Name (text), Age (numeric), and Score (numeric). You want to read it, increase all Score values by 10%, and save the updated data back to a CSV file. Which MATLAB code snippet correctly does this?
hard
A. data = csvread('file.csv'); data(:,3) = data(:,3) * 1.1; csvwrite('file.csv', data);
B. T = readtable('file.csv'); T.Score = T.Score * 1.1; writetable(T, 'file.csv');
C. T = readtable('file.csv'); T(:,3) = T(:,3) * 1.1; writetable(T, 'file.csv');
D. A = readmatrix('file.csv'); A(:,3) = A(:,3) * 1.1; writematrix(A, 'file.csv');

Solution

  1. Step 1: Read mixed data correctly

    Use readtable to read CSV with text and numeric columns preserving all data types.
  2. Step 2: Update the numeric Score column

    Access the Score column by name and multiply by 1.1 to increase by 10%.
  3. Step 3: Save updated table back to CSV

    Use writetable to save the modified table preserving text and numeric data.
  4. Final Answer:

    T = readtable('file.csv'); T.Score = T.Score * 1.1; writetable(T, 'file.csv'); -> Option B
  5. Quick Check:

    Mixed data update = readtable + modify column + writetable [OK]
Hint: Use readtable and writetable for mixed data updates [OK]
Common Mistakes:
  • Using readmatrix loses text column
  • Trying to multiply table columns by index incorrectly
  • Using csvread/csvwrite which don't support text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard