Documentation |
Loading a model brings it into memory but does not display it graphically.
Tip To both bring a model into memory and display it graphically, open the model as described in Open a Model. |
After you load a model (as distinct from opening it), you can work with the model programmatically as if it were visible. However, you cannot use the Simulink^{®} Editor to edit the model unless you open the model.
You cannot load a model using the Simulink Editor or Library Browser.
To load a model programmatically, at the MATLAB^{®} command prompt, enter the load_system command. Specify the model to be loaded. For example, to load the vdp model:
load_system('vdp')
You can use model callbacks to load variables and perform other actions when a model is loaded. For details, see Callbacks for Customized Model Behavior.
You can use the PreloadFcn callback to automatically preload variables into the MATLAB workspace when you open a model.
Parameters in different parts of the Simulink model might require some variables. For example, if you have a model that contains a Gain block and the gain is specified as K, Simulink looks for the variable K to be defined. You can automatically define K every time the model loads.
You can define variables, such as K, in a MATLAB script. You can use the PreLoadFcn callback to execute the MATLAB script.
To create model callbacks interactively, in the Simulink Editor, select File > Model Properties > Model Properties and use the Callbacks tab to edit callbacks.
To create a callback programmatically, at the MATLAB command prompt, enter the following :
set_param('mymodel','PreloadFcn', 'expression')
where expression is a valid MATLAB command or a MATLAB script that exists in your MATLAB search path.
For example, if your model is called modelname.slx and your variables are defined in a MATLAB script called loadvar.m, you would type the following:
set_param('modelname','PreloadFcn', 'loadvar')
Now save the model. Every time you subsequently open this model, the loadvar function executes. You can see the variables from the loadvar.m declared in the MATLAB workspace.
For example, if you have a model that contains a Gain block with a gain specified as K, Simulink looks in the MATLAB base workspace for the variable K to be defined. You can use the PreLoadFcn callback, to automatically define K every time you open the model.
Assuming that you have several variables (such as K) predefined in a MATLAB file called loadvar.m, to be used in a model called modelname.slx, you can use the PreLoadFcn callback to execute the MATLAB file loadvar.m. For example, type the following at MATLAB command prompt:
set_param('modelname','PreLoadFcn', 'loadvar')
After saving the model, every subsequent time you open this model, the loadvar function executes. After opening modelname.slx, type:
whos
The variables from the loadvar.m file are in the MATLAB workspace.