Skip to Main Content Skip to Search
Product Documentation

Saving a Model

Introduction

Changes that you make to a model with the model editor or via MATLAB commands affect only the in-memory copy of your model. To make the changes permanent you must save the model, as described in this section.

How to Tell If a Model Needs Saving

To tell whether a model needs saving, look at the title bar of the model window. An asterisk appears next to the model's name if the model needs saving as in the following example:

At the MATLAB command line and in M programs, you can use the model parameter, Dirty, to determine whether a model needs saving, for example,

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

Techniques for Saving Models

You can save a model by choosing either the Save or Save As command from the File menu. The model is saved by generating a specially formatted file called the model file (with the .mdl extension) that contains the block diagram and block properties.

If you are saving a model for the first time, use the Save command to provide a name and location for the model file. Model file names must start with a letter and can contain letters, numbers, and underscores. The total number must not be greater than a certain maximum, usually 63 characters. You can use the MATLAB software namelengthmax namelengthmax command to find out if the maximum is greater than 63 characters for your system. The file name must not be the same as that of a MATLAB software command.

If you are saving a model whose model file was previously saved, use the Save command to replace the file's contents or the Save As command to save the model with a new name or location. You can also use the Save As command to save the model in a format compatible with previous releases of the Simulink product (see Saving a Model in an Earlier Simulink Version).

The Simulink software follows this procedure while saving a model:

  1. If the mdl file for the model already exists, it is renamed as a temporary file.

  2. All block PreSaveFcn callback routines are executed first, then the block diagram's PreSaveFcn callback routine are executed.

  3. The model file is written to a new file using the same name and an extension of mdl.

  4. All block PostSaveFcn callback routines are executed, then the block diagram's PostSaveFcn callback routine is executed.

  5. The temporary file is deleted.

If an error occurs during this process, the Simulink software renames the temporary file to the name of the original model file, writes the current version of the model to a file with an .err extension, and issues an error message. If an error occurs in step 2, step 3 is omitted and steps 4 and 5 are performed.

Saving Models with Different Character Encodings

When a model is saved, the character encoding in effect when the model was created (the original encoding) is used to encode the text stored in the model's .mdl file, regardless of the character encoding in effect when the model is saved. This can lead to model corruption if you save a model whose original encoding differs from encoding currently in effect.

For example, it is possible that you introduced characters that cannot be represented in the model's original encoding. If this is the case, the model is saved as model.err where model is the model's name, leaving the original model file unchanged. The Simulink software also displays an error message that specifies the line and column number of the first character which cannot be represented. To recover from this error without losing all of the changes that you have made to the model in the current session, use the following procedure. First, use a text editor to find the character in the .err file at the position specified by the save error message. Then, find and delete the corresponding character in the open model and resave the model . Repeat this process until you are able to save the model without error.

It's possible that your model's original encoding can represent all the text changes that you've made in the current session, albeit incorrectly. For example, suppose you open a model whose original encoding is A in a session whose current encoding is B. Further suppose that you edit the model to include a character that has different encodings in A and B and then save the model. If in addition the encoding for x in B is the same as the encoding for y in A, and if you insert x in the model while B is in effect, save the model, and then reopen the model with A in effect the Simulink software will display x as y. To alert you to the possibility of such corruptions, the software displays a warning message whenever you save a model in which the current and original encoding differ but the original encoding can encode, possibly incorrectly, all of the characters to be saved in the model file.

Saving a Model in an Earlier Simulink Version

The Save As command allows you to save a model created with the latest version of the Simulink software in formats used by earlier versions, including: Simulink 6 (Release 14, Release 14SP1, Release 14SP2, Release 14SP3, Release 2006a, Release 2006b, and Release 2007a), and Simulink® 7 (Release 2007b, Release 2008a, Release 2008b, Release 2009a, Release 2009b, and Release 2010a). You might want to perform such a Save As if, for example, you need to make a model available to colleagues who only have access to one of these earlier versions of the Simulink product.

To save a model in an earlier format:

  1. Select Save from the File menu. This saves a copy in the latest version of Simulink. This step is necessary to avoid compatibility problems.

  2. Select Save As from the File menu.

    The Save As dialog box is displayed.

  3. Select a format from the Save as type list in the dialog box.

  4. Click the Save button.

When saving a model in an earlier version's format, 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 does contain blocks or use features that postdate the earlier version, the model might not give correct results when run by the earlier version. In addition, Simulink converts blocks that postdate an earlier version into empty masked Subsystem blocks colored yellow. For example, if you save a model that contains Polynomial blocks to Release R2007b, Simulink will convert the Polynomial blocks into empty masked Subsystem blocks and will color them yellow. Simulink also removes any unsupported functionality of the model.

Saving from One Earlier Simulink Version to Another

You can open a model created in an earlier version of Simulink and use Save as to save the model in a different earlier version. To prevent compatibility problems, use the following procedure if you need to save a model from one earlier version to another:

  1. Use the current version of Simulink to open the model created with the earlier version.

  2. Before making any changes, use Save to save the model in the current version.

    After saving the model in the current version, you can change and resave it as needed.

  3. Use Save as to save the model in the earlier version of Simulink.

  4. Start the earlier Simulink version and use it to open the saved model.

  5. Use Save to 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.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS