Create, Load, Open, Save, and Close Models Programmatically
Using the functions listed on this page, you can programmatically run file operations on
Simulink® model, library, and subsystem files such as creating, loading, opening, saving,
and closing a file. For example, these commands create a new model named
myModel
, store the model handle in the variable named
h
, save the model, and then close the model.
h = new_system("myModel");
save_system(h);
close_system(h);
Specify which model, library, or subsystem you want a function to act on by specifying the filename or handle as a function input argument, typically the first input argument. To reduce run time, use handles rather than filenames. For more information, see the function documentation and Get Handles and Paths. If a model, library, or subsystem is not on the MATLAB® search path, specify the model, library, or subsystem using its file path instead of its name. Unless stated otherwise, the models, libraries, and subsystems in the examples on this page are all on the MATLAB search path.
To edit, save or close a model, library, or subsystem, it must be loaded. Changes you make to a model, library, or subsystem programmatically are not saved automatically. If you make changes without opening a model, library, or subsystem file and then open the file, the file contains your changes. However, the changes are not saved. If you close without saving, you lose the changes.
Start Simulink
You do not need to explicitly start Simulink to edit models. The software starts Simulink automatically when you run Simulink functions. However, opening a model for the first time in a MATLAB session is quicker after starting Simulink.
To start Simulink, use the start_simulink
or simulink
function. For example, when you start MATLAB with the -r
command window option with the intention of
taking action in Simulink, consider starting Simulink using the start_simulink
function.
Action | Syntax |
---|---|
Open Simulink without opening any windows. |
start_simulink |
Open the Simulink start page. |
simulink |
Create Models, Libraries, and Subsystems
To create a new model, library, or subsystem, use the new_system
function. To reuse model content or settings, use templates. Create
custom templates using the Simulink.exportToTemplate
function, then
create new models based on the templates using the Simulink.createFromTemplate
function. To create a protected model, use the
Simulink.ModelReference.protect
(Embedded Coder) function.
The table shows examples of these actions. The commands in the examples return the
handle, h
, of the new model, library, or subsystem. For more syntaxes and
examples, see the function documentation.
Action | Example |
---|---|
Create a model in memory based on the default template. | Create a model named
h = new_system("myModel") |
Create a copy of a model in memory. | Create a model named h = new_system("myModel2",FromFile="myModel1"); |
Create a model in memory based on a subsystem in the currently loaded model. | Create a model named h = new_system("myModel2",Model="myModel1/mySubsystem") |
Create a subsystem in memory that you can reference in multiple models. | Create a subsystem named
h = new_system("mySubsystem","Subsystem") |
Create a library in memory. | Create a library named
h = new_system("myLibrary","Library") |
Create a model in memory based on a custom template. If the
template is not on the MATLAB path, you must specify the fully-qualified path to the template file
and | Create a model named h = new_system("myModel",FromTemplate="myTemplate") Alternatively, use this command. h = Simulink.createFromTemplate("myTemplate",Name="myModel") |
Create a protected model from an existing model. | Create a protected model from a model named h = Simulink.ModelReferenceProtect("myModel") Create
a protected model from a model named h = Simulink.ModelReference.protect("myModel",... Path="C:\Users\user1\Documents\myFolder") |
Load Models, Libraries, and Subsystems
Loading a model, library, or subsystem brings it into memory but does not open it in the Simulink Editor. For information about how to open a model, library, or subsystem, see the next section.
To load a model, library, or subsystem, use the load_system
function. You can also load while getting a block handle using the
getSimulinkBlockHandle
function or while
opening a file using the open_system
function. The table shows
examples of these actions. For more syntaxes and examples, see the function documentation.
Action | Example |
---|---|
Load a model, library, or subsystem into memory. | Load a model named h = load_system("myModel") |
Load a model into memory and get the handle of a block in the model from the block path. | Load the model named h = getSimulinkBlockHandle("myModel/myBlock",true) |
Load and open a model, a library, or a subsystem saved in a subsystem file. | Load and open a model named
open_system("myModel") |
To load multiple models at once using the load_system
,
open_system
, or getSimulinkBlockHandle
function, specify the models as one of the options in this table. Loading multiple models,
libraries, or subsystems using the getSimulinkBlockHandle
function with
the models specified as filenames in a string array is not supported.
How to Specify Multiple Models, Libraries, or Subsystems | Example |
---|---|
Filenames specified as string array (preferred) |
models = ["myModel1";"myModel2";"myModel3"]; h = load_system(models) |
Filenames specified as cell array of character vectors (strings are recommended over character vectors) |
models = {'myModel1';'myModel2';'myModel3'}; h = load_system(models) |
Open Models, Libraries, and Subsystems
To open models, libraries, and subsystems in the Simulink Editor window, use the open_system
or open
function. The table shows examples of these actions. For more syntaxes and examples, see the
function documentation.
Action | Example |
---|---|
Open a model, a library, or a subsystem saved in a subsystem file. | Open a model named
open_system("myModel") |
Open a subsystem in the context of the model that contains the subsystem. The model containing the subsystem must be loaded. | Suppose a model named open_system("myModel/mySubsystem") |
Open a referenced model in the context of a model hierarchy. | Suppose a model named path = Simulink.BlockPath("myModel1/myBlock");
open(path) |
To open multiple models at once using the open_system
function,
specify the models as one of the options in this table.
How to Specify Multiple Models, Libraries, or Subsystems | Example |
---|---|
Filenames specified as string array (preferred) |
models = ["myModel1";"myModel2";"myModel3"]; h = open_system(models) |
Filenames specified as cell array of character vectors (strings are recommended over character vectors) |
models = {'myModel1';'myModel2';'myModel3'}; h = open_system(models) |
Save and Export Models, Libraries, and Subsystems
To save model, library, and subsystem files, use the save_system
function. The table shows examples of these actions. The commands
in the examples return the path of the saved model, library, or subsystem. For more syntaxes
and examples, see the function documentation. For information about how to get the handle of
a model, see Get Handles and Paths. To save and close
models with one command, see the next section.
Note
The syntaxes in the table do not save the base workspace or model workspace. For
information about how to save the model workspace, see the save_system
function documentation. To save workspace variables in the base
workspace, use the save
function.
Action | Example |
---|---|
Save the current top-level model, library, or subsystem. | Open multiple models, and then save the second model you opened. To complete the task, interactively select the second model you opened to make it the current model. Then, enter this command. path = save_system |
Save a new model, library, or subsystem in the current folder. | Save a new model in the current folder with the name
path = save_system("myModel") |
Save a new model, library, or subsystem in a file location other than the current folder. | On the Windows® operating system, save a new model in the folder located at
path = save_system("C:\Users\user1\Documents\myFolder\myModel") On
macOS, save a new model in the folder located at
path = save_system("~/Documents/myFolder/myModel") |
Save an existing model, library, or subsystem. | Save an existing model with the handle
path = save_system(h) |
Save existing model, library, or subsystem in a new or different file. | Save an existing model with the handle path = save_system(h,"myModel2") |
To export models, use the save_system
with the input arguments shown
in the table or use the Simulink.exportToVersion
function. To export
a model as a protected model, use the Simulink.ModelReference.protect
(Embedded Coder) function.
Action | Example |
---|---|
Export to a previous MATLAB version from the past seven years. | Export a model named path = save_system(h,"myExportedModel",ExportToVersion="R2020a") Alternatively, use this command. path = Simulink.exportToVersion("myModel","myExportedModel","R2020a") |
Export to the MDL or SLX file format. | Export a model named path = save_system("myModel.mdl","myModel.slx") |
Export to the XML format. | Export a model with the handle path = save_system(h,ExportToXML=true) |
Export as a protected model (this is the same thing as creating a protected model from an existing model). | Export a model named h = Simulink.ModelReference.protect("myModel") Export
a model named h = Simulink.ModelReference.protect("myModel",... Path="C:\Users\user1\Documents\myFolder") |
To save or export multiple models, libraries, or subsystems at once using the
save_system
function, specify the models, libraries, or subsystems as
one of the options in this table.
How to Specify Multiple Models, Libraries, or Subsystems | Example |
---|---|
Array of handles |
models = [h1;h2;h3]; h = save_system(models) |
Filenames specified as string array |
models = ["myModel1";"myModel2";"myModel3"]; h = save_system(models) |
Filenames specified as cell array of character vectors (strings are recommended over character vectors) |
models = {'myModel1';'myModel2';'myModel3'}; h = save_system(models) |
Close Models, Libraries, and Subsystems
To close models, libraries, and subsystems without saving the changes you made to them,
use the bdclose
function. To save and close models,
libraries, and subsystems, use the close_system
function. The table shows examples of these actions. For more
syntaxes and examples, see the function documentation. For information about how to get the
handle of a model, see Get Handles and Paths.
Action | Example |
---|---|
Close the current system or subsystem. Discard any unsaved changes. | Close the current top-level model without saving the changes you made. bdclose |
Close the specified model, library, or subsystem. Discard any unsaved changes. | Close the model with the handle bdclose(h) |
Close all open models, libraries, and subsystems. Discard any unsaved changes. | Close all open models, libraries, and subsystems without saving the changes you made. bdclose("all") |
Save and then close the specified model, library, or subsystem. | Save an existing model with the handle close_system(h,1) |
Save the specified model, library, or subsystem to a new or different file and then close the model, library, or subsystem. | Save an existing model with the handle close_system(h,"myModel") |
To close multiple models at once using the close_system
function,
specify the models as one of the options in this table.
How to Specify Multiple Models, Libraries, or Subsystems | Example |
---|---|
Array of handles |
models = [h1;h2;h3]; h = close_system(models) |
Filenames specified as string array |
models = ["myModel1";"myModel2";"myModel3"]; h = close_system(models) |
Filenames specified as cell array of character vectors (strings are recommended over character vectors) |
models = {'myModel1';'myModel2';'myModel3'}; h = close_system(models) |
When you close a model with the bdclose
or
close_system
function, the model is no longer loaded. To close a
model but keep the model loaded, use this command instead, where model
is
the handle or name of the
model.
set_param(model,Open="off")
bdclose
or
close_system
function.See Also
new_system
| load_system
| open_system
| save_system
| close_system
| bdclose