Main Content

Best Practices for Using Complex Data in C Charts

R2026b

Complex data is data whose value is a complex number. For example, in a Stateflow® chart in Simulink® model, an input signal with the value 3 + 5i is complex. See Complex Data in Stateflow Charts.

When you use complex data in Stateflow charts that use C as the action language, follow these best practices.

Perform Math Function Operations with a MATLAB Function

Math functions such as sin, cos, min, max, and abs do not work with complex data in C charts. However, you can use a MATLAB® function in your chart to perform math function operations on complex data.

A Simple Example

In the following chart, a MATLAB function calculates the absolute value of a complex number:

Chart that calls MATLAB function myabs with a complex operand, u.

The value of comp_num is 1+2i. Calculating the absolute value gives an answer of 2.2361.

How to Calculate Absolute Value

Suppose that you want to find the absolute value of a complex number. Follow these steps:

  1. Add a MATLAB function to your chart with this signature:

    y = myabs(u)
  2. Double-click the function box to open the editor.

  3. In the editor, enter the code below:

    function y = myabs(u)
    %#codegen
    y = abs(u);

    The function myabs takes a complex input u and returns the absolute value as an output y.

  4. Configure the input argument u to accept complex values.

    1. Open the Model Explorer.

    2. In the Model Hierarchy pane of the Model Explorer, navigate to the MATLAB function myabs.

    3. In the Contents pane of the Model Explorer, right-click the input argument u and select Properties from the context menu.

    4. In the Data properties dialog box, select On in the Complexity field and click OK.

You cannot pass real values to function inputs of complex type. For details, see Rules for Using Complex Data in C Charts.

Perform Complex Division with a MATLAB Function

Division with complex operands is not available as a binary or assignment operation in C charts. However, you can use a MATLAB function in your chart to perform division on complex data.

A Simple Example

In the following chart, a MATLAB function performs division on two complex operands:

Chart that calls MATLAB function mydiv with two complex operands, u1 and u2.

The values of comp_num and comp_den are 1+2i and 3+4i, respectively. Dividing these values gives an answer of 0.44+0.08i.

How to Perform Complex Division

To divide two complex numbers:

  1. Add a MATLAB function to your chart with this function signature:

    y = mydiv(u1, u2)
  2. Double-click the function box to open the editor.

  3. In the editor, enter the code below:

    function y = mydiv(u1, u2)
    %#codegen
    y = u1 / u2;

    The function mydiv takes two complex inputs, u1 and u2, and returns the complex quotient of the two numbers as an output y.

  4. Configure the input and output arguments to accept complex values.

    1. Open the Model Explorer.

    2. In the Model Hierarchy pane of the Model Explorer, navigate to the MATLAB function mydiv.

    3. For each input and output argument, follow these steps:

      1. In the Contents pane of the Model Explorer, right-click the argument and select Properties from the context menu.

      2. In the Data properties dialog box, select On in the Complexity field and click OK.

You cannot pass real values to function inputs of complex type. For details, see Rules for Using Complex Data in C Charts.

Rules for Using Complex Data in C Charts

Complex data is data whose value is a complex number. For example, in a Stateflow chart in Simulink model, an input signal with the value 3 + 5i is complex. See Complex Data in Stateflow Charts.

These rules apply when you use complex data in Stateflow charts that use C as the action language.

C charts do not support complex number notation (a + bi), where a and b are real numbers. Therefore, you cannot use complex number notation in state actions, transition conditions and actions, or any statements in C charts.

To define a complex number, use the complex operator as described in Notation for Complex Data.

Math operations such as sin, cos, min, max, and abs do not work with complex data in C charts. However, you can use MATLAB functions for these operations.

For more information, see Perform Math Function Operations with a MATLAB Function.

If you mix operands for any other math operations in C charts, an error appears when you try to simulate your model.

To mix complex and real operands for division, you can use a MATLAB function as described in Perform Complex Division with a MATLAB Function.

Tip

Another way to mix operands for division is to use the complex, real, and imag operators in C charts.

Suppose that you want to calculate y = x1/x2, where x1 is complex and x2 is real. You can rewrite this calculation as:

y = complex(real(x1)/x2, imag(x1)/x2)

For more information, see Access Real and Imaginary Parts of a Complex Number.

If you define complex data with ml, struct, or boolean base type, an error appears when you try to simulate your model.

When you define the initial value for data that is complex, use only a real value. See Additional Properties for instructions on setting an initial value in the Data properties dialog box.

In the Data properties dialog box, do not enter any values in the Minimum or Maximum field when you define complex data. If you enter a value in either field, an error message appears when you try to simulate your model.

If you assign complex values to real data types, an error appears when you try to simulate your model.

Note

You can assign both real and complex values to complex data types.

This restriction applies to the following types of chart functions:

  • Graphical functions

  • Truth table functions

  • MATLAB functions

  • Simulink functions

If your C chart passes real values to function inputs of complex type, an error appears when you try to simulate your model.

You cannot use complex data as an argument for temporal logic operators, because you cannot define time as a complex number.

See Also

Topics