Written and maintained by CASRAI Editorial Board
Last updated
MATLAB variables live in the workspace and disappear when the session ends unless you save them. This guide is for researchers and analysts who need to save MATLAB work to a file and load it back reliably: which route to use (the Save Workspace button or the save command), how to add to an existing file, and what to do when a variable is too large for the default format.
What saving in MATLAB is used for in research
Saving the workspace writes variables to a MAT-file, MATLAB’s own binary data format, so an analysis can be resumed, shared with a collaborator or archived with a project. Per MathWorks’ documentation, variables do not persist between sessions unless you save them; MATLAB Online is the stated exception. Saving is separate from saving code: scripts and functions are saved from the Editor, while save and Save Workspace store data. For the data-table case specifically, see working with tables in MATLAB.
Install or access (official source only)
MATLAB is distributed by MathWorks; obtain and activate it only through mathworks.com. Installation steps are covered in MATLAB installation. The commands below are taken from the MathWorks documentation for save and for saving, loading and deleting workspace variables, both read on 2026-11-07.
Step-by-step: save a workspace in MATLAB
How do you save a workspace in MATLAB?
Use the Home tab or the save command. Both write a MAT-file; the command form lets you script it.
Option A: the desktop
- To save everything, go to the Home tab, open the Variable section and click Save Workspace.
- To save only some variables, select them in the Workspace panel, right-click and choose Save Selection.
- In the dialog, choose a file name and location. Choosing the MATLAB Script type writes the variables as a script; variables that cannot be written to a script are saved to a MAT-file with the same name.
Option B: the save command
- Save all variables:
save("june10")writesjune10.matin the current folder. - Save chosen variables:
save("june10","A","B"). - Check what was written:
whos -file june10.matlists the variables in the file (sizes shown are in-memory sizes).
Decision table: which save form to use
| You want to | Use | Note from the documentation |
|---|---|---|
| Save everything once, by hand | Home > Variable > Save Workspace | Dialog also offers the MATLAB Script type |
| Save a few variables by hand | Select in Workspace panel, right-click > Save Selection | Saves only the selection |
| Save everything from code | save(filename) |
Command form save filename also works |
| Save named variables | save(filename,variables) |
Variable names given as strings |
| Add variables to an existing file | save(filename,variables,"-append") |
Adds new variables to an existing file; for a Version 6 file also specify -v6 |
| Store struct fields as separate variables | save(filename,"-struct","structName") |
Saves the fields of a scalar structure as individual variables |
| Save a variable of 2 GB or more | save(filename,variables,"-v7.3") |
v7.3 supports variables of 2 GB or more on 64-bit systems; v7, v6 and v4 are limited to 231 bytes per variable |
| Skip compression | save(filename,variables,"-nocompression") |
Supported only for v7 and v7.3; v7+ files are compressed by default |
| Write plain text numbers | save(filename,variables,"-ascii") |
8-digit precision; add -double for 17 digits |
The default MAT-file version is v7, and you can change it in Settings under the MAT-file save format. The -ascii route writes text rather than a MAT-file, so it does not preserve variable names or types; use it only when another program needs plain numbers.
Output and how to read it
Load a saved file back
Load with load("durer") for every variable, or load("durer","X","map") for named variables. In the desktop you can double-click the MAT-file in the Files panel, drag it into the Workspace, or use Import Data to bring in a subset.
Two behaviours to plan for:
- Loaded variables overwrite variables of the same name already in the workspace.
S = load("durer")returns the contents as a structureS, so nothing in the workspace is overwritten; read fields asS.X.
A simple round-trip check: save("check","A"), clear A, load("check"), then confirm A is back with whos. To work with only part of a very large variable without loading all of it, the documentation points to matfile.
Troubleshooting
- A variable is too large to save. Version 7 and earlier files cap each variable at 231 bytes (2 GB). Save with
-v7.3. - Appending to an older file fails or behaves unexpectedly.
-appendadds variables to an existing file; for a Version 6 file specify-v6as well. - Loading overwrote my variables. That is the documented behaviour; load into a structure with
S = load(...)or load only named variables. - File size looks different from
whos.whos -filereports in-memory sizes, and v7 or later MAT-files are compressed by default. - Variables vanished after restarting MATLAB. They are not kept between sessions unless saved (MATLAB Online excepted).
Free alternatives (table)
If a collaborator has no MATLAB licence, these tools read or write MAT-files. Limits are from each project’s own documentation, read 2026-11-07.
| Tool | Save | Limits stated in its documentation |
|---|---|---|
| SciPy (Python) | scipy.io.savemat(file_name, mdict) writes a dictionary of arrays to a .mat file |
Default format='5' targets MATLAB 5 up to 7.2; compression is off unless do_compression=True; structure field names limited to 31 characters unless long_field_names=True (needs MATLAB 7.6 or later) |
| GNU Octave | save with -v7, -v6 or -v4 writes MATLAB-compatible files |
Default format is Octave text, not MAT; saving v7.3 is not supported; load reads MATLAB v4, v6 and v7, with only limited -hdf5 support for v7.3 files |
FAQ
How do you save a workspace in MATLAB?
On the Home tab, in the Variable section, click Save Workspace; or run save("filename") to write all variables to filename.mat. To save only some, select them in the Workspace panel and choose Save Selection, or list them: save("filename","A","B").
Is MATLAB autosave on for the workspace?
The documentation says variables are not retained between sessions unless you save them, with MATLAB Online as the exception. Treat the workspace as unsaved until you have written a MAT-file.
Is .m file for MATLAB?
Yes. .m is the extension of MATLAB code files (scripts and functions), which you save from the Editor. A .mat file holds data. The Save Workspace dialog can also write variables as a MATLAB Script, which produces a code file rather than a MAT-file.
Can I save MATLAB data as text?
Yes: save(filename,variables,"-ascii") writes 8-digit text, or 17 digits with -double, but names and types are not kept.
Sources (official docs)
- MathWorks, save, read 2026-11-07.
- MathWorks, Save, load and delete workspace variables, read 2026-11-07.
- SciPy, scipy.io.savemat, read 2026-11-07.
- GNU Octave, Simple File I/O, read 2026-11-07.
Related guides: printing from MATLAB, MATLAB graphing.







