Validate that value is real or issue error





mustBeReal(A) issues an error if A is not a real number. This function does not return a value.

mustBeReal accepts user-defined objects if the class of the object implements this method:

This function ignores input arguments that are empty values. No error is thrown if the property value is empty.


collapse all

Use mustBeReal to validate that the input is a real number.

Validate that a calculation results in real numbers.

x = -2:2;
z = x.^exp(-x.^2);
Error using mustBeReal (line 13)
values must be real.

The value z is complex.

This class constrains the value of Prop1 to real values.

classdef MyClass
      Prop1 {mustBeReal}

Create an object and assign a value to its property.

x = -2:0.1:2;
obj = MyClass;
obj.Prop1 = x.^exp(-x.^2);
Error setting 'Prop1' property of 'MyClass' class:
values must be real.

When you assign a value to the property, MATLAB® calls mustBeReal with the value being assigned to the property. mustBeReal issues an error because the value assigned to Prop1 is complex.

Input Arguments

collapse all

Value to validate, specified as a scalar or array of any one of the following:

Other data types cause an error.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2017a