This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Validate that value is nonnegative or issue error





mustBeNonnegative(A) issues an error if A is negative. This function does not return a value.

mustBeNonnegative accepts user-defined objects if the class of the object implements these methods:


collapse all

Use mustBeNonnegative to validate that the input contains only nonnegative values.

The randn function creates normally distributed random numbers.

A = randn(1,5);

Validate that the random numbers are nonnegative.

Error using mustBeNonnegative (line 14)
Value must be nonnegative.

This class constrains the value of Prop1 to nonnegative values.

classdef MyClass
      Prop1 {mustBeNonnegative}

Create an object and assign a value to its property.

obj = MyClass;
obj.Prop1 = -10;
Error using mustBeNonnegative (line 14)
Value must be nonnegative.

When you assign a value to the property, MATLAB® calls mustBeNonnegative with the value being assigned to the property. mustBeNonnegative issues an error because the value -10 is negative.

Input Arguments

collapse all

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

Example: A = 1 does not generate an error.

Extended Capabilities

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

Introduced in R2017a