Generate a variable bus from within a mask in Simulink

16 views (last 30 days)
I have a masked subsystem in Simulink, containing a Matlab-function. The Matlab-function generates a structure as an output, based on an input provided in the mask. I would like to have a bus at the output of my subsystem which contains the elements of that structure.
The way I see it, I have to define the bus information to which the bus should adhere before Simulink knows how to deal with it. But I need to do this within the mask, as only the mask has the information to generate said information.
I tried to implement Simulink.Bus.createObject in the mask Initialization, which works, but I cannot find a way of defining a name of the bus in that function. As such, Simulink defaults to it's naming scheme slBus1, slBus2 ... As such, I can label my bus to be of the type slBus1 (if I pass it through the mask). But that only works if there was no slBus1 a priori and each time I evaluate the mask, my workspace gets cluttered with new buses. Furthermore, I cannot access the information written to the base workspace to change e.g. the name (which I can detect), because it is not accessible to the masked Workspace.
I also tried fully defining the bus within the mask Initialization using Simulink.Bus and Simulink.BusElement, but then the bus only exists within the masked Workspace and the bus information should be on the base Workspace for Simulink to use it.
If there would be a way of defining a name for the Simulink.Bus.createObject, that would solve my issues. But any other solution will do.

Accepted Answer

TAB on 28 Sep 2018
You need to use evalin or assign in functions to create required variable in base worksspace from Mask workspace.
For example, in Mask workspace use something like this.
% An example structure to be converted to bus
myStruct.a = 10;
myStruct.b = 20;
% Create bus object in base worksapce with default name and collect its info
busInfo = Simulink.Bus.createObject(myStruct);
% Copy the created object with name you want and delete default object
evalin('base', ['myBus=' busInfo.busName ';clear ' busInfo.busName]);
  1 Comment
Jeroen Stuyts
Jeroen Stuyts on 28 Sep 2018
I knew I was missing a small step somewhere. Thank you! That did the trick.

Sign in to comment.

More Answers (0)


Find more on Composite Interfaces in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!