Discussion:
Dynamic Port Label
(too old to reply)
J A
2009-01-08 16:09:01 UTC
Permalink
I am using an S-function block to create a template type block. I already have the number of outputs set dynamically to the number of elements in a matrix parameter, but I can't seem to find a way to label the output ports with the contents of the parameter in a way that the labels change whenever the user input for the parameter is changed. Is there a way to do this?
Phil Goddard
2009-01-08 22:53:01 UTC
Permalink
Is this an m-code or c-code S-function?
What have you tried so far.

If you've found the properties that need changing, but just aren't seeing any changes you make being reflected in the diagram, then it's probably an update issue.
The signals don't really exist until the model is initialized/updated, so you may have to force the model to do that to see any changes you make to the labels.

Phil.
J A
2009-01-09 14:46:02 UTC
Permalink
It is a c-code S-function. I tried some MATLAB code in the Initialization pane of the Mask Editor, but I get errors from that. Here's the code that I have placed there.

vals = get_param(gcb,'MaskValues');
channel=vals(1);
portStr = {'port_label(''output'',1,'num2str(channel(1)) '))'};
for i=1:length(channel)
portStr= {'port_label(''output'','num2str(i),num2str(channel(i) '));'};
end
set_param(gcb,'MaskDisplay',char(portStr));

Should I be trying to code this into the S-function mdlInitializeSizes or is there a way to do that?
Phil Goddard
2009-01-11 03:11:01 UTC
Permalink
The problems you are having has nothing to do with the type of block being used, it has to do with the way you are creating the mask.

There are several syntax/usage problems with your above code.

1. within the initialization pane you already have access to all variables defined on the mask's Parameters tab, you do not need to use
Post by J A
vals = get_param(gcb,'MaskValues');
The parameters are listed on the left hand side of the Initialization tab for convenience.

2. It's not really clear what you are trying to achieve with
Post by J A
portStr = {'port_label(''output'',1,'num2str(channel(1)) '))'};
for i=1:length(channel)
portStr= {'port_label(''output'','num2str(i),num2str(channel(i) '));'};
end
Nowhere here would port_label get executed.
It's almost as if you are trying to form a string that would then be executed by the eval function.

3.
Post by J A
Post by J A
set_param(gcb,'MaskDisplay',char(portStr));
is not the way to set the mask display, that should be done on the mask's Icon tab.

This is what you need to do:
I'm going to assume that you are creating a mask for a block that has 3 inputs.
On the Initialization tab you want to create the strings that you want to use to label your inports/outports.
So something like the following lines

labels = cell(1,3);
for idx = 1:3
labels{idx} = sprintf('channel %d',idx);
end

Then on the Icon tab you want

port_label('input',1,labels{1})
port_label('input',2,labels{2})
port_label('input',3,labels{3})

Note that you cannot use a loop on the icon tab, you need to write each line explicitly.

You'll need to modify my example slightly to fit the number of inputs that you have, and probably do something similar for outputs.

Phil.
Phil Goddard
2009-01-12 00:02:02 UTC
Permalink
With some reflection I notice that my previous response does not answer your question as it does not allow for the number of ports to be varied dynamically.
The problem with your code it that the MaskDisplay property needs to be one continuous character string, while you have it as elements of a cell array.

If I assume that the user is prompted (on the mask dialog) for the number of inputs, and that is assigned to the variable nports (in your case I think this is called channels) then the following code in the Initialization tab (with nothing in the Icon tab) successfully labels the input ports:

labels = [];
for idx = 1:nports
thisLabel = sprintf('port_label(''input'',%d,''channel %d'')',idx,idx);
labels = [labels thisLabel char(10)];
end
labels(end) = [];
set_param(gcb,'MaskDisplay',labels);

The above creates a character string with only one row.
Some of the charaters in the string are char(10) which is the ascii character for newline.

Phil.
J A
2009-01-12 16:58:02 UTC
Permalink
Thank you for all the help, problem solved.
Krishnakumar M
2017-08-01 15:10:12 UTC
Permalink
Hi
How to add a help command like web('test.html') to simulink mask help?
Thanks
Krishnakumar KM

Loading...