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 positive or issue error





mustBePositive(A) issues an error if A is not positive. Values are positive when they are real, numeric, and greater than zero. This function does not return a value.

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

This function ignores input arguments that are empty values. No error is thrown if the property value is empty.


collapse all

Use mustBePositive to validate that the input contains only positive values.

The rand function creates a uniformly distributed random number.

A = rand(1,5) -0.5;

Validate that array elements are positive.

Error using mustBePositive (line 14)
Value must be positive.

The result of subtracting 0.5 from the array return by rand can contain negative numbers. When a value is negative, mustBePositive issues an error.

This class constrains the value of Prop1 to positive values.

classdef MyClass
      Prop1 {mustBePositive}

Create an object and assign a value to its property.

obj = MyClass;
obj.Prop1 = 0;
Error setting property 'Prop1' of class 'MyClass':
Value must be positive.

When you assign a value to the property, MATLAB® calls mustBePositive with the value being assigned to the property. mustBePositive issues an error because the value 0 is not positive.

Input Arguments

collapse all

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

Extended Capabilities

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

Introduced in R2017a