noobuntu
noobuntu

Reputation: 913

codegen error: Conversion to struct from double is not possible

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

Upvotes: 1

Views: 162

Answers (1)

noobuntu
noobuntu

Reputation: 913

Found the solution!

When using coder.load, the .MAT file is imported as an array structure. So, in my code, I was trying to concatenate a structure with an array of doubles.

The solution to this was the way that I was loading the .MAT file:

old way:

h = coder.load('my_file.mat', 'my_file_var1');

new way:

tmp = coder.load('my_file.mat');
h = tmp.my_file_var1;

Then I was able to use h as an array of doubles.

Upvotes: 2

Related Questions