How can I select data from a bus element returned by the "In Bus Element" block programmatically?

I have a Simulink model in MATLAB R2021b that I would like to access values of programmatically. I am using the "In Bus Element" block and would like to select one of its outputs to use in a script, but the output data of the block seems to only be for the first element of the bus.
How can I access and keep track of the different outputs of the block?

 Accepted Answer

In order to access the different elements of the output of an "In Bus Element" block, you need to log the data of the signal coming from the outport of the block. You can do this programmatically by setting the signal's "DataLogging" parameter to "on". See the following example code, where "cb" is the block handle of the "In Bus Element" block and the bus is loaded into the workspace:
p = get_param(cb,'PortHandles');
set_param(p.Outport(1),'DataLogging','on');
out = sim('fileName.slx');
When the simulation is run, the model will return a Simulation Output object to the MATLAB workspace under the name "out". The name and structure of "out" can be altered in the Configuration Parameters of the model or by changing the name of the output variable for "sim". The values of the elements of the bus can then be accessed through dot indexing of the Simulation Output. For example, the following code will access a structure containing a field for each bus element:
elements = out.logsout{1}.Values;
If you wanted the data for a specific bus element named "a", you would dot index further using "a" as the field name:
data = out.logsout{1}.Values.a;
You can learn more about signal logging at the documentation link below:

More Answers (0)

Categories

Products

Release

R2021b

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!