Write in an edit text character set: A-Z, a-z and '-'

2 次查看(过去 30 天)
Hello all,
I have this problem with an edit text that should allow the user to enter only the charater set: A-Z, a-z and '-'. Using another character then a msg error should appear.
I used the code below:
variable_name=get(handles.variable_name_edittext,'String');
% Exclude characters, which are accepted by sscanf:
variable_name(ismember(variable_name, '-+eEgG')) = ' ';
% Convert to one number and back to a string:
check_val = sprintf('%g', sscanf(variable_name, '%g', 1));
set(handles.variable_name_edittext, 'String', check_val);
% Perhaps a small warning in WARNDLG or inside the GUI:
if ~all(ismember(variable_name, '-1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'))
msgbox('Please use for the name only the following characters: A-Z, a-z and -','Warning','warn')
set(handles.variable_name_edittext,'String','');
end
This is what I also found in one of the questions here, but it was reffering to numbers. So instead of '-1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz' was '.1234567890'.
With the numbers is working. With my character set.. not really.. I tried to type: test-TEST (which contains what is allowed) but the error message still appear for this case which should be accepted!
So.. where is my mistake here? Why is not working? Thank you! Diana

回答(1 个)

Voss
Voss 2021-12-28
You are replacing any character in '-+eEgG' with a space, but the space character is not in the valid character list.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by