Product Support
1818 - How Can I Use Model Callback Routines to Change the Parameters of My Simulink Model?
Model callback routines are a powerful way of customizing your Simulink model. These routines enable you to execute an M-file (or other MATLAB command) when you perform various actions on your model such as clicking on a block, or starting a simulation. For a complete list of model callbacks you can use in Simulink, see the "Using Callback Functions " section of the Using Simulink documentation.
To set-up model callback routines, you must use the SET_PARAM command.
This Technical Note will review four commonly used callback routines:
1. PreloadFcn
Question:
How can I load variables into the MATLAB workspace automatically when my Simulink model is open?
Answer:
Some variables might be required by parameters in different parts of the Simulink model. For example, if you have a model which contains a Gain block and the gain is specified as K, Simulink looks in the MATLAB base workspace for the variable K to be defined. Using the following technique, K will automatically be defined every time the model is opened.
Assuming that you have defined several variables in an M-file called loadvar.m, such as K, to be used in a model called modelname.mdl, you can use the PreLoadFcn callback to execute the M-file loadvar.m. For example, type the following at MATLAB command prompt:
set_param('modelname','PreloadFcn', 'loadvar')
Now save the model. Every time, you open this model, the loadvar function will execute. When you type
whosat the MATLAB prompt after opening the modelname.mdl, you will see the variables from the loadvar.m declared in the MATLAB workspace.
If you want to call your model from an m-file without physically opening your model, you will need to first use the LOAD_SYSTEM function for the PreLoadFcn to be executed:
load_system('modelname')
2. OpenFcn
Question:
How can I execute an M-file by double-clicking on a Simulink block?
Answer:
M-files can perform many different tasks such as defining variables for a block, making a call to MATLAB which brings up a plot of simulated data, or generating a GUI. The OpenFcn callback routine allows you to perform these tasks and others when a Simulink block is opened. This callback overrides the normal behavior which occurs when opening a block (it's parameter dialog box is displayed or a sub-system is opened). The following is an example on how to execute an M-file when double-clicking on a block:
Click on the block that you want to add this property to. From the MATLAB command prompt type,
set_param(gcb,'OpenFcn','expression')
where expression is a valid MATLAB command or an M-file that exists in your MATLAB search path. The test_opnfcn example shows how you can execute an M-file called change_gain.m with the use of the OpenFcn command when double clicking on a subsystem. The command used to set up this callback routine is as follows:
set_param('test_opnfcn/Change_Gain','OpenFcn','change_gain')
Try the following:
- Copy the following files available at the URL above to your working directory:
change_gain.m setgain.m test_opnfcn.mdl - Open the model test_opnfcn.
- Double-click on the change_gain subsystem. This should open a figure window.
- Change the value of the edit text box as desired to update the value of the Gain block.
- Run the Simulation.
3. CloseFcn
Question:
How can I execute a series of commands when a block diagram is closed?
Answer:
You will notice that when closing the model mentioned in step 2 (test_opnfcn) the figure window is also closed. You can make your block perform actions like this by using the CloseFcn callback routine:
set_param('test_opnfcn','CloseFcn','close all')
The command close all is executed before the block diagram is closed.
4. StartFcn
Question:
How can I change my model or block parameters before my simulation starts?
Answer:
As an example, how could you make all of the Scope blocks that exist in a Simulink model come to the forefront when opening the model? This type of task can be executed by the StartFcn callback routine:
To bring up the scopes, create a simple M-file named openscopes.m and save it on your MATLAB search path. For example,
% openscopes.m % Brings scopes to forefront at beginning of simulation.
blocks = find_system(bdroot,'BlockType','Scope');
% Finds all of the scope blocks on the top level of your % model to find scopes in subsystems, give the subsystem % names. Type help find_system for more on this command.
for i = 1:length(blocks) set_param(blocks{i},'Open','on') end
% Loops through all of the scope blocks and brings them % to the forefront
After you have created this M-file, set the StartFcn for the model. For example,
set_param('modelname','StartFcn','openscopes')
Now, every time you run the model, all of the Scope blocks should open automatically and be in the forefront.
Conclusion
These are just a few examples of using Model Callbacks to modify and enhance your Simulink model. The above examples are by no means the limit of what you can do with these features since M-files can be programmed to perform a wide variety of actions. Below is a list of all the model callback parameters available in Simulink. For a full description on how to use them, see the "Using Callback Functions " section of the Using Simulink documentation.
Model Callback Parameters:
CloseFcn PostLoadFcn PostSaveFcn PreLoadFcn PreSaveFcn StartFcn StopFcn CloseFcn CopyFcn DeleteFcn LoadFcn ModelCloseFcn NameChangeFcn OpenFcn ParentCloseFcn PreSaveFcn PostSaveFcn
Store