mustBeNegative - Validate that value is negative - MATLAB (original) (raw)

Main Content

Validate that value is negative

Syntax

Description

mustBeNegative([value](#bvih7w5-1-A)) throws an error if value is not negative. A value is negative if it is less than zero. This function does not return a value.

mustBeNegative calls the following function to determine if the input is negative:

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

This function ignores input arguments that are empty values. Therefore, no error is thrown when the property or function argument value is empty.

example

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)

This class restricts 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.

This function declares two input arguments. Input lower must be negative and input upper must not be negative.

function r = mbNegative(lower,upper) arguments lower {mustBeNegative} upper {mustBeNonnegative} end x = lowerpi:upperpi; r = sin(x); end

Calling the function with a value for lower that does not meet the requirements of mustBeNegative results an error.

Error using mbNegative (line 3) mbNegative(0,4) ^ Invalid argument at position 1. Value must be negative.

Tips

Extended Capabilities

expand all

The mustBeNegative function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced in R2017a