close_system
Close Simulink model window or block dialog box
Syntax
Description
close_system
closes the current system or subsystem. If the current
system is the top-level model and has been modified, close_system
returns an
error.
Use the gcs
function to determine the current
system.
Note
You cannot use close_system
in a block or menu callback to close the
root-level model. Attempting to close the root-level model in a block or menu callback causes
an error and discontinues the callback execution.
close_system(
closes the specified
model or subsystem, or the block dialog box of the specified block. If the model was only
loaded, that is, with blockOrSys
)load_system
, this syntax clears the model from
memory.
close_system(
specifies additional options using one or more sys
,___,Name,Value
)Name,Value
pair
arguments.
Examples
Close Model
Suppose you have a model named myModel
.
Open the model.
open_system('myModel')
Close the model.
close_system
Close Block Dialog Box
Suppose you have a model named myModel
that contains a
Gain block named myGain
.
Open the model.
open_system('myModel')
In the model, double-click the Gain block to open its block dialog box.
Close the block dialog box
close_system('myModel/myGain');
Close Model and Save with New Name
Suppose you have a model named myModel
.
Open the model.
open_system('myModel')
Add a Scope block named myScope
to the model.
add_block('simulink/Sinks/Scope','myModel/myScope');
Close the model and save it with a new name.
close_system('myModel','myNewModel');
Close Model Using Name,Value Options
Suppose you have a model named myModel
.
Open the model.
open_system('myModel')
Close the model. Save the model with the name max
and the
ErrorIfShadowed
parameter set to true
.
close_system('myModel','max','ErrorIfShadowed',true)
This command returns an error because 'max'
is the name of a
MATLAB® function.
Close Top Model and Keep Referenced Models Loaded
Open a model hierarchy, load the referenced model, and then close the top model, but leave the referenced model loaded.
Open the sldemo_mdlref_basic
model and load its referenced model,
sldemo_mdlref_counter
.
openExample('sldemo_mdlref_basic') load_system('sldemo_mdlref_counter')
Check which models are loaded in memory.
loadedModels = Simulink.allBlockDiagrams('model'); modelNames = get_param(loadedModels,'Name')
modelNames = 2×1 cell array {'sldemo_mdlref_counter'} {'sldemo_mdlref_basic' }
Close the sldemo_mdlref_basic
model while keeping the referenced model
loaded.
close_system('sldemo_mdlref_basic',0,'closeReferencedModels',false)
Check what models are loaded in memory.
loadedModels = Simulink.allBlockDiagrams('model'); modelNames = get_param(loadedModels,'Name')
modelNames = 'sldemo_mdlref_counter'
Closing the top model clears it from memory. By default, because they share a window,
using close_system
on the top model would also close the referenced model
and clear it from memory. Setting 'closeReferencedModels'
to
false
keeps the referenced model sldemo_mdlref_counter
loaded.
Input Arguments
blockOrSys
— Block, model, or subsystem to close
character vector | cell array of character vectors | string array | handle | array of handles
Name of model, subsystem, or the block whose dialog box you want to close, specified as a character vector, cell array of character vectors, string array, handle, or array of handles. Do not use a file extension.
Example: "vdp/Mu"
'vdp'
sys
— Name of model to close
character vector | cell array of character vectors | string array | handle | array of handles
Name of model to close, specified as a character vector, cell array of character vectors, string array, handle, or array of handles. Do not use a file extension.
saveflag
— Option to save file with current name
0
| 1
| numeric array
Option to save model using the current file name, specified as 0
to
close without saving or 1
to save and then close. If
sys
is an array, you can supply a single character to apply to all of
the models in the array. Or you can provide a numeric array with values that correspond with
each model in the sys
array.
For information on rules for naming models, see Choose Valid Model File Names.
newsys
— File to save to
character vector | cell array of character vectors | string array
File to save to, specified as a character vector, cell array of character vectors, or string array. You can specify a model name in the current folder or the full path name, with or without an extension.
When you specify a name without an extension, close_system
saves to
the file format specified in your Simulink® preferences. Possible model extensions are .slx
and
.mdl
.
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.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: close_system('mymodel','newmodel','closeReferencedModels',false,'ErrorIfShadowed',true,'OverwriteIfChangedOnDisk',true,'SaveModelWorkspace',true)
closeReferencedModels
— Option to close all referenced models
true
(default) | false
| 'on'
| 'off'
Option to close all referenced models when you close the window for the top model,
specified as the comma-separated pair consisting of
'closeReferencedModels'
and true
,
false
, 'on'
, or 'off'
. Referenced
models that are open in another window are unaffected by this setting. To keep referenced
models loaded, set this argument to false
.
ErrorIfShadowed
— Option to return an error if the new name is already used
false
(default) | true
| 'on'
| 'off'
Option to return an error if the new name is already used on the MATLAB path or in the workspace, specified as the comma-separated pair consisting of
'ErrorIfShadowed'
and true
,
false
, 'on'
, or 'off'
. To receive
this error, you must use the newsys
argument to save the model with a
new name. To learn about shadowed files, see Shadowed Files.
OverwriteIfChangedOnDisk
— Option to overwrite the file on disk
false
(default) | true
| 'on'
| 'off'
Option to overwrite the file on disk when you save the model, even if it has been
modified since the model was loaded, specified as the comma-separated pair consisting of
'OverwriteIfChangedOnDisk'
and true
,
false
, 'on'
, or 'off'
. By default,
if the file changed on disk since the model was loaded, close_system
displays an error to prevent the changes on disk from being overwritten.
You can control whether saving the model displays an error if the file has changed on disk by 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.
SaveModelWorkspace
— Option to save the model workspace
false
(default) | true
| 'on'
| 'off'
Option to save the model workspace when you save the model, specified as the
comma-separated pair consisting of 'SaveModelWorkspace'
and
true
, false
, 'on'
, or
'off'
. The model workspace DataSource
must be a
MAT-file. If the data source is not a MAT-file, saving the model does not save the workspace.
See Specify Source for Data in Model Workspace.
Version History
Introduced before R2006aR2019b: Closing a window for a model hierarchy closes referenced models
Starting in R2019b, when you close a window for a model hierarchy by closing the top model,
all referenced models that are not open in another window are also closed, which clears them
from memory. To keep referenced models loaded in memory, set the
'closeReferencedModels'
argument to false
.
See Also
bdclose
| gcs
| new_system
| open_system
| save_system
| load_system
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)