Main Content

close_system

Close Simulink model window or block dialog box

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.

example

close_system(blockOrSys) closes the specified model or subsystem, or the block dialog box of the specified block. If the model was only loaded, that is, with load_system, this syntax clears the model from memory.

close_system(sys,saveflag) lets you specify whether to save the model with its current name or to close it without saving.

example

close_system(sys,newsys) saves the model to a file with the specified name before closing.

example

close_system(sys,___,Name,Value) specifies additional options using one or more Name,Value pair arguments.

Examples

collapse all

Suppose you have a model named myModel.

Open the model.

open_system('myModel')

Close the model.

close_system

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');

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');

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.

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

collapse all

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'

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.

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.

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)

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.

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.

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.

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 R2006a

expand all