save_system
R2026bSave Simulink model
Syntax
Description
saves the current top-level
model. If the model was not previously saved, fileName = save_systemsave_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.
saves the model with additional options specified by one or more filename = save_system(___,Name=Value)Name=Value
pair arguments.
Examples
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
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
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 replace links to library blocks with copies of the library blocks in the saved
file. To turn the option on, specify true or "on". To
turn the option off, specify the value as false or
"off". This option affects user-defined blocks and Simulink library blocks.
Caution
The BreakAllLinks option can result in compatibility issues when
upgrading to newer versions of Simulink. For example:
Any masks on top of library links to Simulink S-functions do not upgrade to the new version of the S-function.
Any library links to masked subsystems in a Simulink library do not upgrade to the new subsystem behavior.
Any broken links prevent the library forwarding mechanism from upgrading the link. See Maintain Compatibility of Library Blocks Using Forwarding Tables.
If you saved a model with broken links to built-in libraries, use the Upgrade Advisor to scan the model for out-of-date blocks. Then upgrade the Simulink blocks to their current versions.
Data Types: string | character vector | logical
Option to replace links to user-defined library blocks with copies of the library blocks
in the saved file. To turn the option on, specify true or
"on". To turn the option off, specify the value as
false or "off". You cannot break locked library links.
For more information, see Lock Links to Blocks in a Library.
Data Types: string | character vector | logical
Option to replace links to built-in library blocks with copies of the library blocks in
the saved file. To turn the option on, specify true or
"on". To turn the option off, specify the value as
false or "off". This option affects Simulink library blocks and blocks from libraries supplied with MathWorks® toolboxes or blocksets.
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
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
See Also
close_system | new_system | open_system | Simulink.exportToVersion
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)