How to convert a struct into a bus
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
Daniel Luder
2020년 4월 22일
I use the function Simulink.Bus.createObject(struct)
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
Nicolò Binda
2020년 4월 23일
Worked perfectly, Many thanks!
Paul
2020년 4월 24일
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
2020년 4월 24일
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
2020년 4월 24일
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
2020년 4월 24일
@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
2020년 4월 25일
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
2020년 4월 26일
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
2020년 4월 27일
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
2023년 8월 7일
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.
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 String에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
