简体   繁体   中英

Simulink: can I import 1 x n struct from workspace?

I define a struct (say, input ) in the base workspace, and to avoid a lot of refactoring, I want to import it "From Workspace" to Simulink. I will use it in the Matlab Function block. From what I found (eg here ), I need to specify the bus object in the base workspace.

Attempt 1: ( from here )

elm(1) = Simulink.BusElement;
elm(2) = Simulink.BusElement;
elm(1).Name = 'a';
elm(2).Name = 'b';
busObj = Simulink.Bus;
busObj.Elements = elm;
input.a = 1.0;
input.b = 2.0;

Now, in From Workspace block, I specify Data: input , Output Data Type: Bus: busObj . Probably I'm missing something because I get the following error:

Invalid structure-format variable specified as workspace input in 'test/From Workspace'. If the input signal is a bus signal, the variable must be a structure of MATLAB timeseries objects. Otherwise, the variable must include 'time' and 'signals' fields, and the 'signals' field must be a structure with a 'values' field.

Attempt 2:

I try to create such a structure of timeseries objects with Simulink.SimulationData.createStructOfTimeseries('busObj') (while busObj is defined in workspace), but Matlab complains:

To construct a MATLAB structure from a bus object, the first argument must be the name of a bus object. The optional second argument can be either a cell array of MATLAB timeseries objects, or a structure of timeseries objects.

Notes:

  • input is a struct of only static parameters, and it's pointless to treat it as a time series. Maybe it's possible to "hack" it by defining, eg, a single time stamp and extrapolate these variables as constants?

  • It would be best to import input as a multidimensional struct, eg, 1x5 struct with fields: 'a', 'b', ... , but if it's only possible to have a single dimension, ie 1x1 struct , it's okay.

If only I had found out this thread earlier...

Since all elements are constant, it's possible to utilize a Constant Block and name the variable from the workspace as the "imported" input variable. Next, in Signal Attributes Output Data Type: Bus: slBus1 , and everything works fine.

The busObj can be created manually, but this approach gave me a Simulink compilation error: "some elements in the structure are not correctly aligned with the busObj " (I don't know why, everything looked fine). But thankfully this worked:

Simulink.Bus.createObject(input);

which created bunch of auxiliary busObj s for sub-structures and slBus1 top-level bus object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM