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

1 次查看(过去 30 天)
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 个评论
Lai Ken Siang
Lai Ken Siang 2022-4-13
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);

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-4-13
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by