开发者

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


I use assert:

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

see here


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

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