What MATLAB function is commonly used to read data from a CSV file?
The readtable function is commonly used to read data from a CSV file into a table in MATLAB.
Click to reveal answer
beginner
How do you write a MATLAB table to a CSV file?
You use the writetable function to save a MATLAB table to a CSV file.
Click to reveal answer
intermediate
What is the difference between readtable and csvread in MATLAB?
readtable reads CSV files into tables and handles mixed data types and headers, while csvread reads numeric data only and does not handle headers.
Click to reveal answer
intermediate
How can you specify a delimiter other than a comma when reading a CSV file in MATLAB?
You can specify the delimiter using the 'Delimiter' name-value pair argument in readtable, for example: readtable('file.txt', 'Delimiter', ';').
Click to reveal answer
beginner
What MATLAB function would you use to read a CSV file if you want the data as a numeric matrix?
You can use csvread or readmatrix to read CSV files as numeric matrices. readmatrix is recommended for newer MATLAB versions.
Click to reveal answer
Which MATLAB function reads CSV files into a table including headers?
Areadtable
Bcsvread
Cload
Dfopen
✗ Incorrect
readtable reads CSV files into tables and includes headers automatically.
Which function is best to write a table to a CSV file in MATLAB?
Acsvwrite
Bfprintf
Cwritetable
Dreadtable
✗ Incorrect
writetable writes tables to CSV files, preserving headers and data types.
If your CSV file has semicolons as separators, how do you read it in MATLAB?
Aload('file.csv')
Breadtable('file.csv')
Ccsvread('file.csv')
Dreadtable('file.csv', 'Delimiter', ';')
✗ Incorrect
Use the 'Delimiter' option in readtable to specify semicolon separators.
Which MATLAB function reads only numeric data from a CSV file?
Acsvread
Breadtable
Cwritetable
Dtextscan
✗ Incorrect
csvread reads numeric data only and ignores headers.
What is the recommended function to read numeric data from CSV files in recent MATLAB versions?
Acsvread
Breadmatrix
Creadtable
Dload
✗ Incorrect
readmatrix is recommended for reading numeric data from CSV files in newer MATLAB versions.
Explain how to read a CSV file with headers and mixed data types in MATLAB.
Think about the function that returns a table and handles text and numbers.
You got /4 concepts.
Describe the steps to save a MATLAB table to a CSV file.
Focus on the function that writes tables to files.
You got /4 concepts.
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
Step 1: Understand the data type
Numeric data is best handled as a matrix in MATLAB.
Step 2: Choose the appropriate function
readmatrix is designed to read numeric CSV data directly into a matrix.
Final Answer:
readmatrix -> Option C
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
Step 1: Identify the correct function for matrix writing
writematrix is the modern function to write numeric matrices to CSV files.
Step 2: Check syntax correctness
The syntax writematrix(A, 'data.csv'); correctly specifies the matrix and filename.
Final Answer:
writematrix(A, 'data.csv'); -> Option D
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
Step 1: Write matrix to CSV
The matrix A is saved as a 2x2 CSV file with rows [1,2] and [3,4].
Step 2: Read matrix from CSV
readmatrix reads the CSV back into the same 2x2 numeric matrix.
Final Answer:
[1 2; 3 4] -> Option A
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
Step 1: Understand data type in CSV
Mixed numeric and text data require table format to preserve all data correctly.
Step 2: Identify correct write function
csvwrite only supports numeric data and will fail or produce incorrect output for tables.
Final Answer:
Using csvwrite to write tables causes incorrect output -> Option A
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
Step 1: Read mixed data correctly
Use readtable to read CSV with text and numeric columns preserving all data types.
Step 2: Update the numeric Score column
Access the Score column by name and multiply by 1.1 to increase by 10%.
Step 3: Save updated table back to CSV
Use writetable to save the modified table preserving text and numeric data.
Final Answer:
T = readtable('file.csv');
T.Score = T.Score * 1.1;
writetable(T, 'file.csv'); -> Option B