Introduction
CSV files are simple text files that store data in rows and columns. Handling CSV files lets you read data into MATLAB or save your results to share with others.
data = readmatrix('filename.csv'); writematrix(data, 'filename.csv');
data = readmatrix('data.csv');writematrix(data, 'output.csv');T = readtable('data.csv');writetable(T, 'output.csv');data = [10, 20, 30; 40, 50, 60]; writematrix(data, 'mydata.csv'); readData = readmatrix('mydata.csv'); disp(readData);
readmatrix is designed to read numeric CSV data directly into a matrix.A to a CSV file named data.csv in MATLAB?writematrix is the modern function to write numeric matrices to CSV files.writematrix(A, 'data.csv'); correctly specifies the matrix and filename.A = [1, 2; 3, 4];
writematrix(A, 'test.csv');
B = readmatrix('test.csv');
disp(B);A is saved as a 2x2 CSV file with rows [1,2] and [3,4].readmatrix reads the CSV back into the same 2x2 numeric matrix.T = readtable('data.csv');
writetable(T, 'output.csv');data.csv contains mixed numeric and text data, what is a common error to watch for?csvwrite only supports numeric data and will fail or produce incorrect output for tables.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?readtable to read CSV with text and numeric columns preserving all data types.Score columnScore column by name and multiply by 1.1 to increase by 10%.writetable to save the modified table preserving text and numeric data.Pass the test and this pattern moves unknown