Matlab - input validation

What is a good way to check input or exit with an error message?

For example, if I take input, for example

Length = input('\nEnter a length in feet: '); 

How to check if number is greater than 0.

sort of

 if Length > 0 then %%do code else %%Output error %%nothing to do here so it just continues and exits end 
+4
source share
4 answers

I am using assert:

 assert(Length>0,'Length is less than zero, exiting.') 

see here

+5
source

You can use Matlabs built into the assert function (type doc assert or help assert )

  assert(Length > 0, 'your error msg') 
+3
source

The input parser is proposed by MATLAB as a full-featured input of the validator function.

+3
source

You can perform a more advanced check of the input string using the Matlab functions for regular expressions:

http://www.mathworks.com/help/techdoc/ref/regexp.html

For example, this allows you to make sure that the input string contains only numeric characters.

+1
source

Source: https://habr.com/ru/post/1338695/


All Articles