Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

mustBePositive

Validate that value is positive or issue error

Syntax

mustBePositive(A)

Description

example

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:

Examples

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.

mustBePositive(A)
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
   properties
      Prop1 {mustBePositive}
   end
end

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

Was this topic helpful?