save

Saves workspace variables to a MAT-file or a text file.

Category: Data Import and Analysis > Data Import and Export

Syntax

save(filename)

Saves every workspace variable.

save(filename, var1, var2)

Saves only the named variables.

save(filename, ..., '-append')

Adds variables to an existing MAT-file, replacing those with the same name.

save(filename, var, '-ascii')

Writes numeric data as plain text.

Description

A MAT-file stores variables with their class, size and values, so they come back unchanged with load. Text output with '-ascii' is readable by other programs but loses class information and writes numbers in an exponent format.

In function-call form the variable names must be quoted: save('f.mat', 'x'). In command form, save f.mat x, quotes are omitted.

Input arguments

filename
File name. .mat is added when there is no extension.
var1, ...
Names of the variables to save, given as text.
'-append'
Keeps the variables already in the file.
'-ascii'
Text output; only numeric variables are allowed.

Example

Save, append and reload

a = 1; b = [1 2; 3 4]; s = 'text';
save('m1.mat', 'a', 's');
save('m1.mat', 'b', '-append');
q = load('m1.mat')
Output
q = struct with fields:

    a: 1
    b: [2×2 double]
    s: 'text'

See also

load matfile writematrix fopen