How to utilize a MATLAB structure array inside a Stateflow chart?

10 次查看(过去 30 天)

How do I utilize the data stored in a MATLAB structure array (i.e. "struct") inside a Stateflow chart? I have a MATLAB struct that defined in a separate script or a Simulink callback function. The MATLAB struct contains several fields including an array and I want to access the array elements from within the struct using the dot (.) operator in a Stateflow chart. 

采纳的回答

MathWorks Support Team
The example below shows one way to access data from a MATLAB struct inside a Stateflow chart:
1) Defining the MATLAB Struct: Define a MATLAB Struct “y”, similar to “fe_adapt_data”
y.a = 1; y.b = 2; y.c = [1,2,3];
2) Defining the Bus Object: Define a bus object “myBus” with a datatype similar to the MATLAB Struct “y”
myBus = Simulink.Bus; elems(1) = Simulink.BusElement; elems(2) = Simulink.BusElement; elems(3) = Simulink.BusElement; elems(1).Name = “a”; elems(2).Name = “b”; elems(3).Name = “c”; elems(3).Dimensions = [1 3];   %Since element 3 of the structure is an array myBus.Elements = elems;
3) Defining the State Flow Chart 
        Do not pass "y" as an input to the chart (see figures below).
       
       
3) Initializing the Parameters:
  • Open Property Inspector
  • Open Symbols Pane
  • Click on “y” in the Symbols Pane
  • In the Property Inspector, set the “Type” for y to “Bus: myBus” from the drop-down menu.
  • In the Symbols Pane, set “y” as “Parameter Data” by clicking on the box left of “y”.
Please find an example Simulink ("exampleSimulinkModel") model attached with this article that implements the above-mentioned steps. Please check the “initFcn” callback for the struct definition and bus object definition.  

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by