Main Content

Simulink.saveVars

(Not recommended) Save workspace variables and their values in MATLAB code format

Simulink.saveVars is not recommended. Use matlab.io.saveVariablesToScript instead.

Description

example

Simulink.saveVars(filename) saves all variables in the current workspace for which MATLAB® code can be generated to a MATLAB file named filename.m. If MATLAB code cannot be generated for a variable, the variable is saved into a companion MAT-file named filename.mat, and a warning is generated. If either file already exists, it is overwritten. The file name cannot match the name of any variable in the current workspace, and can optionally include the suffix .m. Using Simulink.saveVars has no effect on the contents of any workspace.

Executing the MATLAB file restores the variables saved in the file to the current workspace. If a companion MAT-file exists, code in the MATLAB file loads the MAT-file, restoring its variables also. When both a MATLAB file and a MAT-file exist, do not load the MATLAB file unless the MAT file is available, or an error will occur. Do not load a MAT-file directly, or incomplete data restoration will result. No warning occurs if loading a file overwrites any existing variables.

You can edit a MATLAB file that Simulink.saveVars creates. You can insert comments between or within the MATLAB code sections for saved variables. However, if you later use Simulink.saveVars to update or append to the file, only comments between MATLAB code sections will be preserved. Internal comments should therefore be used only in files that you do not expect to change any further.

You must not edit the header section in the MATLAB file, which comprises the first five comment lines. Simulink® does not check that a manually edited MATLAB file is syntactically correct. Do not edit any MATLAB code in the file. You cannot edit a MAT-file and should never attempt to do so.

Simulink.saveVars(filename, var1,...,varN) saves only the variables specified by var1,...,varN. You can use the wildcard character * to save all variables that match a pattern. The * matches one or more characters, including non-alphanumeric characters.

Simulink.saveVars(filename,'-regexp',RegExps) saves only variables whose names match one of the regular expressions in the comma-separated list of expressions RegExps. See Regular Expressions for more information. A call to the function can specify both VarNames and '-regexps', RegExps, in that order and comma-separated.

Simulink.saveVars(filename,___, UpdateOption) saves the variables as directed by UpdateOption.

Simulink.saveVars(filename,___, Configuration) saves the variables according to the specified configuration options.

Simulink.saveVars(filename, ___, MatlabVer) saves any MAT-file that it creates in the format required by the MATLAB version specified by MatlabVer.

[r1, r2] = Simulink.saveVars(filename, ___) returns the variables saved in r1 and r2.

Examples

collapse all

Define some base workspace variables, then save them to a MATLAB file using a variety of input arguments.

Define some base workspace variables then save them to a new MATLAB file myVars.m.

a = 1;
b = 2.5;
c = 'A string';
d = {a,b,c};
Simulink.saveVars('myVars');

Define additional base workspace variables, then append them to the existing file myVars.m without changing the values previously saved in the file.

K = Simulink.Parameter;
myType = fixdt(1,16,3);
Simulink.saveVars('myVars','-append','K','myType');

Update the variables V1 and V2 with their values in a MATLAB file, or for any whose value cannot be converted to MATLAB code, in a MAT-file. The file must already exist. Any array with more than 10 elements will be saved to a MAT-file that can be loaded on any version of MATLAB. The return argument r1 lists the names of any variables saved to a MATLAB file; r2 lists any saved to a MAT-file.

[r1, r2] = Simulink.saveVars('MyFile', 'V1', 'V2', '-update',
'-maxnumel', 10, '-v4');

Specify a 2-D slice for the output of the my3Dtable 3-D array. Specify that the 2-D slice expands along the first and third dimensions. Review the generated MATLAB code.

my3DTable = zeros(3, 4, 2, 'single');
Simulink.saveVars('mfile.m', 'my3DTable', '-2dslice', 1, 3);
my3DTable = zeros(3, 4, 2, 'single');
my3DTable (:,1,:) = single ( ...
  [1 13;
   5 17;
   9 21]);
my3DTable (:,2,:) = single( ...
  [2 14;
   6 18;
   10 22]);
