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 个评论
Stephen23
2022-4-13
Y = char(X);
Z = ~isnumeric(Y) && ~ischar(Y)
Can you show me one X value for which Z == TRUE ?
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!