Using Simulink Previous page   Next Page

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:

  1. You can also declare a complex number with the function complex, as shown in the following example:

  1. Generally, this can result in a complex result or a warning that the function takes only arguments producing real results. For example, for negative arguments, the function sqrt warns that only positive arguments are allowed.

  1. In the following example, the variable x is declared complex and stays complex:

    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:

  1. For example, the following code produces the complex result 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:

    Another exception is a function call that takes real arguments but produces complex results, as shown in the following example:


Previous page  Declaring Local Variables Implicitly Functions in Embedded MATLAB Functions Next page

© 1994-2005 The MathWorks, Inc.