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

6 views (last 30 days)
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:
% 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')
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

Answers (1)

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


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!