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 (e.g. 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, e.g., a single time stamp and extrapolate these variables as constants?It would be best to import
input
as a multidimensional struct, e.g.,1x5 struct with fields: 'a', 'b', ...
, but if it's only possible to have a single dimension, i.e.1x1 struct
, it's okay.
CodePudding user response:
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.