Codegen saying variable is constrained to being non-complex when it's defined as complex

Any idea why I'd get this error:
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
When setting up my fields in coder I specifically put the variable as a complex variable. Do I need to explicitly put that in my code as complex? If so, how do I do that? Here is my line of code tripping the error:
yvec1(:,tic_ct) = DelayProfile .* (Fade1.Ix + 1i * Fade1.Qx);
Any ideas?

10 Comments

How did you initialize yvec1 ?
yvec1 = complex(zeros(M,N), zeros(M,N));
Here's how I was initializing it originally:
yvec1 = zeros(Fade1.num_taps,tics);
I attempted your suggestion (which still allows the program to run correctly in MATLAB), however I get a new error from Coder stating
Dimension 1 is fixed on the left-hand side but varies on the right ([1 x 10000] ~= [:? x :?]).
Which I did set it to 1 x 10000 because my workspace tells me it's a 1 x 10000 complex double. So I need a way of setting it to variable dimensions I'm assuming? Or is this a limitation in C and it's why Coder can't process it?
Which line of your MATLAB code is generating that new error?
yvec1 = zeros(Fade1.num_taps,tics);
If it would be easier to have a larger bulk of code, let me know.
It would be helpful to see more of the code, and also the build process you are using. Are you using the GUI to build, command line, or a build script?
Are you no longer making the complex assignment to yvec1, and thus have dropped initializing yvec1 as complex() ? Or are you assigning zeros() to yvec1 after it has already been initialized to complex ?
Walter-
I'm assigning zeros() to yvec1 after it has already been initialized to complex.
Rick-
function [out1,Fade1] = UpdateFade1_1pole(Fade1,tics)
M = 1;
N = 10000;
yvec1 = complex(zeros(M,N),zeros(M,N));
yvec1 = zeros(Fade1.num_taps,tics);
out1 = zeros(Fade1.num_taps,tics);
DelayProfile = sqrt(Fade1.RelativePowerNorm)*Fade1.DelayProfile;
for(tic_ct = 1:tics)
Fade1.Ix = Fade1.zeta * Fade1.Ix + (1-Fade1.zeta) * Fade1.sigma * randn(1,Fade1.num_taps);
Fade1.Qx = Fade1.zeta * Fade1.Qx + (1-Fade1.zeta) * Fade1.sigma * randn(1,Fade1.num_taps);
yvec1(:,tic_ct) = DelayProfile .* (Fade1.Ix + 1i * Fade1.Qx);
end
Why assign the non-complex zeros to a variable that is already complex zeros, especially since you need the variable to store complex values? It seems redundant at best and wrong at worst.
Also, this code gives the impression that out1 is unused ? If it will be a copy of yvec1 after the calculation and if yvec1 is still going to be complex, then out1 should also be initialized as complex.
This doesn't include the ending of the code that shows where out1 is used.
%add doppler shifts (if any)
if (Fade1.UseDoppler)
t_vec_norm = Fade1.t_norm + [1:tics];
Fade1.t_norm = Fade1.t_norm + tics; %update time at end of this update
doppler_tones = exp(2*pi*1i*Fade1.DopplerProfileNorm'*t_vec_norm);
out1 = yvec1 .* doppler_tones;
else
out1 = yvec1;
end
And I agree that it seems wrong. The code works fine and yvec1 shows up in my workspace as complex. But when I used Coder, it says it's non-complex.

Sign in to comment.

 Accepted Answer

MATLAB Coder automatically renames variables. These two lines are actually constraining two separate variables.
yvec1 = complex(zeros(M,N),zeros(M,N));
yvec1 = zeros(Fade1.num_taps,tics);
If you delete the first line and replace the second line with this you should be set.
yvec1 = zeros(Fade1.num_taps,tics) + 0i;
Hope this helps.
Good luck.

3 Comments

Two separate variables? How could the compiler keep track of which one is supposed to be associated with which name?
The documentation for complex() specifically indicates that adding 0i to a real array results in a real array, not a complex array, but that complex() specifying a 0 complex part results in a real array. Is the compiler acting differently than that documentation ?
MATLAB Coder treats "complex" as a type whereas MATLAB treats it as an attribute. After all, in generated C code, there is no natural way to make complex behave like an attribute.

Sign in to comment.

More Answers (1)

Hi erveryone, I need to post this question here, because no one responds to the question i have asked in the past. Sorry for that, but would appreciate if you can answer.
Hello everyone, I am having an issue with the interaction of matlab function block in the simulink with my model block. Below you can also find the picture.
I have created a matlab function with three inputs and 1 output. Since in the matlab function block, i have solved an differential equation, it has complex expressions in the form of for example .
(exp(sqrt(-a/b)))
This generates the output of complex data type from the function block. When passing this to the integrator which is the input to my plant, I cannnot run the simulink due to the error
"Cannot pass the complex value to non-complex location"
I tried chaning the data types of integrator, then the same thing happens to other blocks in the simulink.
I also tried using absolute values, which ofcourse gives real number, but turns out to be very large numbr and genrates high magnitude. I really need help in solving this issue.
Can anyone help me in this/
Thanks in advance.

Products

Community Treasure Hunt

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

Start Hunting!