Why does the initialization function in a Simulink subsystem block use an integer rather than the corresponding text string?

1 回表示 (過去 30 日間)
There is an example Simulink file attached. I created a parameter on the "Parameter & Dialog" tab called Parameter1. Parameter1 is a popup and has 3 possible values Option 1, Option 2, and Option 3 as shown below.
&nbsp
If I query Parameter1 from the command line get_param(gcb, 'Parameter1'), I find the value to be one of the three options listed (see picture). However, if I use Parameter1 as a variable in the Initialization tab, it acts like integer (see 2nd picture below). I understand that Simulink is really using Enumeration. Is there a way I can use/invoke this Enumeration in the initialization of my subsystem?
&nbsp

採用された回答

Jason Nicholson
Jason Nicholson 2018 年 3 月 21 日
編集済み: Jason Nicholson 2018 年 3 月 21 日
To use the text string coming from the popup options, turn off the "evaluate" checkbox as shown below. This allows comparison of text in the option in the initialization. There is code snippet below
if strcmpi(Parameter1, 'option 1')
% Do this
else
% Do something else
end
The above code snippet only works if "Evaluate" checkbox is unchecked. Otherwise in the initialization, Parameter1 is an integer corresponding to which option was selected in the popup of Parameter1. For instance with the "Evaluate" checkbox checked, if the first option in the popup was selected, Parameter1 is 1 and if the second option was selected, then Parameter1 is 2.

その他の回答 (1 件)

Ankit Bhatnagar
Ankit Bhatnagar 2017 年 2 月 9 日
I understand that you are trying to utilize the mask parameters to initialize your subsystem.
1. As you used the get_param for 'Parameter1', you can also use set_param to assign it a value.
2. You can enter any valid MATLAB expression, consisting of MATLAB functions and scripts, operators, and variables defined in the mask workspace. However, Initialization commands run in the mask workspace, not the base workspace.
3. Additionally, there is a check box on the same window:
'Allow library block to modify its contents'
This check box is enabled only if the masked subsystem resides in a library. Checking this option allows the block's initialization code to modify the contents of the masked subsystem by adding or deleting blocks and setting the parameters of those blocks. Otherwise, an error is generated when a masked library block tries to modify its contents in any way.
Refer the following documentation link to read more about the Initialization tab and check out a simple tutorial as well.
You can also use the 'Initfcn' callback of a particular block itself for initialization purposes.
  1 件のコメント
Jason Nicholson
Jason Nicholson 2018 年 3 月 21 日
編集済み: Jason Nicholson 2018 年 3 月 21 日
Thanks for the response.
I appreciate the response but it is too naive for what I am asking. I am very deep into building models as a "blockset author." I am having a hard time find resources that really get into the fine details of how best to maintain large models and write clean, maintainable large Simulink models. My initialization mask are often 800+ lines of MATLAB code. This integer corresponding to an option is hard to keep track of when you have lots of options. My answer below makes for much cleaner code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSubsystems についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by