| Using Simulink | ![]() |
Declaring Local Complex Variables Implicitly
Using complex variables in Embedded MATLAB functions differs, in part, from the way in which complex variables are used in MATLAB. In MATLAB, the identity of a variable as complex is an attribute of the variable. In Embedded MATLAB functions, the identity of a variable as complex is part of its assigned type. Use the following rules to specify and use complex variables in Embedded MATLAB functions:
sqrt warns that only positive arguments are allowed.
x is declared complex and stays complex:
x = 1 + 2i; %x is declared a complex variable y = int16(x); %real and imaginary parts of y are int16 x = 3; %x now has the value 3 + 0i
Conflicts can occur from operations with real operands that can have complex results. For example, the following code is flagged as an error:
The following is a possible workaround that you can use if you know that a variable can be assigned a complex number:
z:
In MATLAB, this code generates the real result z = 0. However, in Embedded MATLAB, when code for z = x + y is generated, the types for x and y are known, but their values are not. Because either or both operands in this expression are complex, z is declared a complex variable requiring storage for both a real and an imaginary part. This means that z has the complex result 4 + 0i in Embedded MATLAB, not 4 as in MATLAB.
An exception to the preceding rule is a function call that takes complex arguments but produces real results, as shown in the following examples:
y = real(x); %y is the real part of the complex number x. y = imag(x); %y is the real-valued imaginary part of x. y = isreal(x); %y is false (0) for a complex number x.
Another exception is a function call that takes real arguments but produces complex results, as shown in the following example:
| Declaring Local Variables Implicitly | Functions in Embedded MATLAB Functions | ![]() |
© 1994-2005 The MathWorks, Inc.