Main Content

close_system

Close Simulink system 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

Open the model vdp and then close it.

vdp
close_system

Open the model vdp.

vdp

In the model, double-click the Mu block to open its block dialog box.

Close the block dialog box

close_system('vdp/Mu');

Open a model, modify it, save the model with a new name and close it.

Open the model vdp and add a block to it.

vdp
block = add_block('vdp/Mu','vdp/Mu','MakeNameUnique','on');

Close the model, saving it with a new name.

close_system('vdp','myvdp');

This command tries to save the vdp system to a file with the name 'max', but returns an error because 'max' is the name of an existing MATLAB® function.

close_system('vdp','max','ErrorIfShadowed',true)

Open a model hierarchy, load the referenced model, then close the top model, leaving the referenced model loaded.

Open the sldemo_mdlref_basic model and load its referenced model, sldemo_mdlref_counter.

open_system('sldemo_mdlref_basic')
load_system('sldemo_mdlref_counter')

Check what 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 sldemo_mdlref_basic 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 Model 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 Model Names.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

Compatibility Considerations

expand all

Behavior changed in R2019b

Introduced before R2006a