How to convert a struct into a bus

306 visualizaciones (últimos 30 días)
Nicolò Binda
Nicolò Binda el 22 de Abr. de 2020
Editada: Christian el 9 de En. de 2024
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

Respuesta aceptada

Daniel Luder
Daniel Luder el 22 de Abr. de 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
  11 comentarios
Martin Ryba
Martin Ryba el 7 de Ag. de 2023
Hi Nicolo, the way you can populate it is by using a Constant block to feed your structure into a port with that bus definition (at least that worked for me). I put similar code to define and populate the struct, and invoke the Simulink.Bus.createObject into a initialization script that is the PostLoadFcn callback of the top level model.
Christian
Christian el 9 de En. de 2024
Editada: Christian el 9 de En. de 2024
Great solution, thank you.
You have to be careful, it does not work if the struct contains char. Matlab fails to provide a proper error message.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Composite Interfaces en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by