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

24 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Abhishek Kumar Singh
Abhishek Kumar Singh am 6 Sep. 2024
Bearbeitet: Abhishek Kumar Singh am 6 Sep. 2024
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 Kommentare
Abhishek Kumar Singh
Abhishek Kumar Singh am 6 Sep. 2024
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 am 6 Sep. 2024
This worked, thank you for your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Paul
Paul am 6 Sep. 2024
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.

Kategorien

Mehr zu Event Functions finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by