Main Content

mustBeInteger

Validate that value is integer

Description

example

mustBeInteger(value) throws an error if value does not contain integer values. This function tests for numbers with no fractional part. The data type of the values is not considered. mustBeInteger does not return a value.

mustBeInteger calls these functions to determine if value is an integer:

Class support: All numeric classes, logical, and MATLAB® classes that overload the functions called by mustBeInteger.

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

Validate that a calculation results in integer values.

A = randi(9)/randi(9);
mustBeInteger(A)
Error using mustBeInteger (line 14)
Values must be integer.

If the result of dividing the two random integers contains a fractional part, mustBeInteger issues an error.

This class restricts the value of the property to be only integers.

classdef MyClass
   properties
      Prop1 {mustBeInteger}
   end
end

Create an object and assign a value to its property. The result of dividing two random integers can result in an integer or a number with a fractional part.

obj = MyClass;
obj.Prop1 = randi(9)/randi(9);
Error setting 'Prop1' property of 'MyClass' class:
Value must be integer.

When you assign a value to the property, MATLAB calls mustBeInteger with the value being assigned to the property. mustBeInteger issues an error when the value resulting from the division of these particular random integers does not result in an integer.

This function restricts the input argument to a value that has not fractional part.

function r = mbInteger(A,idx)
    arguments
        A (1,:) {mustBeNumeric}
        idx (1,1) {mustBeInteger}
    end
    r = A(idx).^2;
end

Calling this function with a noninteger value results in an error being thrown by mustBeInteger. The result of dividing two random integers can result in an integer or a number with a fractional part.

idx = randi(9)/randi(9);
A = rand(1,10);
r = mbInteger(A,idx);
Error using mbInteger
Invalid input argument at position 2. Value must be integer.

Input Arguments

collapse all

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

Other data types cause an error.

Tips

  • mustBeInteger 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