0

I am new to matlab and am trying to compile legacy matlab code into C. I come across the following error when doing so:

??? The left-hand side has been constrained to be non-complex, but the right-hand side is complex. To correct this problem, make the right-hand side real using the function REAL, or change the initial assignment to the left-hand side variable to be a complex value using the COMPLEX function.

The code that it complains on is in the comments of the code below:

function [z_out,ovf_flag,ovf_cnt] = fxpt_sgn_saturate(z_in,Nb_out)
Nb_out=(Nb_out<=0)+(Nb_out>0)*Nb_out;
max_val =  2^(Nb_out-1)-1;
min_val = -2^(Nb_out-1);
ovf_cnt = 0;
tmp_ind = find(real(z_in) > max_val);
z_in(tmp_ind) = max_val+1j*imag(z_in(tmp_ind)); // ERROR OCCURS HERE
ovf_cnt = ovf_cnt + numel(tmp_ind);
tmp_ind = find(real(z_in) < min_val);
z_in(tmp_ind) = min_val+1j*imag(z_in(tmp_ind));
ovf_cnt = ovf_cnt + numel(tmp_ind);
tmp_ind = find(imag(z_in) > max_val);
z_in(tmp_ind) = real(z_in(tmp_ind))+1j*max_val;
ovf_cnt = ovf_cnt + numel(tmp_ind);
tmp_ind = find(imag(z_in) < min_val);
z_in(tmp_ind) = real(z_in(tmp_ind))+1j*min_val;
ovf_cnt = ovf_cnt + numel(tmp_ind);
z_out = z_in;
ovf_flag = ~(ovf_cnt==0);
return

I don't particularly understand the code well. Any ideas how to fix this issue?

Thanks

noobuntu
  • 903
  • 1
  • 18
  • 42

1 Answers1

4

When generating the code, you can use the -args flag to specify the size, class, and complexity of your input arguments. You can explicitly cast z_in as a complex number to ensure that the code generation succeeds.

codegen fxpt_sgn_saturate -args {complex(z_in), double(Nb_out)}

Alternately, if you're calling this function from within many other functions and are only calling codegen on the top-level function, then you'll want to explicitly cast the input as a complex datatype within your code prior to calling the function so that codegen can appropriately determine the complexity of the input.

function a(thing)
    fxpt_sgn_saturate(complex(thing), 10);
end
Suever
  • 64,497
  • 14
  • 82
  • 101