检查“编辑文本"字符串的正确输入 [英] Checking For Proper Input of `Edit Text` String

查看:100
本文介绍了检查“编辑文本"字符串的正确输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUIDE GUI,要求用户输入他们的姓名.如果他们输入数字字符,字符串和数字字符的混合或空白框,则应执行错误对话框.

I have a GUIDE GUI where I ask the user to enter in their name. It should execute an error dialog if they enter numerical characters, a mix of string and numerical characters, or a blank box.

问题是,当我输入数字或字符串和数字字符的组合时,它将输出Error Code II(第一个elseif语句)而不是Error Code III(仅输入数字)或Error Code IV (输入数字和字符串).输入将不胜感激.

The issue is that when I enter in numbers or a mix of string and numerical characters, then it is outputting Error Code II (1st elseif statement) instead of Error Code III (entering in numbers only) or Error Code IV (entering numbers and strings). Input would be greatly appreciated.

本质上是我所拥有的:

if isempty(editString)
errordlg('Please enter a name into the text-box. We thank you in anticipation.',...
'Error Code I');
return
elseif char(editString) > 12
    errordlg('Please enter a name that is less than 12 characters long. Thank you.',...
 'Error Code II');
    return
elseif isa(editString, 'integer')
    errordlg('Please enter a name, not numbers. Thank you.', 'Error Code III');
    return
elseif isa(editString, 'integer') && isa(editString, 'char')
    errordlg('Please enter a name without mixing numbers & characters. Thanks.',...
    'Error Code IV');
else 
delete(gcf)    
gui_02
end

推荐答案

isa()函数在这种情况下不起作用,因为您从Edit Text读取的全部是string,换句话说是char.因此,即使您编写isa('123', 'integer'),函数也会返回0而不是1.无论如何,多亏了MATLAB,它具有一个函数: isstrprop() 确定是否字符串属于指定的类别,例如integerchar ..

Well, isa() function doesn' t work in this case because all you read from Edit Text is string in other words char. Thus, if you even write isa('123', 'integer'), function returns 0 not 1. Anyway, thanks to MATLAB there is a function: isstrprop() determines whether string is of specified category such as integer, char..

检查以下代码:

if isempty(editString)
    errordlg('Please enter a name into the text-box. We thank you in anticipation.', 'Error Code I');
    return
elseif length(editString) > 12
    errordlg('Please enter a name that is less than 12 characters long. Thank you.', 'Error Code II');
    return
elseif ~isempty(find(isstrprop(editString, 'digit'), 1)) & isempty(find(isstrprop(editString, 'alpha'), 1))
    errordlg('Please enter a name, not numbers. Thank you.', 'Error Code III');
    return

elseif ~isempty(find(isstrprop(editString, 'digit'), 1)) & ~isempty(find(isstrprop(editString, 'alpha'), 1))
    errordlg('Please enter a name without mixing numbers & characters. Thanks.', 'Error Code IV');
    return
end

它看起来并不优雅,但可以正常工作.

It doesn' t look elegant but works.

这篇关于检查“编辑文本"字符串的正确输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