How to label a signal using a sub-system mask parameter in Simulink?

조회 수: 24 (최근 30일)
Hello!
I am working in Simulink and I have a masked subsystem with a string mask parameter. Within the subsystem I want to label a signal based in this parameter. How can I go about doing this? Thank you in advance.

채택된 답변

Abhishek Kumar Singh
Abhishek Kumar Singh 2024년 9월 6일
편집: Abhishek Kumar Singh 2024년 9월 6일
Hi @Kevin,
I found a method to dynamically rename signal labels using mask parameters in Simulink:
Firstly retrieve Mask Parametes:
sigLabel = get_param('your_model/Subsystem', 'myParam');
Assuming myParam is the parameter name.
Then get all the port handles from a certain block/subsystem inside of your subsystem mask:
PortHandles = get_param('your_model/Subsystem/block1', 'PortHandles');
Use getfullname to identify the index of output port you want to rename. Then rename the signal:
set_param(PortHandles.Outport(1), 'Name', sigLabel);
Replace 1 with the index of outport of your choice. This approach allows you to update the signal name based on a mask parameter, enhancing flexibility in your model. Adjust 'your_model/Subsystem', 'block1' and 'myParam' to fit your specific model, block and parameter names.
You can further add this code to PostLoadFcn or InitFcn callback to automate this. Refer to the doc here: Model Callbacks - MATLAB & Simulink (mathworks.com)
Hope this helps.
  댓글 수: 3
Abhishek Kumar Singh
Abhishek Kumar Singh 2024년 9월 6일
Hi @Kevin,
It is working but you are not able to see the results as you are getting port handle of the subsystem and no signal is emanating from there. Try connecting a Terminator and it works:
If you want to rename the signal inside of the subsystem, you will need to get corresponding port handle:
portHandles = get_param('test_signallabeling/mainSub/Add', 'PortHandles')
Reg. your query related to port handles, indeed they are double- as expected. To get the name, use getfullname function:
getfullname(portHandles.Outport)
Kevin
Kevin 2024년 9월 6일
This worked, thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul
Paul 2024년 9월 6일
Hi Kevin,
I was able to change a signal name in the masked subsystem using code in the mask initialization code. In my simple example, I have a masked subsystem with a dialog parameter called sname.
The first line of code gets the handles to the ports of the Gain block. The second line changes the name of the signal output from the Gain block to the string value in the mask dialog for the sname parameter.
ph = get_param([gcb,'/Gain'],'PortHandles');
set_param(ph.Outport,'Name',sname)
I think you should be able to adopt this to your model.

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by