Property Validation Functions - MATLAB & Simulink (2024)

Property Validation Functions

MATLAB Validation Functions

MATLAB® defines functions for use in property validation. These functions support common use patterns for validation and provide descriptive error messages. The following tables categorize the MATLAB validation functions and describe their use.

Numeric Value Attributes

Name

Meaning

Functions Called on Inputs

mustBePositive(value)

value > 0

gt, isreal, isnumeric, islogical

mustBeNonpositive(value)

value <= 0

ge, isreal, isnumeric, islogical

mustBeNonnegative(value)

value >= 0

ge, isreal, isnumeric, islogical

mustBeNegative(value)

value < 0

lt, isreal, isnumeric, islogical

mustBeFinite(value)

value has no NaN and no Inf elements.

isfinite

mustBeNonNan(value)

value has no NaN elements.

isnan

mustBeNonzero(value)

value ~= 0

eq, isnumeric, islogical

mustBeNonsparse(value)

value is not sparse.

issparse

mustBeSparse(value)

value is sparse.

issparse

mustBeReal(value)

value has no imaginary part.

isreal

mustBeInteger(value)

value == floor(value)

isreal, isfinite, floor, isnumeric, islogical

mustBeNonmissing(value)

value cannot contain missing values.

ismissing

Comparison with Other Values

Name

Meaning

Functions Called on Inputs

mustBeGreaterThan(value,c)

value > c

gt, isreal, isnumeric, islogical

mustBeLessThan(value,c)

value < c

lt, isreal, isnumeric, islogical

mustBeGreaterThanOrEqual(value,c)

value >= c

ge, isreal, isnumeric, islogical

mustBeLessThanOrEqual(value,c)

value <= c

le, isreal, isnumeric, islogical

Data Types

Name

Meaning

Functions Called on Inputs

mustBeA(value,classnames)

value must be of specific class.

Uses class definition relationships

mustBeNumeric(value)

value must be numeric.

isnumeric

mustBeNumericOrLogical(value)

value must be numeric or logical.

isnumeric, islogical

mustBeFloat(value)

value must be floating-point array.

isfloat

mustBeUnderlyingType(value,typename)

value must have specified underlying type.

isUnderlyingType

Size

Name

Meaning

Functions Called on Inputs

mustBeNonempty(value)

value is not empty.

isempty

mustBeScalarOrEmpty(value)value must be a scalar or be empty.

isscalar, isempty

mustBeVector(value)value must be a vector.

isvector

Membership and Range

Name

Meaning

Functions Called on Inputs

mustBeMember(value,S)

value is an exact match for a member of S.

ismember

mustBeInRange(value,lower,upper,boundflags)value must be within range.

gt, ge, lt, le

Text

Name

Meaning

Functions Called on Inputs

mustBeFile(path)

path must refer to a file.

isfile

mustBeFolder(folder)path must refer to a folder.

isfolder

mustBeNonzeroLengthText(value)

value must be a piece of text with nonzero length.

Not applicable

mustBeText(value)

value must be a string array, character vector, or cell array of character vectors.

Not applicable

mustBeTextScalar(value)

value must be a single piece of text.

Not applicable
mustBeValidVariableName(varname)varname must be a valid variable name.

isvarname

Validate Property Using Functions

Use property validation functions in class definitions to impose specific restrictions on property values. A validation function accepts a potential property value as an argument and issues an error if the value does not meet the specific requirement imposed by the function.

During the validation process, MATLAB passes the value to each validation function listed in the class definition. MATLAB calls each function from left to right and throws the first error encountered. The value passed to the validation functions is the result of any conversion applied by the class and size specifications. For more information on class and size validation, see Property Class and Size Validation.

For a list of MATLAB validation functions, see MATLAB Validation Functions.

Validation Function Syntax

Specify validation functions as a comma-separated list of function names or function calls with arguments, enclosed in braces.

classdef MyClass properties Prop {fcn1,fcn2} = defaultValue endend

MATLAB passes the potential property value to the validation function implicitly. However, if the validation function requires input arguments in addition to the potential property value, then you must include both the property and the additional arguments. Additional arguments must be literal values and cannot reference variables. Literal values are nonsymbolic representations, such as numbers and text.

For example, consider the function mustBeGreaterThan. It requires a limiting value as an input parameter. This validation function requires that a property value must be greater than this limiting value.

Pass the property as the first argument. Use the property name, but do not enclose the name in quotation marks. This property definition restricts Prop to values greater than 10.

properties Prop {mustBeGreaterThan(Prop,10)}end

Using Validation Functions

The following class specifies validation functions for each property.

  • Data must be numeric and finite.

  • Interp must be one of the three options listed. Specify a default value for this property to satisfy this requirement.

classdef ValidatorFunction properties Data {mustBeNumeric, mustBeFinite} Interp {mustBeMember(Interp,{'linear','cubic','spline'})} = 'linear' endend

Creating a default object of the class shows the initial values.

a = ValidatorFunction

Assigning values to properties calls the validation functions.

a.Data = 'cubic'
Error setting property 'Data' of class 'ValidatorFunction':Value must be numeric.

Because the Data property validation does not include a numeric class, there is no conversion of the char vector to a numeric value. If you change the validation of the Data property to specify the class as double, MATLAB converts the char vector to a double array.

properties Data double {mustBeNumeric, mustBeFinite}end

