2

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
asked Oct 1, 2014 at 13:50
5
  • Could you show your model? Commented Oct 1, 2014 at 13:54
  • I am not allowed to publish the model, sorry. But it's mainly a Subsystem with an input which accepts a "FOO" bus type (whose elements are well defined in a Simulink.Bus object). I don't want to manually enter the elements of the bus (in the Bus Creator dialog) in order to create a "FOO" typed bus. Commented Oct 1, 2014 at 14:07
  • 2
    I think you will find it difficult to get a useful answer then I'm afraid. I'm sure you could create an mcve with dummy data/objects. Commented Oct 1, 2014 at 14:15
  • @Manex you should post your answer as an separate answer and title it with R2007a Commented Oct 2, 2014 at 7:33
  • However I am forced to accept @MohsenNosratinia answer because it provided me the know-how to come up with the definite solution :-) Commented Oct 2, 2014 at 9:13

2 Answers 2

3

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.

answered Oct 1, 2014 at 15:02
Sign up to request clarification or add additional context in comments.

1 Comment

That would not work in R2007a as arrayfun does not accept BusElements, however that set the basis for the solution (see question), Thanks!
0

Modifying @MohsenNosratinia's solution in order to work in Matlab R2007a:

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
answered Oct 2, 2014 at 9:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.