MATLAB Write Table to CSV: A Practical Guide
Learn how to export MATLAB tables to CSV with writetable, covering headers, encoding, delimiters, and appending. This technical guide provides practical code and tips for robust CSV output.
MATLAB uses the writetable function to export a table to CSV. For a basic export, use writetable(T, 'data.csv'); To ensure UTF-8 encoding and safer delimiter handling, add options like 'Encoding','UTF-8' and 'Delimiter',','; To append data, use 'WriteMode','append' and set 'WriteVariableNames' to false to avoid header duplication.
Understanding MATLAB tables and CSV basics
MATLAB stores data in a variety of container types, with tables offering a convenient way to hold heterogeneous data in labeled columns. When you want to save a table to a CSV, MATLAB provides writetable, a function designed to convert the in-memory table into a plain-text representation that can be consumed by other tools, databases, or analytics pipelines. This section lays the groundwork: what a MATLAB table is, why CSV is a common interchange format, and the basic syntax for exporting. The essential idea is that a well-formed table can be written to a CSV with a single function call, provided you supply a filename and the table object.
% Create a simple table
T = table([101;102;103], {'Alice';'Bob';'Carol'}, [88.5;92.0;76.3], 'VariableNames', {'ID','Name','Score'});
% Basic export to CSV (header included)
writetable(T, 'students.csv');% Export without headers (no variable names in the first row)
writetable(T, 'students_no_header.csv', 'WriteVariableNames', false);These examples demonstrate the core pattern: construct a table, then call writetable with a target file name. The header row is derived from VariableNames unless you override with WriteVariableNames. For the reader, this is the fastest path to take your in-memory data to a portable CSV.
Steps
Estimated time: 60-90 minutes
- 1
Prepare your data
Load or construct the data you want to export as a MATLAB table. Ensure types are consistent within each column and decide on whether to include variable names as headers.
Tip: If your data come from multiple sources, normalize types before building the table. - 2
Create the table
Convert your arrays into a MATLAB table with meaningful column names. This is your canonical data structure for export.
Tip: Use descriptive VariableNames to improve downstream readability. - 3
Write with default headers
Export the table to a CSV using writetable with the simplest call. This creates a header row from VariableNames by default.
Tip: Check the resulting file to confirm the header row matches your expected names. - 4
Customize encoding and delimiter
If you need UTF-8 encoding or a non-comma delimiter, pass additional Name-Value pairs to writetable.
Tip: UTF-8 ensures compatibility with international characters. - 5
Append data safely
To add data to an existing CSV, use WriteMode','append' and disable headers to avoid duplicates.
Tip: When appending, set WriteVariableNames to false to prevent repeated headers. - 6
Verify your CSV
Read the file back into MATLAB to verify structure and data types.
Tip: Consider readtable with explicit Delimiter to ensure correct parsing.
Prerequisites
Required
- Required
- Familiarity with MATLAB tables and basic syntaxRequired
- Sample data available in the workspaceRequired
Optional
- A text editor or IDE for scripting (optional)Optional
Commands
| Action | Command |
|---|---|
| Run a MATLAB script non-interactivelyExecutes and quits. Useful for batch processing. | — |
| Run a MATLAB script and stay in sessionKeeps you in the session for interactive work. | — |
People Also Ask
What does writetable do in MATLAB?
Writetable converts a MATLAB table into a delimited text file, typically CSV, preserving the table’s column names by default. It’s the standard method to export tabular research results for downstream tools.
Writetable saves your MATLAB table as a CSV or text file, keeping headers by default.
How can I export a table without headers?
Pass the option 'WriteVariableNames', false to writetable to omit the header row. This is useful when the target consumer expects header-less data.
If you don’t want headers in the output, tell writetable not to write variable names.
Can I control the encoding of the exported CSV?
Yes. Use the EncodingName option, such as 'Encoding','UTF-8', to ensure proper Unicode handling during export.
You can set the encoding to UTF-8 to keep characters intact across systems.
What about appending data to an existing CSV?
Use 'WriteMode','append' and disable headers to add rows without duplicating the header row.
To add new rows, append and skip the header row.
How do I verify the exported CSV in MATLAB?
Read the file back with readtable and inspect the resulting table to confirm structure and data types.
Read it back into MATLAB to make sure everything looks right.
Main Points
- Use writetable(T, 'file.csv') for quick CSV exports
- Specify 'Encoding','UTF-8' to preserve Unicode characters
- Use 'WriteVariableNames', false when appending to avoid header duplication
- Delimiters can be customized with 'Delimiter' for regional CSV formats
- Read back with readtable to verify the export correctness
