How to display errordlg when input is non alphabet and non numeric( including negative(-) and fraction(/) symbol ) in matlab gui

1 view (last 30 days)
I try to use this code but nothing happen and when I input symbol there is an error msg 'Array indices must be positive integers or logical values'
input = char(get(handles.edit1, 'String'));
if ~isnumeric(input) && ~ischar(input)
errordlg('ERROR : input must be alphabet or number');
end
  2 Comments
Lai Ken Siang
Lai Ken Siang on 13 Apr 2022
So this is my code and when I input any combination of alphabets and numbers it works well. When I input special characters/ symbol like &$^ it didnt pop up the errordlg. May I knw whr did I went wrong?
input = char(get(handles.edit1, 'String'));
if ~isnumeric(input) && ~ischar(input)
errordlg('ERROR : input must be alphabet or number');
end
input = upper(input);
tomorse = '';
morse = {'.----','..---','...--','....-','.....','-....','--...','---..',...
'----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....',...
'..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...',...
'-','..-','...-','.--','-..-','-.--','--..','/','.-.-.-'};
alphanum = {'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F',...
'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V',...
'W','X','Y','Z',' ','STOP'};
for i=1:length(input)
character = input(i);
[~, index] = ismember(character, alphanum);
if ~isempty(index)
if isempty(tomorse)
tomorse = morse{index};
else
tomorse = [tomorse ' ' morse{index}];
end
end
end
set(handles.text7, 'string',tomorse);

Sign in to comment.

Accepted Answer

Voss
Voss on 13 Apr 2022
Instead of checking for characters that aren't letters and aren't numbers, how about you check for characters that cannot be encoded (because they aren't in your character set alphanum)? After all, ' ' (space) is not a letter and it's not a number, but it can be encoded into Morse code using your program.
Another thing to consider is that 'STOP' is not a single character, so you'll never have the character 'STOP' in the input message. Therefore it can be removed from alphanum and then alphanum can be made into a character vector rather than a cell array, which simplifies things a little bit.
(Also, don't use input as a variable name since it's the name of a built-in MATLAB function.)
input_str = upper(char(get(handles.edit1, 'String')));
alphanum = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
[ism, index] = ismember(input_str, alphanum); % now you have the index for all the characters in the input message
if any(~ism)
errordlg('ERROR : input must be letters and numbers (and spaces) only');
return
end
morse = {'.----','..---','...--','....-','.....','-....','--...','---..',...
'----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....',...
'..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...',...
'-','..-','...-','.--','-..-','-.--','--..','/','.-.-.-'};
tomorse = '';
for i=1:length(input_str)
if isempty(tomorse)
tomorse = morse{index(i)};
else
tomorse = [tomorse ' ' morse{index(i)}];
end
end
set(handles.text7, 'string',tomorse);

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!