![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824408/image.png)
Initialise Simulink variables for one subsystem from a mask of another subsystem
8 views (last 30 days)
Show older comments
I would like to have a 'Control panel' which has no connections. At the moment. I have created an empty subsystem and then created a mask for this subsystem, and added a Popup box with 3 variables, lets say, ['a' 'b' 'c']. Outside of this subsystem, ie, on the same level, I would like to be able to obtain the chosen variable and use as an input, ie a constant.
At the moment, I have named X my parameter in the mask. Then in the Initialization I have:
global const; const = X;
I then have a constant, where the value is set to const, which is connected to a display, outside of this subsystem.
However this does not work. Does anyone have any suggestions to how I can make this work?
This is a very simplified model, please don't suggest that I should use go to tags or data stores or something and replace this with the constant. This will not work.
Thanks in advance for any suggestions.
0 Comments
Answers (1)
Rajanya
on 28 Jan 2025 at 12:53
After ensuring that the parameter field(popup/edit etc.) is configured correctly in the mask, the 'assignin' function can be used in the model initialization callback to register a variable in the base workspace with the value of the entered mask parameter. The Constant block can then successfully take its value from the workspace variable (independent of the subsystem scope) and display it in the Display block.
In this way, changing the parameter values from the subsystem mask will be reflected directly in value of the Constant block which is on the same level as the subsystem itself.
Below is a demo example:
The mask of 'mySubsys' (empty subsystem) here, has been configured to take an edit field value with parameter name 'constValue'.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824408/image.png)
To register 'constValue' to the base workspace at initialization, 'assignin' function is used in the Init Model Callback and the external base variable storing the value of 'constValue' is named 'externalConstValue', which is also then used as Value in the Constant block.
externalConstValue = get_param('untitled/mySubsys', 'constValue');
externalConstValue = str2double(externalConstValue);
assignin('base', 'externalConstValue', externalConstValue);
The mask values are taken in as 'char' and since that is not accepted as a valid value to the Constant block, it has been converted to double using 'str2double'. The 'assignin' function then linked 'externalConstValue' correctly with 'constValue' and the Display block displays the same.
A similar approach can also be followed if the mask parameter type is 'pop-up' or anything else.
To know more about 'assignin' and its usage, you can refer to the documentation of 'assignin' by executing the following command from the MATLAB Command Window:
doc assignin
Thanks.
0 Comments
See Also
Categories
Find more on Subsystems in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!