New fields cannot be added when structure has been read or used, MATLAB coder.

37 次查看(过去 30 天)
Hi,
I am trying to convert my matlab project into C++ using MATLAB Coder. I have a Class and in the constructor, I am initializing all the values of the struct that I have declared in the properties. The code goes something like this.
classdef ZCNET
properties
ZCNET_cfg;
z_own_turbo_param_2bit_base;
trellis;
end
methods
function self = ZCNET()
self.ZCNET_cfg.AP_ANT_NUM = 1;
self.ZCNET_cfg.NODE_ANT_NUM = 1;
self.ZCNET_cfg.useLTETurbo = 1;
self.ZCNET_cfg.USE_SIMIC = 1;
self.ZCNET_cfg.ANT_NUM = self.ZCNET_cfg.AP_ANT_NUM;
if 1
load_perm = load ('ZCNET_permutations.mat'); % NOTE: to get load_perm.ZCNET_permutations. load_perm.ZCNET_permutations{100} has a random permutation of 100 numbers
else
for h=1201:1500
load_perm.ZCNET_permutations{h} = randperm(h);
end
load_perm.ZCNET_permutations_rev = cell(1,length(load_perm.ZCNET_permutations));
.
.
.
.
.
Since I cannot convert a class directly, I have this function that create an object of the class and them I am using the object to display some valies. This is a test code in order to fix this issue. The function looks something like this.
function call_zcnet()
zc1 = ZCNET;
x = zc1.ZCNET_cfg;
disp(x.bandwidth);
disp(x.permutations{1,5});
end
I am passing "call_zcnet.m" to the coder but when it is giving quiet a few errors. I can run the code by callign 'call_zcnet' from the command window without any problem but coder is giving me following run-time issues.
Any help is appreciated.
Thank you.
  2 个评论
Denis Gurchenkov
Denis Gurchenkov 2021-6-15
Hi Raghav, the screenshot you created does not contain the actual text of the errors.
Maybe you can just attach a zip file with all the files that one needs in order to reproduce the error? It'll be easier for someone to offer suggestions if the issue can be reproduced.
Raghav Rathi
Raghav Rathi 2021-6-15
Hi @Denis Gurchenkov, Thanks for getting back to me. I have created a simpler version of the code that I am attaching in a zip file. I am trying to initialize a struct in this class but according to the defination, MATLAB does not allow to define structs and other data structures. I also refered this post (https://www.mathworks.com/matlabcentral/answers/223848-how-to-use-structures-as-properties-of-a-matlab-class#answer_182706) I can do something like this, but the struct I am having is more than 100 fields which contains other structs and cell in it.
It would be great if you could help me with this problem.

请先登录,再进行评论。

回答(1 个)

Darshan Ramakant Bhat
If you want to make this class codegen compatible, you will have to make the field as a struct type. Below is a sample modified code ;
classdef ZCNET
properties
ZCNET_cfg = struct('AP_ANT_NUM',1,'NODE_ANT_NUM',3,'useLTETurbo',1,'USE_SIMIC',1);
end
methods
function self = ZCNET() %constructor
self.ZCNET_cfg.AP_ANT_NUM = 1;
self.ZCNET_cfg.NODE_ANT_NUM = 3;
self.ZCNET_cfg.useLTETurbo = 1;
self.ZCNET_cfg.USE_SIMIC = 1;
end
end
end
Hope this will be helpful.
  1 个评论
Raghav Rathi
Raghav Rathi 2021-6-16
Hi @Darshan Ramakant Bhat, Thanks for your suggestion but, like I mentioned here https://www.mathworks.com/matlabcentral/answers/857060-new-fields-cannot-be-added-when-structure-has-been-read-or-used-matlab-coder#comment_1585065 I have already tried it and it works, but then the struct (ZCNET_cfg) I am using is having contains more than 100 fields and some of them are cell arrays. I tried to define empty cell array like this,
ZCNET_cfg = struct('ZC_cell_array', cell(1,8))
But this does not work. That is the reason why, I am trying to define the fields in the constructor.
Is the only way to define a structure in a class is what you mentioned? If so, then I will have to structure my project which will be a pain. Thank you for the help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by