CSV file handling in MATLAB - Time & Space Complexity
When working with CSV files in MATLAB, it's important to know how the time to read or write data changes as the file size grows.
We want to understand how the program's running time grows when handling bigger CSV files.
Analyze the time complexity of the following code snippet.
filename = 'data.csv';
data = readmatrix(filename);
for i = 1:size(data, 1)
disp(data(i, :));
end
This code reads a CSV file into a matrix and then prints each row one by one.
- Primary operation: Looping through each row of the data matrix to display it.
- How many times: Once for every row in the CSV file (n times, where n is number of rows).
As the number of rows in the CSV file increases, the number of times the loop runs also increases directly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times loop runs |
| 100 | About 100 times loop runs |
| 1000 | About 1000 times loop runs |
Pattern observation: The work grows in a straight line with the number of rows.
Time Complexity: O(n)
This means the time to process the CSV file grows directly in proportion to the number of rows.
[X] Wrong: "Reading a CSV file always takes the same time no matter how big it is."
[OK] Correct: The bigger the file, the more rows there are to read and process, so it takes longer.
Understanding how file size affects processing time helps you write efficient code and explain your reasoning clearly in real projects or interviews.
"What if we used readtable instead of readmatrix? How would the time complexity change?"
Practice
Solution
Step 1: Understand the data type
Numeric data is best handled as a matrix in MATLAB.Step 2: Choose the appropriate function
readmatrixis designed to read numeric CSV data directly into a matrix.Final Answer:
readmatrix -> Option CQuick Check:
Numeric CSV data = readmatrix [OK]
- Using readtable for purely numeric data
- Using load which is for MAT files
- Confusing csvread with readmatrix (csvread is older)
A to a CSV file named data.csv in MATLAB?Solution
Step 1: Identify the correct function for matrix writing
writematrixis the modern function to write numeric matrices to CSV files.Step 2: Check syntax correctness
The syntaxwritematrix(A, 'data.csv');correctly specifies the matrix and filename.Final Answer:
writematrix(A, 'data.csv'); -> Option DQuick Check:
Write matrix to CSV = writematrix [OK]
- Using non-existent function writecsv
- Using csvwrite which is outdated
- Using writetable for numeric matrix without conversion
A = [1, 2; 3, 4];
writematrix(A, 'test.csv');
B = readmatrix('test.csv');
disp(B);Solution
Step 1: Write matrix to CSV
The matrixAis saved as a 2x2 CSV file with rows [1,2] and [3,4].Step 2: Read matrix from CSV
readmatrixreads the CSV back into the same 2x2 numeric matrix.Final Answer:
[1 2; 3 4] -> Option AQuick Check:
Write and read matrix preserves shape [OK]
- Expecting a single row vector output
- Confusing disp output format with array shape
- Assuming file read error without saving first
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?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
csvwriteonly supports numeric data and will fail or produce incorrect output for tables.Final Answer:
Using csvwrite to write tables causes incorrect output -> Option AQuick Check:
csvwrite not for tables = error [OK]
- Using readmatrix for mixed data losing text
- Assuming file must be closed manually in MATLAB
- Using writetable on numeric matrix without conversion
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?Solution
Step 1: Read mixed data correctly
Usereadtableto read CSV with text and numeric columns preserving all data types.Step 2: Update the numeric
Access theScorecolumnScorecolumn by name and multiply by 1.1 to increase by 10%.Step 3: Save updated table back to CSV
Usewritetableto 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 BQuick Check:
Mixed data update = readtable + modify column + writetable [OK]
- Using readmatrix loses text column
- Trying to multiply table columns by index incorrectly
- Using csvread/csvwrite which don't support text
Ready to prove it?
Pass the test and this pattern moves unknown
