Create array of buses / subbuses within MATLAB programmatically and dynamically (without eval)

Dear all,
have the following problem: I am reading data from a text file. I have written a function which creates a struct from this data. This struct might contain substructs.
In the next step, I am creating buses from the structs. Here, I am struggling with creating the subbuses / array of buses from the substructs if I want to name them dynamically. The following code should provide an example of my problem:
% create data
t = 1:100;
x1 = ones(1,1,length(t));
x2 = x1*2;
x3 = x1*3;
s.x2 = x2;
s.x3 = x3;
% create main bus
BusElements(1) = Simulink.BusElement;
BusElements(1).Name = 'x1';
BusElements(1).DataType = class(x1);
BusElements(2) = Simulink.BusElement;
BusElements(2).Name = 's';
BusElements(2).DataType = 'Bus: sBus';
DataBus = Simulink.Bus;
DataBus.Elements = BusElements;
% create subbus
BusElements(1) = Simulink.BusElement;
BusElements(1).Name = 'x2';
BusElements(1).DataType = class(x2);
BusElements(2) = Simulink.BusElement;
BusElements(2).Name = 'x3';
BusElements(1).DataType = class(x3);
sBus = Simulink.Bus; % how to avoid naming this bus variable manually?
sBus.Elements = BusElements;
% create time series
varTimeSeries.x1 = timeseries(x1,t,'name','x1');
varTimeSeries.s.x2 = timeseries(s.x2,t,'name','x2');
varTimeSeries.s.x3 = timeseries(s.x3,t,'name','x3');
Help is greatly appreciated!
Best
Ludwig

1 Kommentar

Since I am still wondering how to avoid this issue, I would be grateful for any help!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Raspberry Pi Hardware finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 16 Sep. 2022

Verschoben:

am 4 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by