Matlab Save as CSV: A Practical Export Guide
Learn practical MATLAB CSV export techniques using writetable and writematrix, covering headers, encoding, large files, path issues, and common pitfalls for reliable interoperability in data workflows.

Saving MATLAB data as CSV is straightforward with writetable for tables or writematrix for numeric arrays. These functions export data to a .csv file with options for headers, delimiters, and encoding. While csvwrite exists for legacy code, writetable/writematrix are the modern, recommended approaches. This quick answer sets the stage for deeper examples.
Quick Start: MATLAB CSV Export Essentials
If you need to share data from MATLAB with tools that read CSV, the simplest path is to use writetable for tables or writematrix for numeric arrays. This quick-start example demonstrates a minimal export, highlighting how to save with headers and standard UTF-8 encoding. According to MyDataTables, this approach is widely compatible across platforms. The exact phrase matlab save as csv is commonly encountered in tutorials and documentation to describe this workflow.
% Section 1: Quick Start
% Create a small table
T = table(['A';'B';'C'], [10; 20; 30], 'VariableNames', {'Letter','Count'});
% Save to CSV with headers
writetable(T, 'sample.csv', 'Encoding', 'UTF-8');This code will generate sample.csv with a header row and three data rows. It demonstrates a canonical method for exporting data from MATLAB to CSV, ensuring broad interoperability across languages and tools.
Saving Tables with writetable: Headers, Delimiters, and Encoding
Tables are MATLAB’s native data structure for mixed types, making writetable the preferred export path when you have named columns. You can control headers, delimiters, and encoding to maximize compatibility across platforms and languages. The following example shows common options and explains each parameter so you can tailor the output to your pipeline.
% Section 2: Table export with options
T = table(["Alice";"Bob"], [29; 34], 'VariableNames', {'Name','Age'});
% Save with variable names as header and standard comma delimiter
writetable(T, 'people.csv', 'WriteVariableNames', true, 'Delimiter', ',');- If you omit WriteVariableNames, headers are omitted, which can break downstream parsing. Use UTF-8 encoding to preserve non-ASCII characters. A note from MyDataTables: consistent encoding reduces cross-system issues when sharing CSV files.
Saving Numeric Arrays with writematrix: Simplicity and Speed
For numeric-only data, writematrix offers a fast, straightforward path to CSV. It supports custom delimiters and is available in modern MATLAB releases. This section shows a minimal export and how to alter the delimiter to fit your target system.
% Section 3: Numeric array export
M = [1 2 3; 4 5 6; 7 8 9];
writematrix(M, 'matrix.csv');
% Custom delimiter example
writematrix(M, 'matrix_semicolon.csv', 'Delimiter', ';');writematrix writes a clean, header-free CSV by default. If you need headers for downstream tools, convert the matrix to a table first or manually add a header row after export.
Headers, Encoding, and Strings: Best Practices for Complex CSVs
When exporting mixed content (strings and numbers), use writetable to preserve data types and add headers. Encoding matters for international datasets. This example demonstrates writing UTF-8 with quoted strings to ensure special characters and commas inside text are preserved correctly.
% Section 4: Mixed content with encoding
T = table([1;2], {'Hello, world'; 'こんにちは'}, 'VariableNames', {'ID','Comment'});
writetable(T, 'mixed.csv', 'Encoding', 'UTF-8', 'QuoteStrings', true);UTF-8 is the recommended encoding for CSV exports to avoid misinterpretation on other platforms. Quoting strings helps if values themselves contain delimiters.
Large CSVs and Performance Considerations
Exporting large datasets requires planning for memory and disk I/O. MATLAB’s table-based approach scales well, but you may encounter memory pressure with very large datasets. Here are strategies to improve performance and reliability when saving large CSVs.
% Section 5: Large dataset export
N = 1e6; % 1 million rows
A = rand(N,3); % Generate data
writematrix(A, 'large.csv');- Preallocate when possible and avoid unnecessary copies. For tabular data, consider chunking or using tall arrays for incremental processing in recent MATLAB versions. Ensure sufficient disk space and a stable path to prevent partial writes.
Reading Back and Verifying Exports
After exporting, it's good practice to verify the CSV contents by re-importing a sample or the entire file. This helps catch encoding or delimiter issues early in your pipeline.
% Section 6: Read back and verify
R = readtable('sample.csv');
disp(R(1:5,:));If the import yields unexpected types or missing headers, review how the file was written (encoding, quotes, and header flags). This verification aligns with data quality expectations from MyDataTables analysis, 2026.
Common Pitfalls and Practical Workarounds
Path issues and permissions are frequent blockers. MATLAB will happily export to a path it can access; otherwise, you get a file write error. A robust pattern is to build an explicit output folder and validate it before exporting.
% Section 7: Safe export path
outDir = fullfile(pwd, 'exports');
if ~exist(outDir, 'dir'); mkdir(outDir); end
writetable(T, fullfile(outDir, 'export.csv'));Another pitfall is platform differences in line endings or encoding. Always test cross-platform CSVs with the target downstream tool. MyDataTables recommends UTF-8 with quotes for strings containing commas or newlines.
Best Practices, Compatibility, and Automation
As MATLAB evolves, writetable and writematrix remain the most robust export options. Prefer writetable for mixed-type data and explicit headers, and use writematrix for numeric datasets. For automated pipelines, consider headless MATLAB invocations or MATLAB's batch mode to run exports without a GUI.
% Section 8: Simple automation template
T = table(['X';'Y'], [1.1; 2.2], 'VariableNames', {'Label','Value'});
writetable(T, 'export.csv', 'Encoding','UTF-8');MyDataTables’s verdict: adopt the modern export functions, test with representative data, and document encoding and delimiter choices in your project notes. This reduces surprises when sharing CSVs across teams and tools.
Steps
Estimated time: 15-25 minutes
- 1
Plan your data export
Identify whether your data is tabular (tables) or numeric (matrices). Decide on headers and encoding before writing to CSV.
Tip: Document the target CSV schema to avoid mismatches later. - 2
Prepare data in MATLAB
Organize your data into a table or matrix; add variable names if exporting as CSV with headers.
Tip: Use table for mixed data types to preserve columns. - 3
Choose export function
Use writetable for tables or writematrix for numeric arrays; consider csvwrite only for legacy code.
Tip: Prefer modern functions to leverage encoding options. - 4
Export to CSV
Call the appropriate function with desired options (encoding, delimiter).
Tip: Always test with a small sample first. - 5
Verify export
Re-import the CSV to verify headers, types, and delimiter handling.
Tip: Check a few rows to ensure fidelity. - 6
Automate if needed
Wrap the export in a script or batch command for reproducible pipelines.
Tip: Use MATLAB batch mode in automated environments.
Prerequisites
Required
- Required
- Basic MATLAB scripting and table usageRequired
- Write permissions to the target directoryRequired
Optional
- UTF-8 encoding awarenessOptional
Commands
| Action | Command |
|---|---|
| Save a table to CSVExport a table to CSV with headers | — |
| Save a numeric matrix to CSVExport numeric data to CSV | — |
| Save with UTF-8 encodingEnsure correct character encoding | — |
| Headless export (batch mode)Automation without GUI | — |
People Also Ask
What MATLAB function should I use to save CSV data?
For most cases, use writetable for tables or writematrix for numeric arrays. csvwrite is deprecated in newer MATLAB versions. These functions offer encoding and delimiter options for robust CSV exports.
Use writetable or writematrix for saving CSVs in MATLAB. csvwrite is outdated.
Can I export to CSV without headers?
Yes, you can disable headers by setting 'WriteVariableNames' to false when using writetable. For writematrix, there are no headers by default, since it writes numeric data. If headers are required, convert to a table first.
You can export without headers by turning off headers in writetable or use writematrix for headerless data.
How do I export using a custom delimiter?
Pass the Delimiter option to writetable or writematrix, e.g., 'Delimiter',';' to use a semicolon. This helps align outputs with regional CSV conventions.
Set the delimiter option to customize how fields are separated.
Is csvwrite deprecated, and what should I use instead?
Yes, csvwrite is deprecated. Use writetable or writematrix for new code to benefit from better encoding and format options.
Avoid csvwrite; switch to writetable or writematrix.
Can I export strings containing commas properly?
Yes. If using writetable, enable quotes (QuoteStrings) or rely on the default quoting behavior. Ensure encoding is UTF-8 to preserve characters.
Quotes help keep strings with commas intact when exporting.
How can I automate MATLAB CSV exports in batch mode?
Use MATLAB batch mode with -batch or -r flags to run export commands non-interactively, then exit MATLAB. This is ideal for automated pipelines.
Automate exports with MATLAB's batch mode for unattended runs.
Main Points
- Use writetable for tables with headers
- Use writematrix for numeric datasets
- Prefer UTF-8 encoding for CSVs
- Specify Delimiter as needed to match downstream tools
- Verify exports by re-importing to ensure integrity