MATLAB: Codegen error: Conversion to struct from double is not possible

codegenconversion errordoubleMATLABstruct

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..

Best Answer

"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.
Related Question