MATLAB: MATLAB coder – how can I add fields to an existing struct

matlab coderstructures

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

Best Answer

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;