MATLAB Function block containing a locally defined "struct" outputs an error that the struct is not defined

1 view (last 30 days)
I have a MATLAB Function block (MLFB) that contains the below script:
simParameters = []; % Clear the simParameters variable
simParameters.NumFramesSim = 30; % Simulation time in terms of number of 10 ms frames
simParameters.NumUEs = 4;
simParameters.UEDistance = [100; 400; 1500; 1000];
The code is simply creating a "struct" "simParameters" that has a few "double" elements. The above script would execute just fine in a regular MATLAB script. But, in the MLFB, when I run the model, I get the below error message:
Error:Attempt to extract field 'NumFramesSim' from 'double'.
Function 'Function-Call Subsystem/MATLAB Function' (#24.156.169), line 8, column 1:
"simParameters"
Launch diagnostic report.
Error:Attempt to extract field 'NumUEs' from 'double'.
Why does this script execute nominally in a regular MATLAB script but throws the above errors in MLFB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2020
In a MATLAB Function Block, compiled code from MATLAB is being "generated" to be included in Simulink. So, to define and declare a "struct", just like "simParameters", you would need to follow a different workflow and syntax than the one in the example code you are using.
In the below link, there is an overview of how structures should be defined in MATLAB Function Blocks:https://www.mathworks.com/help/simulink/ug/create-structures-in-matlab-function-blocks.html
For example, if you are only defining "simParameters" to be a local struct and not an output of your block, maybe you can follow the recommendation listed in the below link:https://www.mathworks.com/help/simulink/ug/defining-scalar-structures-for-code-generation.html

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!