MATLAB Answers

Coder global structures with strings

5 views (last 30 days)
Gertjan Hofman
Gertjan Hofman on 1 Sep 2021
Commented: Gertjan Hofman on 2 Sep 2021
I am trying to port functional code that uses global structures with string elements in Matlab coder (code generation). I am unclear whether this is actually supported. Strings are, globals are, data structures are but combined ? In the end I am trying to generate a DLL that can be used by LabView.
A typical section of code is this:
function Output = CallMeFromLabView ( Mode)
global Data ; % this is a structure
Data.x =5;
Data.PathName = "/myfiles/subdir/here.dat"
if (mode == 1)
Data.PathName = "/trythesefolders/hello.txt"
return Output;
Ofcourse other functions will be called from here that use the global Data.
Coder returns with "unsupported global data initialization class for global variable Data ...." the first occurance of assigning the "PathName" string.
Is this even possible ? I know strings can be made variable length but it is unclear whether this works for globals. Does anyone have any example I could follow ?
Much appreciated

Answers (1)

Adit Calambur
Adit Calambur on 2 Sep 2021
Hi Gertjan,
If I understand you correctly, you are trying to generate deployable code for a project that has global structures with strings.
To generate code for MATLAB code that contains global variables, you must define and initialize global data in:
  • the MATLAB global workspace,
  • in a MATLAB Coder project,
  • or at the command line.
Global variables containing strings are supported. Global structure variables cannot contain handle objects or sparse arrays.
If you do not initialize global data in the project or at the command line, MATLAB Coder looks for the variable in the MATLAB global workspace. If the variable does not exist, MATLAB Coder generates an error.
For more information see Generate Code for Global Data.
~ Adit
  1 Comment
Gertjan Hofman
Gertjan Hofman on 2 Sep 2021
Adit - thanks, but this isn't very clear- you just quoted a help page I had already read. I think what is required is definition of the structure outside of the function (say in the test calling code). But an example would really be helpful (if it exist, please point me to one).

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by