BAR
BAR

Reputation: 17121

Matlab - input validation

What is a good way to validate an input or exit the program with an error message altoghether?

For example if I take an input such as

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

How can I check if the number is greater than 0.

something like

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

Upvotes: 4

Views: 3856

Answers (4)

rymurr
rymurr

Reputation: 444

I use assert:

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

see here

Upvotes: 5

zellus
zellus

Reputation: 9592

Input Parser is offered by MATLAB as full-blown function input 'validator'.

Upvotes: 3

wwwald
wwwald

Reputation: 460

You can do more advanced checking on the input string using Matlab's functions for regular expressions:

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

For example, this allows you to make sure there are only numerical characters in the input string.

Upvotes: 1

Chris R
Chris R

Reputation: 735

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

 assert(Length > 0, 'your error msg')

Upvotes: 3

Related Questions