Simulink.saveVars
(Not recommended) Save workspace variables and their values in MATLAB code format
Simulink.saveVars is not recommended. Use matlab.io.saveVariablesToScript instead.
Syntax
Description
Simulink.saveVars( saves all
variables in the current workspace for which MATLAB® code can be generated to a MATLAB file named filename)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(
saves only the variables specified by filename, var1,...,varN)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(
saves only variables whose names match one of the regular expressions in the comma-separated
list of expressions filename,'-regexp',RegExps)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(
saves the variables as directed by filename,___, UpdateOption)UpdateOption.
Simulink.saveVars(
saves the variables according to the specified configuration options.filename,___, Configuration)
Examples
Input Arguments
Output Arguments
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:
Simulink.ConfigSetobjects with custom target components.
(Use theSimulink.ConfigSetmethodsaveAsinstead.)andSimulink.Timeseriesobjects.Simulink.ModelDataLogs
If a Simulink data object in the workspace contains a large array, the function saves only the numeric value to a MAT file and writes the rest of the data object to a 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:
Permanently store variables in a data dictionary instead of using
Simulink.saveVars. A data dictionary also provides more tools for managing variables. See Determine Where to Store Variables and Objects for Simulink Models.Save variables in a MAT file by using the
savefunction.
Tips
If you do not need to save variables in an easily-understood form, see the
savefunction.If you need to save only bus objects, use the
Simulink.Bus.savefunction.If you need to save only a configuration set, use the
Simulink.ConfigSet.saveAsmethod.