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.

mustBeNegative

Validate that value is negative or issue error

Syntax

mustBeNegative(A)

Description

example

mustBeNegative(A) issues an error if A is not negative. A value is negative if it is less than zero. This function does not return a value.

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

Examples

collapse all

Use mustBeNegative to validate that the input contains only negative values.

The rand function creates uniformly distributed random numbers in the interval (0,1). Test the array after the subtraction to validate that all values are negative. If not, mustBeNegative issues an error.

A = rand(1,5) - 0.75;
mustBeNegative(A)
Error using mustBeNegative (line 14)
Value must be negative.

This class constrains the value of Prop1 to negative values.

classdef MyClass
   properties
      Prop1 {mustBeNegative}
   end
end

Create an object and assign a value to Prop1.

obj = MyClass;
obj.Prop1 = rand(1,5) - 0.75;
Error setting property 'Prop1' of class 'MyClass':
Value must be negative.

When you assign a value to the property, MATLAB® calls mustBeNegative with the value being assigned to the property. mustBeNegative issues an error if the any of the elements in the array are not negative.

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?