Save the Model
You can tell that a model has unsaved changes by looking at the title bar in the Simulink® Editor. An asterisk (also known as the dirty flag) appears in the title bar when a model has unsaved changes..
To determine programmatically whether a model has unsaved changes, you can query the value
of the model parameter Dirty
with the get_param
function. For example, this command saves the current system
(gcs
) if it is dirty:
if strcmp(get_param(gcs,'Dirty'),'on') save_system; end
Save a Model
To save a model for the first time, in the Simulink Editor, on the Simulation tab, click Save. Provide a location and name for the model file. To see the naming requirements, see Model Names.
To save a previously saved model, follow one of these processes:
To save the model using the current file name, in the Simulink Editor, on the Simulation tab, click Save.
To save the contents of the top model to a new name or location, or to change the model format, in the Simulink Editor, on the Simulation tab, select Save > Save As.
Note
For details about the SLX format, see Convert File Format to SLX.
To save the contents of a referenced model to a new name, location, or format, open the referenced model as a top model. Then, on the Simulation tab, select Save > Save As.
To save the top model in a format compatible with an earlier Simulink version, on the Simulation tab, select Save > Previous Version. See Export Model to Previous Simulink Version.
Model Names
Model file names must start with a letter and can contain letters, numbers, and underscores. The file name must not be:
A language keyword (for example,
if
,for
,end
)A reserved name:
'simulink'
,'sl'
,'sf'
A MATLAB® software command
The total number of characters in the model name must not be greater than a certain
maximum, usually 63 characters. To find the maximum file name length for your system, use
the MATLAB
namelengthmax
command.
To understand how MATLAB determines which function to call when you specify a model name, see Function Precedence Order.
How Simulink Saves Models
When you save a model, Simulink saves the block diagram, block properties, and other information associated with the model in the model file.
If you have any pre-save or post-save callbacks, they execute in this order:
All block
PreSaveFcn
callback routines execute, then the modelPreSaveFcn
callback routine executes.Simulink writes the model file.
All block
PostSaveFcn
callback routines execute, then the modelPostSaveFcn
executes.
During the save process, Simulink maintains a temporary backup copy (named
) that it uses to restore the
model in case of an error. If an error occurs during saving or during any callback during
the save process, Simulink:modelname
.bak
Restores the original file
Writes any content saved before the error occurred in a file named
modelname
.errIssues an error message
When saving a model loaded from an SLX file, the original SLX file must still be present. Simulink performs incremental loading and saving of SLX files, so if the original file is missing at save-time, Simulink warns that it cannot fully reconstruct the file.
Save Models in the SLX File Format
Save New Models as SLX
Simulink saves new models and libraries in the SLX format by default, with file
extension .slx
. The SLX format is a compressed package that conforms to
the Open Packaging Conventions (OPC) interoperability standard. The SLX format stores
model information using Unicode® UTF-8 in XML and other international formats. Saving Simulink models in the SLX format:
Typically reduces file size compared to MDL files. The file size reduction between MDL and SLX varies depending on the model.
Enables incremental loading and saving. Simulink optimizes performance and memory usage by loading only required parts of the model and saving only modified parts of the model.
You can specify your file format for saving new models and libraries with the Simulink preference File format for new models and libraries.
Convert File Format to SLX
If you convert an MDL file to the SLX file format, the file contains the same
information as the MDL file. Functions that works with MDL files, such as
get_param
and set_param
, also work with the
SLX file format. If you convert an MDL file to the SLX file format without changing the
model name or location, then Simulink creates a backup file by renaming the MDL file (if the file is
writable).
If you save an existing MDL file by clicking Save on the Simulation tab, Simulink saves your model in MDL format.
To save an existing MDL file in the SLX file format:
On the Simulation tab, select Save > Save As.
Leave the default Save as type as SLX, and click Save.
Simulink saves your model in SLX format and creates a backup file by renaming the MDL file (if the file is writable) to
mymodel.mdl.
, for example,releasename
mymodel.mdl.R2010b
.
Alternatively, use save_system
:
save_system mymodel mymodel.slx
mymodel.slx
, and if the existing file
mymodel.mdl
is writable, it is renamed
mymodel.mdl.releasename
.SLX files take precedence over MDL files, so if both exist with the same name and you do not specify a file extension, you load the SLX file.
Caution
If you use third-party source control tools, register the model file extension
.slx
as a binary file format. If you do not, these third-party
tools might corrupt SLX files when you submit them.
The following table describes operations with possible compatibility considerations when using SLX files.
Operations with Possible Compatibility Considerations when Using SLX | What Happens | Action |
---|---|---|
Hard-coded references to file names with the extension
.mdl | Scripts cannot find or process models saved with the new file extension,
.slx . | Make your code work with both the Use functions like
|
Third-party source control tools that assume a text format by default | The binary format of SLX files can cause third-party tools to corrupt the files when you submit them. | Register the .slx and .mdl extensions
as binary file formats with third-party source control tools. See Register Model Files with Source Control Tools. |
The format of content within MDL and SLX files is subject to change. To operate on
model data, use documented functions (such as get_param
, find_system
, and Simulink.MDLInfo
).
Export Model to Previous Simulink Version
You can export a model created with the latest version of the Simulink software in a format used by an earlier version. For example, you can share a model with colleagues who only have access to a previous version of Simulink.
To export a model in an earlier format:
In the Simulink Editor, on the Simulation tab, click Save. This saves a copy in the latest version of Simulink. This step avoids compatibility problems.
In the Simulink Editor, on the Simulation tab, select Save > Previous Version.
In the Export to Previous Version dialog box, from the Save as type list, select the previous version to which to export the model. The list supports seven years of previous releases.
Click Save.
When you export a model to a format of a previous version, the model is saved in the
earlier format, regardless of whether the model contains blocks and features that were
introduced after that version. If the model contains blocks or uses features that postdate
the earlier version, the model might not give correct results when you run it in the earlier
version of Simulink software. In addition, Simulink converts blocks that postdate an earlier version into yellow, empty, masked
Subsystem blocks. For example, if you use save_system
to export a model to Release R2007b, and the model contains Polynomial
blocks, Simulink converts the Polynomial blocks into yellow, empty, masked
Subsystem blocks. Simulink also removes any unsupported functionality from the model. See save_system
.
Save from Earlier Simulink Version to Another Earlier Version
You can open a model created in an earlier version of Simulink and export that model to a different earlier version. To prevent compatibility problems, use the following procedure:
Use the current version of Simulink to open the model created with the earlier version.
Before you make any changes, save the model in the current version by clicking Save on the Simulation tab.
After saving the model in the current version, you can change and resave it as needed.
Save the model in the earlier version of Simulink. On the Simulation tab, select Save > Previous Version.
Start the earlier Simulink version and use it to open the model that you exported to that earlier version.
Save the model in the earlier version.
You can now use the model in the earlier version of Simulink exactly as you could if it had been created in that version.
These Simulink preferences can help you work with models from earlier versions: