How to display errordlg when input is non alphabet and non numeric( including negative(-) and fraction(/) symbol ) in matlab gui
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 13 Avr 2022
Y = char(X);
Z = ~isnumeric(Y) && ~ischar(Y)
Can you show me one X value for which Z == TRUE ?
Réponse acceptée
Voss
le 13 Avr 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);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!