Checking For Proper Input of `Edit Text` String

2019-08-24 03:18发布

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.

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.

Here's essentially what I have:

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

1条回答
SAY GOODBYE
2楼-- · 2019-08-24 03:58

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..

Check the code below:

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.

查看更多
登录 后发表回答