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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 13 Apr. 2022
Y = char(X);
Z = ~isnumeric(Y) && ~ischar(Y)
Can you show me one X value for which Z == TRUE ?
Lai Ken Siang
Lai Ken Siang am 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);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 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);

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by