coder.mustBeComplex - Validate that value lies on the complex plane - MATLAB (original) (raw)

Main Content

Validate that value lies on the complex plane

Since R2023b

Syntax

Description

coder.mustBeComplex([value](#mw%5F67952a0f-8365-4094-9203-371195f2f8ea)) validates that the function input value can have a nonzero imaginary part. In MATLABĀ® execution, this function does not throw an assertion because any numeric input can have a nonzero imaginary part. In code generation, this validator specifies at compile time that value has a complex type.

example

Examples

collapse all

This example shows how to specify that an entry-point input has complex type for code generation using the coder.mustBeComplex validator.

Define entry-point function multiplyByThree that accepts a complex single-precision scalar input.

function out = multiplyByThree(in) arguments in (1,1) single {coder.mustBeComplex} end out = in*3; end

Generate a static library for multiplyByThree using the codegen command. Because the arguments block fully specifies the type, size, and complexity of the input argument in, you do not need to use the -args option.

codegen -config:lib -c multiplyByThree -report

Code generation successful: View report

Open the code generation report. Observe that the input argumentin has a complex type.

Screenshot of code generation report showing that the argument in has complex type.

This example shows how to validate that an input to a non-entry-point function has a complex type.

Define the entry-point function testComplex that calls the local function local. The function local accepts one input that is declared to be complex using the coder.mustBeComplex validator.

function y = testComplex(x) y = local(x); end

function v = local(u) arguments u {coder.mustBeComplex} end v = u + sqrt(u); end

Attempt to generate code for the testComplex function. Specify the input to be a scalar double using the -args option. Code generation fails because the code generator treats the input totestComplex as real by default. You pass this real type directly tolocal, thereby triggering an error from thecoder.mustBeComplex validator.

codegen testComplex -args 0

Value must be complex.

To fix this issue, convert the input x totestComplex to a complex type before passing it tolocal using the complex function.

function y = testComplex(x) y = local(complex(x)); end

function v = local(u) arguments u {coder.mustBeComplex} end v = u + sqrt(u); end

Run the same codegen command again.

codegen testComplex -args 0

Code generation successful.

Input Arguments

collapse all

Value to validate, specified as a numeric scalar or array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Extended Capabilities

Version History

Introduced in R2023b