MATLAB coder - how can I add fields to an existing struct?

6 views (last 30 days)
Hi, I want to be able to add a field to an existing struct. Here is a simplified example:
function [Prm] = Testangles(input)
Prm.NumAngles = input;
Prm.Angles = linspace(0,360,Prm.NumAngles);
When I try to generate code, I get the error message: "??? This structure does not have a field 'Angles'; new fields cannot be added when structure has been read or used."
What would be the best way to deal with this problem?
Thanks! Sheida

Accepted Answer

SK
SK on 18 Oct 2014
There are many restictions in converting Matlab to C via coder. Coder emits C code on the fly as it parses the .m file. It looks like it assumes that the struct has been fully defined once any of its fields have been used. It would then emit the corresponding C code that defines the C-struct. Since, in C, the whole structure has to be defined in one place, no further changes to the struct are possible.
You will need to get the results beforehand in temporary variables and then put them in the struct.
function [Prm] = Testangles(input)
numangles = input;
angles = linspace(0, 360, numangles);
Prm.NumAngles = numangles;
Prm.Angles = angles;

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!