How to add a parameter value to the icon of an S-function block
이전 댓글 표시
I'm new to S-function blocks in Simulink but I've made a working S-function block and now I want to display a value on its icon.
Right now, my Icon & Ports script looks like this:

However instead of the text 'MKF' being static, I want it to display a string from my first parameter:
disp(block.DialogPrm(1).Data.label)
Obviously this doesn't work. This is how you access parameters from within the S-function m-file.
How can I get the same variable from my S-function parameter to the icon script?
I looked in the Parameters & Dialog menu tab and it looks like this:

Does one of these contain my parameter?
채택된 답변
추가 답변 (1개)
Benjamin Thompson
2022년 2월 10일
0 개 추천
In the Icon and Ports tab, set Run initialization to on, then anything you define in the mask workspace in the Initialization tab can be used in your drawing and display commands in Icon and Ports. See the MATLAB documentation "Mask Editor Overview" for more information. In the Initialization tab you can use the get_param commands to get parameter values.
Alternatively, use Block Annotations. Right click on a block and go to properties. Any parameters you created in your mask will also be available there for display, but it will be above or below the block rather than inside.
댓글 수: 9
Bill Tubbs
2022년 2월 10일
편집: Bill Tubbs
2022년 2월 10일
Benjamin Thompson
2022년 2월 10일
Here is an example. So if you created to mask parameters p0 and p1 whose values are set by edit boxes, then add a disp statement in the icon drawing box like:
disp(['p0 is ' get_param(gcb, 'p0')]);
Then you get "p0 is 1" on your block. If this is not what you are looking for let us know. The mask controls are generally not type checking data that the user types into the edit boxes on the mask. I think your goal was to just copy the information from the mask to the block display. In the Initialization tab you can also add data type conversion stuff, data validation stuff if you don't like what was entered, etc. I added simple stuff there in my example.
With the block in question selected in Simulink, you can also test these get_param calls in the MATLAB workspace:
>> get_param(gcb, 'p0')
ans =
'1'
Bill Tubbs
2022년 2월 10일
편집: Bill Tubbs
2022년 2월 10일
Benjamin Thompson
2022년 2월 11일
You should be able to simplify your procedure for getting the value of a parameter as I have shown here and in my example. Are you setting the Name field of the parameter in the mask editor, and setting the Run initialization option in the Icon and Ports tab to On. If so, get_param should return the value of that parameter as a string, which you can then use directly in a call to disp in the Icon and Ports tab. If you are having problems post your example.
Bill Tubbs
2022년 2월 11일
Benjamin Thompson
2022년 2월 11일
Yes I have built and masked many S-Functions. I now understand you are accessing the comma separated list of symbols in the Parameters edit box of the S-Function block properties. The more common approach to mask design and mask drawing is to use mask variables, and those mask variables are set by the edit boxes, check boxes, buttons, sliders, etc. that the mask designer puts into the mask. Or they are defined in the Initialization tab as internal mask workspace variables whose values are defined in some way from other mask variables.
Then, you can pass those mask variables to your S-Function in the parameters list.
So, why not use a base workspace variable? There are many cases where the base workspace is not available to your block: the model using the block may be configured to use Model Workspace only, or data dictionary only, as its extenal data source. The S-Function may be inside a masked subsystem itself.
Another consideration is the possibility of using your S-Function block more than once. What if you wanted three instances of your block and wanted to associate them with MKF1, MKF2, and MKF3 parameters in the base workspace. So you give the block a mask accepting one parameter, create three copies of that block in your model, and then change the mask parameter for each block to MKF1, MKF2, and MKF3 respectively. In the mask workspace it is always the same variable name and you reference this in your icon drawing commands as well as in the parameter list passed to the S-Function code.
Bill Tubbs
2022년 2월 12일
Bill Tubbs
2022년 2월 13일
편집: Bill Tubbs
2022년 2월 13일
Bill Tubbs
2022년 2월 13일
카테고리
도움말 센터 및 File Exchange에서 Model, Block, and Port Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






