Main Content

mustBeFinite

Validate that value is finite

Description

example

mustBeFinite(value) throws an error if value is not finite. A numeric value is finite if it is not NaN or Inf. This function does not return a value.

mustBeFinite calls the following function to determine if the input is finite:

Class support: All numeric classes, logical, char, and MATLAB® classes that overload isfinite.

This function ignores input arguments that are empty values. Therefore, no error is thrown when the property or function argument value is empty.

Examples

collapse all

Use mustBeFinite to validate that no array elements are NaN or Inf.

d = 0:9;
A = 1./d;
mustBeFinite(A)
Error using mustBeFinite (line 13)
Value must be finite.

The division by d resulted in one element becoming Inf, which causes an error.

This class restricts the value of Prop1 to finite values.

classdef MyClass
   properties
      Prop1 {mustBeFinite}
   end
end

Create an object and assign a value to its property.

d = 0:9;
obj = MyClass;
obj.Prop1 = 1./d;
Error setting property 'Prop1' of class 'MyClass':
Value must be finite.

When you assign a value to the property, MATLAB calls mustBeFinite with the value being assigned to the property. mustBeFinite issues an error because the result of division by 0 is Inf.

This function declares an input argument that must be a vector of doubles that contain no Inf or NaN elements.

function s = mbFinite(x)
    arguments
        x (1,:) double {mustBeFinite}
    end
    n = length(x);
    m = sum(x)/n;
    s = sqrt(sum((x-m).^2/n));
end

Calling the function with an input that does not meet the requirement of mustBeFinite results in an error.

values = [12.7, 45.4, 98.9, Inf, 53.1];
s = mbFinite(values);
Error using mbFinite
Invalid input argument at position 1. Value must be finite.

Input Arguments

collapse all

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

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

Tips

  • mustBeFinite is designed to be used for property and function argument validation.

Extended Capabilities

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

Introduced in R2017a