Main Content

save_system

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, instead use Simulink.SubSystem.copyContentsToBlockDiagram to copy the subsystem contents to a new model. You can then save that model using save_system. See Simulink.SubSystem.copyContentsToBlockDiagram.

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

example

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

example

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

example

filename = save_system(sys,newsys,Name,Value) saves the model to a new file with additional options specified by one or more Name,Value pair arguments. To use Name,Value pairs without saving to a new file, use [] for newsys.

example

Examples

collapse all

Create a model.

new_system('myModel')

Save the model.

save_system('myModel')

Open the example. Then, open the vdp model.

open_system('vdp')

Save the model in the current folder with the name myModel.

save_system('vdp','myModel')

Without a file extension, the function saves the model in the format specified by your Simulink® preferences.

After you save the model by another name, the model is no longer open under its original name.

Open the vdp model again.

open_system('vdp')

Save the model as a .mdl file in the current folder.

save_system('vdp','myModel2.mdl')

Suppose you have a model named myModel.

Save the model with a new name and return an error if something with this name exists on the MATLAB® path.

open_system('myModel')
save_system('myModel','max','ErrorIfShadowed',true)
Error using save_system (line 38)
The model 'myModel' cannot be saved with the new name 'max', because this name is
shadowing another name on the MATLAB path or in the workspace.  Choose another
name, or do not use the option 'ErrorIfShadowed'

In this case, save_system displays an error because max is the name of a MATLAB function. The model is not saved.

Suppose you have a model named myModel.

Open the model.

open_system('myModel')

Save the model with the name myModel2. Also save the model workspace, break links to user-defined library blocks in the model, and overwrite if the file has changed on the disk.

save_system('myModel','myModel2','SaveModelWorkspace', 
true,'BreakUserLinks',true,'OverwriteIfChangedOnDisk',true)

Suppose you have a model named myModel.

Open the model.

open_system('myModel')

Save the model without changing its name. Break links to user-defined library blocks in the model.

save_system('myModel','BreakUserLinks',true)

Input Arguments

collapse all

Name of model to save, specified as a character, cell array of character vectors, string array, handle, or array of handles. Do not use a file extension.

File to save to, specified as a character vector, cell array of character vectors, string array, or, to use Name,Value pairs without changing the file name, []. You can specify a model name in the current folder or the full path name, with or without an extension.

With no an extension, save_system saves to 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

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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: save_system('mymodel','newmodel','SaveModelWorkspace',true,'BreakUserLinks',true,'OverwriteIfChangedOnDisk',true)

Option to allow dialog box prompts, specified as true, false, 'on', or 'off'. By default, warnings and error messages appear at the command line.

Option to return an error if the new name exists on the MATLAB path or workspace, specified as true, false, 'on', or 'off'.

Option to export the model to a file in a simple XML format, specified as true, false, 'on', or 'off'. Specify the full name of the file, including the .xml extension. The block diagram in memory does not change and no callbacks execute. Use this option without any other Name,Value pair arguments. This option warns and will be removed in a future release.

Example: save_system('mymodel','exportfile.xml','ExportToXML',true)

MATLAB release name to export to, specified in either of these forms (not case sensitive). You can export to seven years of previous releases.

  • Release name, for example, 'R2013B', 'R2016B'

  • Release name, followed by an underscore and then the extension, for example, 'R2016A_SLX', 'R2014A_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 to a Previous Version.

Option to overwrite the file on disk even if it has been modified since the model was loaded, specified as true, false, 'on', or 'off'. By default, if the file changed on disk since the model was loaded, save_system displays an error to prevent the changes on disk from being overwritten.

You can control whether save_system 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.

Option to save referenced models that have unsaved changes while saving changes to their parent model, specified as true, false, 'on', or 'off'. This option 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 return an error.

Option to save the contents of the model workspace, specified as true, false, 'on', 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.

Output Arguments

collapse all

Full name of saved file, returned as a character vector or a cell array of character vectors.

Version History

Introduced before R2006a