MATLAB Answers

How to convert a struct into a bus

104 views (last 30 days)
Hi
I need to create a bus for simulink, and to avoid to do it manually i generated a struct usingi different for cycles
Now i have a struct that conains simple values and other struct.
es=
car.id=1
car.engine.pistons.compression_ring.maintenance_time
car.engine.pistons.compression_ring.maintenance_cost
car.engine.pistons.oil_control_ring
car.engine.bearings
car.body.door
ecc...
Now i need to trasform this struct into a bus object but i really can't find how
I find a lot of solution on matlab like:
struct2bus and others like Simulink.Bus.cellToObject(busCell) without any luck
or also this one:
thanks

  0 Comments

Sign in to comment.

Accepted Answer

Daniel Luder
Daniel Luder on 22 Apr 2020
For your problem use the following lines:
% Create Model Struct
car.id = 1;
car.engine.pistons.compression_ring.maintenance_time = 2;
car.engine.pistons.compression_ring.maintenance_cost = 3;
car.engine.pistons.oil_control_ring = 4;
car.engine.bearings = 5;
car.body.door = 6;
% Create Estimation Data Bus for Simulink Model
car_bus_info = Simulink.Bus.createObject(car);
car_bus = evalin('base', car_bus_info.busName);
In Simulink use a constant block and define the constant value as car. Under Signal Attributes --> Output data dype define Bus: car_bus.
I hope that solves your problem

  9 Comments

Show 6 older comments
Nicolò Binda
Nicolò Binda on 25 Apr 2020
Hi Daniel, thanks for your help!
i can't share my real model because is part of a work with a company and i can't condivide any data, but i created a small model that explain well my problem:
the main script generate the struct and the bus (it's not needed because i already saved both in the two mat file)
the Car_bus.mat define the bus
the Car_parts_value.mat is the struct that contain data
In the model i generate entity that are "bus" entity of the same type o the car_bus.mat
In the model i need to assign to each entity generated the value present in the car_parts_value.mat
Thanks!
Nicolò
Daniel Luder
Daniel Luder on 26 Apr 2020
Hi Nicolò
As far as I understand, the Car_bus object only defines the entity structure without any data. In the example attached under Event actions I randomly set the car.id to 1 or 2. The output switch then sends the entity via the corresponding port.
Best,
Daniel
Nicolò Binda
Nicolò Binda on 27 Apr 2020
Hi
Yes the problem is that the bus does not contain any data, i need to copy all the data contained in the struct into the bus as soon as the entity is generated
i partially solved this problem implementing the code i used to generate the struct with data into "event actions" changing it a little bit to adapt to simulink, so i need to declare the variable in the matlab workspace and the code load the different values into the bus, but is a little bit confusing in my view, this is why i'm still searching a way to copy all the struct value into the bus
thank for your help Daniel
Nicolò

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!