Naming of bus elements using a cell-array of names

조회 수: 6(최근 30일)
Thomas Kurz
Thomas Kurz 2016년 2월 23일
답변: Thomas Kurz 2016년 5월 9일
I want to access the vector output of an S-function as a bus. For this, I'm using a Demux to give labels to each element and then a BusCreator to create the bus. The question now is, how can I assign the channel names in the Demux outputs with commands using a list of names? Or is there a better way altogether?
There is a property of the Demux block called 'OutputSignalNames', which are the current names, but that is read-only.


Shivam Chaturvedi
Shivam Chaturvedi 2016년 3월 1일
편집: Shivam Chaturvedi 2016년 3월 1일
Hi Thomas,
Instead of using OutputSignalNames, you can use the PortHandles parameter, and get the handles to the individual signals and assign the Name property to each of the signals individually.
here's an example:
% assuming you had the handle of the demux block in a variable called 'demuxhandle'
hPorts = get_param(demuxhandle, 'PortHandles');
outputPorts = hPorts{1}.Outport;
% assuming you had just 2 outports
signalnames = {'a', 'b'};
firstPort = outputPorts(1);
set_param(firstPort , 'Name', signalnames{1})
secondPort = outputPorts(1);
set_param(secondPort , 'Name', signalnames{2})
Hope this helps!

Thomas Kurz
Thomas Kurz 2016년 5월 9일
I found a solution to my problem, which I want to share here. This is a full example, which creates a simulink model with the necessary elements, which are then adjusted to our needs
%%Create a bus object in the workspace and save it
signalNamesTmp1 = {'SignalName1','SignalName2','SignalName3'};
signalNamesTmp2 = [signalNamesTmp1; num2cell(ones(size(signalNamesTmp1)))];
busTmp = Simulink.Bus.createObject(struct(signalNamesTmp2{:}));
myBusDef = eval(busTmp.busName);
% Save this into a .mat file, which you will need to load every time you
% want run the model
% save('myBusDef.mat', 'myBusDef');
%%Create the Simulink model
open_system('myModel'); % Open model visibly
set_param('myModel','Lock','off'); % Unlock it
% Create the elements we need here
add_block('built-in/Demux','myModel/myDemux', 'DisplayOption','bar','Position',[150 35 155 130]);
add_block('built-in/BusCreator','myModel/myBusCreator', 'Position',[285 35 290 130])
%%Update the Simulink model
% Set the number of signals
set_param('myModel/myDemux', 'Outputs', num2str(numel(signalNamesTmp1)));
set_param('myModel/myBusCreator', 'Inputs', num2str(numel(signalNamesTmp1)));
% Get handles to ports which shall be connected
inPorts = get_param('myModel/myBusCreator', 'PortHandles');
outPorts = get_param('myModel/myDemux', 'PortHandles');
for i = 1:numel(outPorts) % Assign names to outputs of myDemux
set(outPorts.Outport(i), 'SignalName', signalNamesTmp1{i});
% Specify to use the previously saved bus definition
set_param('myModel/myBusCreator', 'OutDataTypeStr', 'Bus: myBusDef');
% Connect the demux and bus creator blocks and assign the names
for i=1:length(signalNamesTmp1)
add_line('myModel', outPorts.Outport(i), inPorts.Inport(i));
set_param(outPorts.Outport(i), 'Name', signalNamesTmp1{i});


Community Treasure Hunt

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

Start Hunting!

Translated by