Main Content

Save Models

You can save a model, template, or library. For information about how to create templates and libraries, see Create Template from Model and Create Custom Library.

You can save Simulink® models in the SLX format or the MDL format.

By default, Simulink saves new models and libraries in the SLX format with the 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 varies depending on the model.

Saving models in the SLX format also 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 the file format for saving new models and libraries with the Simulink preference File format for new models and libraries.

In addition to the model itself, you can also store and save data that your model needs to run. While the model is open, you can use variables to store data in the model workspace and the base workspace. For more information about the difference between these workspaces, see Model Workspaces. When you close the model, the data is deleted unless you save it.

You can save the data as a MAT-File, or using a data dictionary. A data dictionary is a persistent repository of data that are relevant to your model. For more information about data dictionaries, see What Is a Data Dictionary?

Determine Whether a Model Has Unsaved Changes

If a model has unsaved changes, an asterisk (also known as the dirty flag) appears next to the model name in the title bar of the Simulink Editor.

Title bar showing an asterisk next to the model name

To determine programmatically whether a model has unsaved changes, query the value of the model parameter Dirty with the get_param function. For example, this command saves the current system (gcs) if the system is dirty.

if strcmp(get_param(gcs,'Dirty'),'on')
  save_system;
end

Save Models

To save a model for the first time, in the Simulink Editor, on the Simulation tab, click Save. For file naming requirements, see Choose Valid Model File Names.

To resave a previously saved model, do one of the following:

  • 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 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. For information about the SLX format, see Convert Model File Format to SLX.

  • To save the model in a format compatible with a previous Simulink version, on the Simulation tab, select Save > Previous Version. For more information, see Export Model to Previous Version of Simulink.

  • To save the contents of a referenced model to a new name, location, or format, open the file of the referenced model. Then, on the Simulation tab, select Save > Save As.

Choose Valid Model File Names

Model file names must start with a letter and can contain letters, numbers, and underscores. The total number of characters in the model name must not be greater than the maximum value that your system supports, which is usually 63 characters. To find the maximum file name length for your system, use the namelengthmax function.

You can open a model even when the file does not have a valid name, for example, mymodel (1).slx. Simulink opens the model and assigns a valid name. To rename or save changes to the newly named model, click Save.

When you name your model, do not use:

  • A language keyword (for example, if, for, or end)

  • A reserved name (for example, simulink)

  • A MATLAB® built-in function (for example, plot)

For information about how MATLAB determines which function to call when multiple files in the current scope have the same name, see Function Precedence Order.

Save Model Variables and Data

If your MATLAB workspace contains variables and data that your Simulink model needs to run, you can save those contents and reuse them the next time you open the model.

If you want to save all the contents of your workspace, in the MATLAB Editor, in the MATLAB Toolstrip, on the Home tab, in the Variable section, click Save Workspace. Save the data in a MAT-file.

If you want to save a specific item in your workspace, in the MATLAB Editor, in the Workspace Browser, right-click the item and select Save As. Save the item in a MAT-file.

The next time you open the model, before you run the simulation, load the MAT-file into the workspace. See Load Model Variables and Data from MAT-File.

Specify Code That Runs Before or After Saving Model

A callback is code that runs at a specified action point,such as right before or after you save a model.

You can specify the action point by choosing the corresponding type of callback.

  • A PreSaveFcn function runs right before you save a model.

  • A PostSaveFcn function runs right after you save a model.

For example, if you want to automatically clear the values of all workspace variables right after you save a model:

  1. If the Property Inspector is already open and is minimized, restore it by clicking Property Inspector at the right edge of the model window.

    If the Property Inspector is not open, in the Simulink Toolstrip, on the Modeling tab, in the Design section, click Property Inspector.

  2. On the Properties tab, in the Callbacks section, select PostSaveFcn.

  3. In the text box, enter this code.

    clearvars;

    Tip

    Alternatively, you can save the code as a MATLAB script and then enter the name of the script in the text box.

  4. Save the model.

Model Properties dialog box with the PostSaveFcn callback type selected and the clearvars command in the right pane

Now, every time you save the model, when the model finishes saving, the workspace is cleared.

To learn more about callbacks, see Customize Model Behavior with Callbacks. To define a callback for loading variables programmatically, see Programmatically Load Variables When Opening a Model.

Convert Model File Format to SLX

To convert an existing MDL file to the SLX file format:

  1. On the Simulation tab, select Save > Save As.

  2. Leave the default Save as type as SLX and click Save.

When you convert a 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.

When you convert a MDL file to the SLX file format without changing the model name or location, the software creates a backup file by renaming the MDL file (if the file is writable) to mymodel.mdl.releasename, for example, mymodel.mdl.R2010b.

Note

You can also convert the MDL file to the SLX file format programmatically using the save_system function.

save_system mymodel mymodel.slx

This command creates the file mymodel.slx. 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. Otherwise, the third-party tools might corrupt SLX files when you submit them.

This table describes operations with possible compatibility considerations when using SLX files.

Operations with Possible Compatibility Considerations when Using SLXWhat Happens Action
Hard-coded references to file names with the extension .mdlScripts cannot find or process models saved with the new file extension, .slx.

Make your code work with both the .mdl and .slx extension.

Use functions like which and what instead of file names.

Third-party source control tools that assume a text format by defaultThe 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 Version of Simulink

You can export a model to a previous version of Simulink. For example, you can share a model with colleagues who only have access to a previous version of Simulink.

To export a model to a previous version of Simulink:

  1. Ensure that all changes you made to the model are saved before proceeding. If a model has unsaved changes, in the Simulink Editor, on the Simulation tab, click Save.

  2. In the Simulink Editor, on the Simulation tab, select Save > Previous Version.

  3. 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.

  4. Click Save.

When you export a model to a previous version, the model is saved in the previous version regardless of whether the model contains blocks and features that were introduced after that version. If the model contains blocks or uses features that were developed after the previous version, the model might not give correct results when you run it in the previous version of Simulink.

Simulink converts blocks that were introduced in a previous version into yellow, empty, masked Subsystem blocks.

Simulink also removes any unsupported functionality from the model. See save_system.

See Also

Related Topics