Main Content

mustBeNonzero

Validate that value is nonzero

Description

example

mustBeNonzero(value) throws an error if value is zero. This function does not return a value.

mustBeNonzero calls these functions to determine if the input is not zero:

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

Examples

collapse all

Use mustBeNonzero to validate that the input does not contain values that are zero.

A is an array of numbers that is the result of a calculation:

A = sin([-1,0,1]);
mustBeNonzero(A)
Value must not be zero.

This class restrict the value of Prop1 to be nonzero.

classdef MyClass
   properties
      Prop1 {mustBeNonzero}
   end
end

Create an object and assign a value to its property.

obj = MyClass;
obj.Prop1 = sin([-1,0,1]);
Error setting property 'Prop1' of class 'MyClass':
Value must not be zero.

When you assign a value to the property, MATLAB calls mustBeNonzero with the value being assigned to the property. mustBeNonzero issues an error because the one of the values in A is zero.

This function declares two input arguments. Input A must be a numeric vector and input offset must be a scalar that is not equal to zero.

function r = mbNonzero(A,offset)
    arguments
        A {mustBeNumeric}
        offset (1,1) {mustBeNonzero}
    end
    r = A + offset;
end

Calling the function with a value for offset equal to zero does not meet the requirements of mustBeNonzero and results in an error.

A = [12.7, 45.4, 98.9, 77.1, 53.1];
r = mbNonzero(A,0);
Error using mbNonzero
 r = mbNonzero(A,0);
                 ↑
Invalid input argument at position 2. Value must not be zero.

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

  • mustBeNonzero 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™.

Version History

Introduced in R2017a