matlab.io.saveVariablesToScript

Saves workspace variables as a MATLAB script that recreates them.

Category: Environment and Settings > System Commands

Syntax

matlab.io.saveVariablesToScript(filename)

Writes every variable of the workspace to filename.

matlab.io.saveVariablesToScript(filename, varnames)

Writes only the named variables.

Description

Instead of a binary MAT-file, the result is plain code of the form x = 3; that anyone can read and edit, and running the script restores the variables. Numeric, char and logical values are written as literals. A variable that cannot be written as code is skipped and a comment in the file says so.

Input arguments

filename
Name of the script file to create.
varnames
Variable name, or a cell array of names, to save.

Example

Save and read back

x = 3;
b = [1 2 3];
c = 'hi';
matlab.io.saveVariablesToScript('demo_vars.m', {'x', 'b', 'c'});
clear x b c
demo_vars
disp(x)
disp(b)
disp(c)
delete('demo_vars.m')
Output
     3
     1     2     3
hi

See also

save load who clear