MATLAB: How to create a global variable so that all the masked subsystems can see it

modelsimulink

How do I create a global variable so that all my masked subsystems can see it?

Best Answer

There are a couple of ways to create a global variable so that all of your masked subsystems can see it:
1. Set the value of your variable in the Model Initialization callback function of your model. This will place the variable in the base workspace. You can then access this variable throughout your model simply by entering in the variable name.
2. Create a global workspace. To do this, use the GLOBAL function in the Initialization Page of the Mask Editor. For example, if you have a variable 'x' which should be global, use the command 'global x'.
3. Use the EVALIN command. For example, if you have a variable 'k' you would like to be global in a Gain block in your masked subsystem, enter the following command into the Parameters box of the Gain block:
evalin('base','k')
The Gain block will then use the Base workspace, or MATLAB's workspace. Of course, please be sure to define 'k' in the MATLAB workspace.