Main Content

save_system

R2026b

Save Simulink model

Description

fileName = save_system saves the current top-level model. If the model was not previously saved, save_system creates a file in the current folder.

To save a subsystem, copy the subsystem contents to a new model using the Simulink.SubSystem.copyContentsToBlockDiagram function, and then save the model using the save_system function.

example

filename = save_system(sys) saves the specified model. The model must be open or loaded.

example

filename = save_system(sys,newSys) saves the model to a file with the specified name. If you do not specify an extension, the software uses the file format specified in your Simulink® preferences.

example

filename = save_system(___,Name=Value) saves the model with additional options specified by one or more Name=Value pair arguments.

Examples

collapse all

Suppose you open two models sequentially. First, you open a model named myModel1 and make changes to the model. Then, you open a model named myModel2 and make changes to that model.

Save the current system.

save_system

The second model you open is the current system, so the command saves myModel2.

You can programmatically check which model is saved using the bdIsDirty function. If the model has unsaved changes, the function returns a value of 1. Otherwise, the function returns a value of 0.

mdls = ["myModel1","myModel2"];
bdIsDirty(mdls)
ans = 1×2 logical array
1	0

Close myModel2. Once myModel2 closes, myModel1 becomes the current system.

Run the command again.

save_system

The command saves myModel1.

You can verify using the bdIsDirty function.

mdls = ["myModel1","myModel2"];
bdIsDirty(mdls)
ans = 1×2 logical array
0	0

Suppose you have two open models, named myModel1 and myModel2. The model named myModel1 is the current system.

Save the model named myModel2.

save_system("myModel2")

Suppose you have an open model named myModel1.

Save the model with the new name myModel2. Return an error if the new name is shadowed — when the name is already used on the MATLAB® path or in the workspace.

save_system("myModel",ErrorIfShadowed=true)

Input Arguments

collapse all

Systems to save, specified as one or more handles or model names. Format the handles as numeric scalars or numeric arrays. Format the names as strings, string arrays, character vectors, or cell arrays of character vectors. Do not specify file extensions in the names.

For information about getting handles, see Get Handles and Paths.

Example: "myModel"

Example: ["myModel1";"myModel2"]

Data Types: double | array of doubles | string | string array | character vector | cell array of character vectors

Name with which the file is saved, specified as a string, string array, character vector, or cell array of character vectors. You can specify a model name in the current folder or the file path, with or without an extension.

When you specify a name without an extension, the model saves in the file format specified in your Simulink preferences. Possible model extensions are .slx and .mdl. With the 'ExportToXML' option, use the extension .xml.

For information on rules for naming models, see Choose Valid Model File Names.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: save_system("myModel",SaveModelWorkspace=true,BreakUserLinks=true,OverwriteIfChangedOnDisk=true)

Option to allow dialog box prompts. To turn the option on, specify true or "on". To turn the option off, specify the value as false or "off". By default, warnings and error messages appear at the command line.

Data Types: string | character vector | logical

Option to return an error if the new name exists on the MATLAB path or workspace. To turn the option on, specify true or "on". To turn the option off, specify the value as false or "off".

Data Types: string | character vector | logical

This option is no longer supported and will be removed in a future release.

Option to export the model to a file in a simple XML format. The block diagram in memory does not change and no callbacks execute. To use this option, you must use this syntax.

save_system(sys,newSys,ExportToXML=true)

Do not use this option with any additional Name=Value arguments.

When specifying the name of the exported file, include the .xml extension. To turn the option on, specify the value of ExportToXML as true or "on". To turn the option off, specify the value as false or "off".

Example: save_system("myModel","myExport.xml",ExportToXML=true)

Data Types: string | character vector | logical

MATLAB release name to which you want to export the model, specified as a string or character vector. To use this option, you must use this syntax.

save_system(sys,newSys,Name=Value)

Specify the export version in one of these forms (not case sensitive). You can export to seven years of previous releases.

  • Release name, for example, "R2023B", "R2020B"

  • Release name, followed by an underscore and then the extension, for example, "R2016A_SLX", "R2022A_MDL". If you do not specify an extension, you export to the file format specified in your Simulink preferences.

save_system exports the model such that the specified Simulink version can load it. If the model contains functionality not supported by the specified Simulink version, the command removes the functionality in the exported file. It also replaces unsupported blocks with empty masked subsystem blocks colored yellow. As a result, the exported model might generate different results.

Alternatively, use Simulink.exportToVersion or, interactively, the Export to Previous Version dialog box.

To export a project to previous releases, see Export a Project and References to Previous Version.

Example: save_system("myModel1","myModel2",ExportToVersion="R2022A")

Data Types: string | character vector

Option to overwrite the file on disk even if it has been modified since the model was loaded. To turn the option on, specify true or "on". To turn the option off, specify the value as false or "off".

By default, if the file changed on disk since the model was loaded, the function displays an error to prevent the changes on disk from being overwritten. You can control whether the function displays an error if the file has changed on disk using a Simulink preference. In the Model File pane of the Simulink Preferences dialog box, under Change Notification, select Saving the model. This preference is on by default.

Data Types: string | character vector | logical

Option to save referenced models that have unsaved changes while saving changes to their parent model. To turn the option on, specify true or "on". To turn the option off, specify the value as false or "off". This option only applies to models that are directly referenced by each model that is saved. If the parent model of a dirty referenced model is not saved, the dirty referenced model is not saved.

Suppose you have a model hierarchy in which model A references model B, and model B references model C. If models B and C both have unsaved changes, they are both saved. If model C has unsaved changes but model B does not have unsaved changes, neither model is saved.

By default, attempting to save a model that contains unsaved referenced models returns an error.

Data Types: string | character vector | logical

Option to save the contents of the model workspace. To turn the option on, specify true or "on". To turn the option off, specify the value as false or "off". The model workspace DataSource must be a MAT file. If the data source is not a MAT file, save_system does not save the workspace. See Specify Source for Data in Model Workspace.

Data Types: string | character vector | logical

Output Arguments

collapse all

File path of saved file, returned as a character vector or a cell array of character vectors.

Data Types: character vector | cell array of character vectors

Version History

Introduced before R2006a