Open a MATLAB .mat file without MATLAB, and save its variables as CSV
Drop a .mat file a supervisor, a dataset page or a lab instrument gave you. See every variable in it with its class, size and bytes, open a matrix as a table, read text, walk through structs and cell arrays, and save any matrix as CSV for Excel, R or Python, or the whole file as JSON.
The file is read by this tab on your device. It is not uploaded, so unpublished data stays yours.
What it shows
- The file's own header. A Level 5 file starts with 116 bytes of text such as
MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Tue Mar 12 09:14:02 2024: who wrote it, on which system and when. It is shown as written, with the byte order and how many variables are compressed. - Every variable, like
whos. Name, class (double,single,int8touint64,logical,char,cell,struct, sparse), dimensions such as1000×64×3, whether it is complex, sparse or global, and its size in bytes counted the way MATLAB'swhoscounts it. - Matrices as a table. Numeric and logical arrays open as a grid with row and column numbers counted from 1, as in MATLAB. The first 1,000 rows and 200 columns are drawn, with buttons for the next block and the true size stated above. A 3-D or larger array is shown one 2-D slice at a time: pick the index of each extra dimension, and the table shows
X(:, :, k). Complex values show as1.5-0.25i. - Text. A char array is shown as text, one line per row, and as a table of characters. A 2×5 char matrix holding
helloandworldreads as those two lines. - Structs and cell arrays as a tree. Tap a branch to open it:
results.trial(3).rtordata{2}. Short values show in place; a matrix or text inside has an Open button that shows it as a table, ready to save as CSV. - Sparse matrices as triplets. The non-zeros as (row, column, value), column by column as MATLAB stores them, with how full the matrix is. Save them as a three-column CSV, which
sparse(r, c, v)in MATLAB orscipy.sparse.coo_matrixturns back into the matrix.
Saving as CSV and JSON
- Save as CSV writes the whole 2-D variable, or the slice you picked of an N-D one, not only the part drawn on screen: one line per row, values separated by commas. Numbers are written at full precision in the shortest form that reads back to the same double (
0.1, not0.10000000000000001), integers exactly (anint64above 253 keeps every digit),NaN,Infand-Infas MATLAB writes them, logical values as 1 and 0. - Save all as JSON writes every variable in one file: matrices as arrays of rows, N-D arrays as their size plus the values in MATLAB's column-major order, text as strings, cell arrays and structs nested as they are in the file. NaN and Inf become the strings
"NaN"and"Inf", because JSON has no way to write them.
The formats, and what this reads in each
- v7 (MATLAB's default)
- What
savewrites unless told otherwise: a Level 5 file in which each variable is zlib-compressed on its own (the miCOMPRESSED data type). It is unpacked here with the bundled fflate library. Files from GNU Octave (save -v7) and SciPy (savemat(..., do_compression=True)) are the same format. - v6 and v5
- The same Level 5 layout without compression:
save -v6, MATLAB 5 to 6.5, andscipy.io.savematby default. Little-endian (IM) and big-endian (MI, from old Sun and Mac machines) files both open. - Level 4
- MATLAB 4's format, still written by
save -v4, Octave, SciPy (format='4') and some instruments: a 20-byte header per matrix and its values, no compression, numeric and text matrices only (and sparse ones as a list of triplets). A Level 4 file has no signature, so outside this page viewhack only recognises it when its name ends in .mat. - v7.3
- An HDF5 file behind a 512-byte MAT-file header, written by
save -v7.3and needed for variables over 2 GB. This page recognises it and says so, but cannot read it yet. Re-save it in MATLAB withS = load('data.mat'); save('data_v7.mat', '-struct', 'S', '-v7'); GNU Octave can usually load a v7.3 file of plain arrays and save it the same way.
What this cannot do
- Read v7.3 files. They are HDF5 inside, a different format altogether; see above for the two-line re-save.
- Show function handles. A saved
@(x) x.^2is listed by name and class only; what it points to is kept in MATLAB's own private encoding. - Show MATLAB class objects. Objects of
classdefclasses, includingstring,table,timetable,datetimeandcategorical, are stored in an undocumented subsystem format and are listed by name and class only. Convert them in MATLAB before saving (table2array,char,datenum,double). Old-style@classobjects show their fields. - Edit or write .mat files. It reads and exports; it does not change values or save a .mat back.
- Run MATLAB code, or plot. It shows numbers, it does not compute with them or draw graphs. For that, the CSV opens in a spreadsheet, and GNU Octave reads the .mat itself for free.
Opening the CSV elsewhere
- Excel or LibreOffice
- Open the .csv directly. The values have no header row, just as the matrix has no column names; row 1 of the sheet is row 1 of the matrix.
- Python
numpy.loadtxt('A.csv', delimiter=',')gives back the same matrix. NaN and Inf read correctly; complex values (written1+2i) need MATLAB'sichanged to Python'sjfirst.- R
as.matrix(read.csv('A.csv', header = FALSE)).