1

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

4
  • Is h an array of structs? Commented Mar 8, 2017 at 19:16
  • @Suever It is an array of doubles Commented Mar 8, 2017 at 19:22
  • Does explicitly changing h to double(h) resolve the issue? Commented Mar 8, 2017 at 19:32
  • I tried something like: h1 = [double(h) zeros(1,Nfft-mod(coh,Nfft))]; which doesn't work. Is that what you meant? Commented Mar 8, 2017 at 19:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.