Plot CSV data in MATLAB
How to plot a CSV file in MATLAB: what importing changes in your data, when readtable or readmatrix fits, common pitfalls, and limits when sharing plots.
Plotting a CSV file in MATLAB takes two steps. MATLAB first converts delimited text into a typed variable, usually a table, and then draws a figure from it. Most problems come from the first step, because the import decides which columns become numbers, dates or text. This review explains what that conversion changes, when it is the right approach, and what holds up when you share the result. It is based on documented MATLAB behavior. No test files or measured results were supplied, so behavior that varies by MATLAB release or file layout is flagged rather than asserted.
Is importing and plotting in MATLAB the right fit?
For numeric or time-based columns that you want to inspect, compare or chart repeatedly, importing the CSV into a MATLAB table is a sound default. Once the data is imported, you can refer to columns by name, change the plot in code, and rerun the script when the file updates.
The main tradeoff is that the import interprets your text. Each column is assigned a type, and header names may be changed. A wrong type guess can silently turn values into NaN or leave a date column as plain text. For files with irregular layouts, check the import before trusting the plot.
Keeping the data as plain text is the better choice when you only need to read or hand-edit the file, when exact original formatting matters (leading zeros, fixed decimal places, identifier strings), or when the recipient does not use MATLAB. In those cases, keep the CSV as the source of record and treat the MATLAB variable and figure as derived outputs.
A minimal workflow
The usual entry point is readtable. It reads a delimited text file into a table, using the first row as variable names when it looks like a header. The example below assumes a file with columns named Time and Value. Replace these with your own column names.
T = readtable('data.csv');
plot(T.Time, T.Value)
xlabel('Time')
ylabel('Value')
title('Value over time')
grid on
If the file contains only numbers and you do not need column names, readmatrix returns a plain numeric array instead. You then plot by column index.
M = readmatrix('data.csv');
plot(M(:,1), M(:,2))
Older functions such as csvread are marked as not recommended in current MATLAB documentation, so new scripts should use readtable or readmatrix.
What the import actually changes
The CSV on disk is not modified. Every change happens to the in-memory copy, and the figure shows that copy, not the original text. The following changes are the ones most likely to affect a plot.
- Column types: each column is detected as numeric, datetime, duration, text or categorical. A single stray non-numeric entry can cause a whole column to be read as text, which plot cannot use as y-values.
- Header names: by default, readtable converts headers into valid MATLAB identifiers. For example, spaces and symbols are replaced, so a header like Temp (C) arrives under a different name. Setting VariableNamingRule to 'preserve' keeps the original text, but you then refer to the column with T.("Temp (C)").
- Missing values: empty numeric cells become NaN. plot skips NaN points, so gaps appear in lines rather than errors.
- Number formatting: leading zeros, trailing zeros and the exact number of decimal places are not kept once text becomes a number. Identifiers that look numeric, such as ZIP codes or part numbers, should be imported as text.
- Dates: date strings may or may not be detected automatically, depending on their format. An undetected date column stays as text, and the x-axis will not behave like a time axis.
Controlling the import when defaults guess wrong
When the automatic detection is wrong, create an import options object, correct it, and pass it to readtable. This makes the conversion explicit and repeatable. Change the column names and date format in the example to match your file.
opts = detectImportOptions('data.csv');
opts = setvartype(opts, 'Date', 'datetime');
opts = setvaropts(opts, 'Date', 'InputFormat', 'yyyy-MM-dd');
opts = setvartype(opts, 'PartID', 'string');
T = readtable('data.csv', opts);
plot(T.Date, T.Reading)
For tab-separated or other delimited files, state the delimiter instead of relying on detection. Files that use a comma as the decimal separator also need that stated explicitly.
T = readtable('data.tsv', 'FileType', 'text', 'Delimiter', '\t');
T = readtable('data_eu.csv', 'Delimiter', ';', 'DecimalSeparator', ',');
Before plotting an unfamiliar file, run head(T) and summary(T) to confirm the column names, types and missing-value counts. This is the quickest way to catch a misread column.
Choosing a plot type
| Data shape | Suggested function | Note |
| One series over an ordered x or time column | plot | Rows should be sorted by x, or the line will zigzag |
| Two numeric columns, no meaningful order | scatter | Avoids drawing misleading connecting lines |
| Several numeric columns sharing one x-axis | plot with hold on, or stackedplot | stackedplot gives each variable its own axis |
| Category labels with one value each | bar | Import the label column as text or categorical |
| Distribution of one column | histogram | NaN values are excluded |
Will the result stay usable after sharing?
A figure exported as an image, for example with exportgraphics or saveas, keeps the appearance of the plot but not the underlying values. Readers cannot copy numbers out of a raster image, and search tools cannot index them. If others need the data, share the original CSV or an exported table (writetable) alongside the image.
A CSV written back out from MATLAB may differ from the original, since numeric formatting, header names and date formats reflect the imported types. Do not assume a round trip is lossless. Compare the files before replacing a source.
For accessibility, a chart image on its own does not convey values to screen reader users. Give it a text description that states what the plot shows, or provide the data table it was drawn from. Label axes with units, and avoid using color as the only way to tell series apart. How a particular document editor, slide tool or web page handles alt text for an exported figure was not tested here, so check that in the destination you actually use.
Gaps in this review
No sample files, MATLAB release details or rendered outputs were provided. As a result, this review does not confirm how automatic type and date detection behaves on any specific file, or which name-value options are available in a given older release. If your installation rejects an option shown here, check the readtable documentation for that release. As a general rule, inspect the imported table before drawing conclusions from the plot.