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

6 次查看(过去 30 天)
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

采纳的回答

SK
SK 2014-10-18
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by