Search code examples
matlabsimulink

Forcing bus creator to only accept signals from a bus object


This is Matlab/Simulink R2007a.

I have created a bus named "FOOBUS" in the bus editor, which contains three elements, say "FOO1,FOO2,FOO3".

The input port of a given subsystem is forced to accept -only- "FOO" type buses. This subsystem is saved in a library, along with a MAT file where the "FOO" Simulink.Bus object is defined.

Now, when it comes to integrating the subsystem with other blocks:

-How can I force a Bus Creator to show me the elements that comprise FOO? The thing is, that when building the FOO bus object (normally composing it using a Bus Creator configured to output a FOO bus object) I can't see the signals that should comprise the FOO bus object, and have to dive in the subsystem (or in the bus editor) to see which elements it has, and hand-write them at the Bus Creator dialog.

-Any other proposal? The aim is to have clear interface definitions that can be invoked quite simply, and not re-writing them by hand in Bus Creator blocks.

Thank you.

Added

Just to clarify, what I am mainly seeking is to create the Subsystem input bus without having to manually add the items (as the Bus Creator suggests AFAIK).

Answer for R2007a @MohsenNosratinia provided the basis for the answer (the original answer would not work in R2007A as looks like arrayfun does not accept BusElements. I used the plain array approach as a workaround.

function addSignalsToBusCreator(busDef)
busEls = busDef.Elements;
sigString = ' ';
for i = 1 : length(busEls)
    sigString = [sigString busDef.Elements(i).Name ','];
end
set_param(gcb, 'Inputs', sigString(1:end-1));
end

Solution

  • You need to do that programmatically. Simulink does not offer a way to accomplish this in GUI. You can create a function like this:

    function addSignalsToBusCreator(busDef)
    elemNames = arrayfun(@(x) x.Name, busDef.Elements, 'uni', 0);
    sigString = sprintf('''%s'',', elemNames{:});
    set_param(gcb, 'Inputs', sigString(1:end-1));
    end
    

    And after adding the bus creator to the model, select it and run this function with the bus definition

    >> addSignalsToBusCreator(FOO)
    

    The whole trick is in the 'Inputs' parameter of the bus creator block. It can take two dfferent type of values. If it is a string containg a number say 5, it will interpret it as if you have chosen 'Inherit bus signal names from input signals' option with 5 inputs. However, if it contains a string with comma-separated single-quoted names it interprets it as if you have chosen 'Require input signal names to match signals below'. In your example the string will be 'FOO1','FOO2','FOO3'.

    I have tested this in R2011b.