codegen error: Conversion to struct from double is not possible

11 次查看(过去 30 天)
I am trying to convert Matlab legacy code into a C program. I went though the usual flows, but am having a build error that I do not understand:
Nfft = 8;
[~,coh] = size(h); // h = array of 168 elements;
display(coh); // displays 168
if mod(coh,Nfft)~=0,
h1 = [h zeros(1,Nfft-mod(coh,Nfft))];
else
h1 = h;
end
This works as expected in Matlab. But when I run it through codegen (after removing the display), I get an error at the line h1 = [h zeros(1,Nfft-mod(coh,Nfft))]; with the error message:
Conversion to struct from double is not possible.
I realize that in the matlab code, it doesn't go through this part of the code. (since 168%8 == 0).
Any ideas how to fix this?
EDIT: After some investigating, I realize that I am reading in h from a .mat file and this may be the reason. Is data read in from a .mat file considered to be a structure? If this is the case, then maybe I need to convert each element to a double first? Seems kind of hacky..

采纳的回答

Walter Roberson
Walter Roberson 2017-3-8
"Is data read in from a .mat file considered to be a structure?"
If you used
h = load('YourFile.mat')
then h would be a structure, containing one field for each variable that you loaded from the .mat file. If the only thing in that .mat file was a variable named 'h' then you would then address it as h.h
The general method to use is something like
filestruct = load('YourFile.mat'); %load into a structure
h = filestruct.h; %pull out a specific part of the structure.
  1 个评论
ChipMonk
ChipMonk 2017-3-8
Yes this works! I was using:
h = coder.load('my_file.mat', 'my_file_var1');
Changed it to:
tmp = coder.load('my_file.mat');
h = tmp.my_file_var1;
Thank you@!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by