Embedded MATLAB function: Call global struct; Error: Referencing a component from array of non-scalar values is not supported for code generation.

5 views (last 30 days)
Hello Everybody!
I'm trying to use a global struct variable defined via a Simulink.Bus + Simulink.Signal + MATLAB Value Initialisation struct (all fields = 0) in a Simulink Modell. I've successfully implemented the global struct variable, however as soon as I try to work with it in a Simulink-embedded MATLAB function I get the following Coder Error for every single code line where I want to access any field of my global struct:
"Referencing a component from array of non-scalar values is not supported for code generation."
My struct has the following field architecture:
Init_FMS.FM.Fly_BM = false;
Init_FMS.FM.Fly_FPL = false;
Init_FMS.FM.Fly_Manual = false;
Init_FMS.BM.Psi = single(0);
Init_FMS.BM.H = single(0);
Init_FMS.BM.H_dot = single(0);
Init_FMS.BM.IAS = single(0);
Init_FMS.FPL.GEN.Name = uint8(zeros(1,20));
Init_FMS.FPL.GEN.Num_of_WPTs = uint16(0);
Init_FMS.FPL.WPT.ID = uint16(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.LAT = double(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.LON = double(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.ALT = single(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.VEL = single(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.TRT = uint8(zeros( maxNumWPTs,1));
Init_FMS.FPL.WPT.Loiter_ID = uint16(zeros(maxNumWPTs,1));
Init_FMS.FPL.WPT.Loiter_Time = int16(zeros( maxNumWPTs,1));
Within the embedded function I call it just like a normal global variable before trying to write new data to the different fields:
function = Receive_FMS_ProcessInternData (...)
global FMS
if ...
if ...
FMS.FM.Fly_BM = true;
...
end
and so on.
What am I doing wrong (code/ variable settings?) to avoid getting the coder error all the time?
Thank you!! :)

Answers (2)

Suhan
Suhan on 23 May 2017
Hi,
The error "Referencing a component from array of non-scalar values is not supported for code generation" typically comes up with Structure Arrays when dot notation is used to access the elements. Structures are supported for code generation, so that should not be an issue: http://www.mathworks.com/help/simulink/ug/matlab-language-features-supported-for-code-generation.html. Also, see if specifying the data type of the bus structure of the inputs and outputs to the MATLAB function block in the Model Explorer fixes the issue
  1 Comment
Franck Yin
Franck Yin on 29 May 2017
Edited: Franck Yin on 29 May 2017
What worked for me : I forgot that my element was an array. After adding the index -"element(1).subelement" instead of "element.subelement" in my case- the problem was gone.

Sign in to comment.


Andreas Kustermann
Andreas Kustermann on 24 May 2017
Solved the problem, thank you. In the model explorer, for the respective settings of the Simulink.Signal its dimensions need to be set to "1" and "auto"; during my first setup I had specified the exact dimension size (= row number) of my global struct which was apparently wrong.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!