How can I declear a struct array in m file when generating C++ codes through Matlab Coder?
2 views (last 30 days)
Show older comments
Matlab Coder reports an error when generating C++ codes from matlab codes:
??? Conversion to struct from double is not possible.
Error in ==> structtest Line: 5 Column: 14
I guess data type absence leads to this error.But hoa can I declear a as an empty struct array?
Notes :
function below is just an example. in my application, I don't know how long 'a' will be.
the struct member 'image' is an uint8 matrix with non fixed size.
my Matlab version is 2012a.
matlab codes is as below:
function a = structtest()
a = [];
for i = 1 : 10
b.image = 1;
a = [a(:); b];
end
end
Thanks!
0 Comments
Answers (2)
Mike Hosea
on 12 Jul 2013
I would use repmat. Initially MATLAB Coder did not support empty struct arrays. Unfortunately, I don't remember when that restriction was lifted. This works in 13a
function a = tstruct
% coder.varsize('a',[inf,1],[true,false]);
b = struct('image',1); % Define the structure type.
a = repmat(b,0,1);
for i = 1:10
b.image = 1;
a = [a;b];
end
The varsize line is commented out because it isn't needed. However, if you have an upper bound in mind on how long the array can be, you can substitute it for the inf there.
0 Comments
See Also
Categories
Find more on Structures 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!