The assignment to the char vector does not produce an error because MATLAB converts the char vector to class double.

a.Data = 'cubic'
a = ValidatorFunction with properties: Data: [99 117 98 105 99] Interp: 'linear'

Assignment to the Interp property requires an exact match.

a = ValidatorFunction;a.Interp = 'cu'
Error setting property 'Interp' of class 'ValidatorFunction':Value must be a member of this set linear cubic spline

Using an enumeration class provides inexact matching and case insensitivity.

Enumeration Class for Inexact Matching

Property validation using an enumeration class provides these advantages:

  • Inexact, case-insensitive matching for unambiguous char vectors or string scalars

  • Conversion of inexact matches to correct values

For example, suppose that you define the InterpMethod enumeration class for the Interp property validation.

classdef InterpMethod enumeration linear cubic spline endend

Change the Interp property validation to use the InterpMethod class.

classdef ValidatorFunction properties Data {mustBeNumeric, mustBeFinite} Interp InterpMethod endend

Assign a value matching the first few letters of 'cubic'.

a = ValidatorFunction;a.Interp = 'cu'
a = ValidatorFunction with properties: Data: [] Interp: cubic

Define Validation Functions

Validation functions are ordinary MATLAB functions that are designed for the specific purpose of validating property and function argument values. Functions used to validate properties:

  • Accept the potential property value as an input argument

  • Do not return values

  • Display error messages if the validation fails

Creating your own validation function is useful when you want to provide specific validation that is not available using the MATLAB validation functions. You can create local functions within the class file or place the function on the MATLAB path to be available for use in any class.

For example, the ImgData class uses a local function to define a validator that restricts the Data property to only uint8 or uint16 values, excluding subclasses and not allowing conversion from other numeric classes. The predefined validation function mustBeInRange restricts the range of allowed values.

classdef ImgData properties Data {mustBeImData(Data),mustBeInRange(Data,0,255)} = uint8(0) endendfunction mustBeImData(a) if ~(isa(a,'uint8') || isa(a,'uint16')) error("Value of Data property must be uint8 or uint16 data.") endend

When you create an instance of the ImgData class, MATLAB validates that the default value is a uint8 or uint16 value, in the range 0...255, and not empty. Note that the default value must satisfy the validation requirements like any other value assigned to the property.

a = ImgData
a = ImgData with properties: Data: 0

Property assignment invokes the validators in left-to-right order. Assigning a char vector to the Data property causes an error thrown by mustBeImData.

a.Data = 'red';
Error setting property 'Data' of class 'ImgData'.Value of Data property must be uint8 or uint16 data.

Assigning a numeric value that is out of range causes an error thrown by mustBeInRange.

a.Data = uint16(312);
Error setting property 'Data' of class 'ImgData'. Value must be greaterthan or equal to 0, and less than or equal to 255.

For related functions, see mustBeInteger, mustBeNumeric, and mustBePositive.

Related Topics

  • Validate Property Values
  • Property Class and Size Validation

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Property Validation Functions- MATLAB & Simulink (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Property Validation Functions
- MATLAB & Simulink (2024)

References

Top Articles
Best Pool Heaters of Every Type: 2024 - Pool Research
7 Best Pool Heaters: 2023 Swimming Pool Heating Option Reviews
Using GPT for translation: How to get the best outcomes
Le Blanc Los Cabos - Los Cabos – Le Blanc Spa Resort Adults-Only All Inclusive
Health Benefits of Guava
Sissy Transformation Guide | Venus Sissy Training
Unlocking the Enigmatic Tonicamille: A Journey from Small Town to Social Media Stardom
Tap Tap Run Coupon Codes
The Powers Below Drop Rate
What Was D-Day Weegy
Little Rock Arkansas Craigslist
About Us | TQL Careers
Dump Trucks in Netherlands for sale - used and new - TrucksNL
Chile Crunch Original
Race Karts For Sale Near Me
Swgoh Blind Characters
bode - Bode frequency response of dynamic system
Iroquois Amphitheater Louisville Ky Seating Chart
Katie Sigmond Hot Pics
Ppm Claims Amynta
Egizi Funeral Home Turnersville Nj
Die 8 Rollen einer Führungskraft
Times Narcos Lied To You About What Really Happened - Grunge
Cfv Mychart
Truck from Finland, used truck for sale from Finland
Tracking every 2024 Trade Deadline deal
Uno Fall 2023 Calendar
49S Results Coral
Japanese Pokémon Cards vs English Pokémon Cards
Max 80 Orl
Skroch Funeral Home
Vip Lounge Odu
House Of Budz Michigan
Aveda Caramel Toner Formula
Babylon 2022 Showtimes Near Cinemark Downey And Xd
Sams La Habra Gas Price
دانلود سریال خاندان اژدها دیجی موویز
Gvod 6014
Vocabulary Workshop Level B Unit 13 Choosing The Right Word
Urban Blight Crossword Clue
2700 Yen To Usd
Tyler Perry Marriage Counselor Play 123Movies
R: Getting Help with R
855-539-4712
Bama Rush Is Back! Here Are the 15 Most Outrageous Sorority Houses on the Row
Gear Bicycle Sales Butler Pa
Kidcheck Login
Craigslist Yard Sales In Murrells Inlet
Worlds Hardest Game Tyrone
Inloggen bij AH Sam - E-Overheid
Ranking 134 college football teams after Week 1, from Georgia to Temple
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6352

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.