How to convert a struct into a bus

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

Nicolò Binda
Nicolò Binda el 23 de Abr. de 2020
Worked perfectly, Many thanks!
Paul
Paul el 24 de Abr. de 2020
This answer was very helpful.
Any idea why:
a) Simulink.Bus.createObject poofs a variable into the base workspace instead of returning it as an output alongside car_bus_info?
b) If it has to poof a variable into a workspace, why not into the workspace of the caller instead of the base? That way the call to Simulink.Bus.createObject could be called from within a function with no need clear the created bus object from the base worksapce after assigning it to car_bus.
a) I assume it is because the bus objects from the base workspace are linked to the Simulink model. You can find the corresponding bus objects in the Bus Editor as shown below.
b) I think because of a). As workaround I clear all variables of type Simulink.Bus from my base workspace before I call Simulink.Bus.createObject. Below you will find an example implemented as a function.
car_bus = createSimulinkBus(car);
function my_bus = createSimulinkBus(my_struct)
% Clear all Variables from base Workspace of Class Type 'Simulink.Bus'
S = evalin('base', 'whos');
workspace_vars_names = {S.name};
idx_bus_vars = strcmp({S.class}, 'Simulink.Bus');
if max(idx_bus_vars)
evalin('base',['clear' sprintf(' %s', workspace_vars_names{idx_bus_vars})]);
end
% Create Bus Object for Simulink Model
my_bus_info = Simulink.Bus.createObject(my_struct);
my_bus = evalin('base', my_bus_info.busName);
% Clear the automatic generated 'slBus1' Bus Object
vars = {'slBus1'};
evalin('base',['clear' sprintf(' %s',vars{:})]);
end
Nicolò Binda
Nicolò Binda el 24 de Abr. de 2020
Daniel i'd like to ask you one more things: when i generate a entity in simulink and i choose "bus" as "Entity" type and i assign the bus i created as "Entity type name", there is a way (a block or so) to automatically import all the numbers contained in the struct?
for example ai have:
car.door.pieces=50;
when i generate the bus the value 50 is lost, so in simulink i need to reasign it
many thanks
Nicolò
Paul
Paul el 24 de Abr. de 2020
I wrote myself a function very similar to Daniel's to create a bus object from a struct. The fact that such a function even needs to be written that poofs and clears variables in the base workspace suggests there is something suboptimal with how this all works. Note that the output of Simulink.Bus.createObject, which in this case is my_bus_info, isn't even needed (at least not for simulation execution) after the bus object, my_bus, is created. Insofar that Simulink.Bus.createObject is already creating the bus object that's really needed, it seems to me the bus object should really be the primary output of Simulink.Bus.createObject.
Also, the last two lines of the function could be made a little simpler and more general with:
evalin('base',['clear ' my_bus_info.busName]);
Thanks for the discussion. At least now I have a path forward, even if it is bit kludgy IMHO.
Daniel Luder
Daniel Luder el 24 de Abr. de 2020
@Nicolò: Unfortunately, I do not really have experience with discrete event simulation in Simulink. Can you share your code / model?
@Paul: I agree with you, this workaround should not be necessary. I may have misinterpreted something in the documentation, and there is an easier, more straightforward way to do this.
Nicolò Binda
Nicolò Binda el 25 de Abr. de 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 el 26 de Abr. de 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 el 27 de Abr. de 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ò
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

Productos

Versión

R2020a

Preguntada:

el 22 de Abr. de 2020

Editada:

el 9 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by