my3DTable (:,3,:) = single( ...
  [3 15;
   7 19;
   11 23]);
my3DTable (:,4,:) = single( ...
  [4 16;
   8 20;
   12 24]);

Input Arguments

collapse all

Name of file that the function creates or updates, specified as a character vector or string scalar. The file name cannot match the name of any variable in the current workspace. The file name can have the suffix .m, but the function ignores it.

A variable or sequence of comma-separated variables, specified as a character vector or string scalar. The function saves only the specified variables to the output file. You can use the wildcard character * to save all variables that match a pattern. The * matches one or more characters, including non-alphanumeric characters.

Example: Simulink.saveVars('myFile','A*') saves all variables in the workspace whose names start with A with their values.

A regular expression or sequence of comma-separated regular expressions, specified as a character vector or string. The function saves to the output file only those variables whose names match one of the expressions. See Regular Expressions for more information. A call to the function can specify both VarNames and '-regexp', RegExps, in that order and comma-separated.

Example: Simulink.saveVars('myFile','-regexp','v*')

Keyword that controls the action of the function, specified as one of these values:

  • '-create' — Create a new MATLAB file (and MAT-file if needed).

  • '-update' — Update the existing MATLAB file (and MAT-file if needed) specified by filename by changing only the specified variables that already exist in the file or files. The order of the variables in the file or files is preserved.

  • '-append' — Update the existing MATLAB file (and MAT-file if needed) specified by filename by:

    • Updating the specified variables that already exist in the file or files, preserving the existing order.

    • Appending the specified variables that do not exist in the file or files.

Configuration options for the function, specified as any or all of these values, in any order, separated by commas if more than one appears:

  • '-maxnumel' MaxNum — Limits the number of elements saved for an array to MaxNum, which must be an integer between 0 and 10000. If an array is larger than that, the whole array appears in the MAT-file rather than the MATLAB script file, generating a warning. Default: 1000

  • '-maxlevels' MaxLevels — Limits the number of levels saved for a structure or cell array to MaxLevels, which must be an integer between 0 and 200. If a structure or cell array is deeper than that, the whole entity appears in the MAT-file rather than the MATLAB script file, generating a warning. Default: 20

  • '-textwidth' TextWidth — Sets the text wrap width in the MATLAB script file to TextWidth, which must be an integer between 32 and 256. Default: 76

  • '-2dslice' — Sets two dimensions for 2-D slices that represent n-D (where n is greater than 2) arrays of char, logic, or numeric data. Using the '-2dslice' option produces more readable generated code that is consistent with how MATLAB displays n-D array data.

    Simulink.saveVars uses the first two dimensions of the n-D array to specify the size of the 2-D slice, unless you supply two positive integer arguments after the -2dslice option. If you specify two integer arguments:

    • The two integers must be positive.

    • The two integers must be less than or equal to the number of dimensions of the n-D array.

    • The second integer must be greater than the first.

Note

You can use the MATLAB Preferences to change the defaults for the -maxnumel, -maxlevels, '-2dslice', and -textwidth configuration options. In the Workspace pane, use the options in the Saving variables as MATLAB script files group.

MATLAB version whose syntax will be used by any MAT-file saved by the function, specified by values including:

  • '-v7.3' — 7.3 or later

  • '-v7.0' — 7.0 or later

  • '-v6' — Version 6 or later

  • '-v4' — Any MATLAB version

Output Arguments

collapse all

Variables that were saved to a MATLAB file, specified as a cell array of character vectors.

Variables that were saved to a MAT-file, specified as a cell array of character vectors.

Limitations

The Simulink.saveVars function:

  • Does not preserve shared references.

  • Ignores dynamic properties of objects.

  • Saves the following to the MAT-file although they could appear in the MATLAB file:

If you save many variables, the generated MATLAB file can contain many lines of code and take a long time to execute. To avoid the long execution time, consider these alternatives:

Tips

  • If you do not need to save variables in an easily-understood form, see the save function.

  • If you need to save only bus objects, use the Simulink.Bus.save function.

  • If you need to save only a configuration set, use the Simulink.ConfigSet.saveAs method.

Introduced in R2010a