mustBeReal
Checks that a value has no imaginary part.
Category: Programming > Functions
Syntax
mustBeReal(A)
Checks that A contains only real values. Returns nothing if the check passes and throws an error if not.
Description
mustBeReal is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeReal. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.
A complex array whose imaginary parts are all zero is stored as real by MATLAB rules and therefore passes.
Input arguments
A- The value to check. Any array of the class the validator accepts.
Example
Passing and failing input
mustBeReal([1 2 3]);
disp('passed')
try
mustBeReal(1 + 2i)
catch err
disp(err.message)
end
Output
passed
Value must be real.