Keep getting NaN error when i run code in gui

Hello i have attached both my. .Fig file and .M file when i run my code with my gui i keep getting NaN in my output gui please help :)

 Accepted Answer

You get NaN because of this:
function edit1_Callback(hObject, eventdata, handles)
% convert the message from edit1 to a number:
x=str2double(get(hObject,'String'));
% convert the number back to a char vector, put it in edit2
set( handles.edit2,'String',num2str(x));
Let's say the message you entered is 'OK'. Look what happens when you do str2double('OK'):
x = str2double('OK')
x = NaN
because 'OK' is not a character representation of a number. If your message was '98' it would work fine:
x = str2double('98')
x = 98
So the correct thing to do is avoid str2double:
function edit1_Callback(hObject, eventdata, handles)
word = get(hObject,'String');
set(handles.edit2,'String',word);
Now, there's another problem:
character = word(k);
[~, idx] = ismember(character, NumberOrLetter);
if ~isempty(idx)
The second output from ismember is a vector of indices into the second input argument, i.e., idx is the indices in NumberOrLetter where that element of NumberOrLetter is character. idx will only be empty if character is empty (and character is never empty because it is one character from word). But idx can be 0; specifically, it is 0 if character does not exist in NumberOrLetter. So you mean to use idx ~= 0 there rather than ~isempty(idx).
You'll also want to do something with the Morse-code encoded message (wordToMorseCode), like display it somewhere in the GUI.
And you will find that lower-case letters in your message do not get encoded into Morse code; that's because they don't exist in NumberOrLetter (no numbers too). You can allow lower-case letters by using the upper function to convert word to all upper-case before encoding. (And you can allow numbers by including them in NumberOrLetter along with their Morse code symbol in morse.)

5 Comments

Hi thanks for the reply i have changed what you asked me too i dont get NaN anymore however my message is not being decoded now when i input MOO for example it just come out as MOO
anyone ? :) i really need help
You set edit2's String to be the input message at the top of edit1's Callback:
function edit1_Callback(hObject, eventdata, handles)
word = get(hObject,'String');
set(handles.edit2,'String',word);
Then the encoded message is constructed and nothing is done with it. Like I said before, probably you want to do something with the encoded message. Maybe edit2 is where the encoded message is supposed to be displayed:
function edit1_Callback(hObject, eventdata, handles)
word = get(hObject,'String');
% set(handles.edit2,'String',morse);
wordToMorseCode = '';
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
NumberOrLetter={'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',' '};
for k=1:length(word)
character = word(k);
[~, idx] = ismember(character, NumberOrLetter);
if idx ~= 0
if isempty(wordToMorseCode)
wordToMorseCode = morse{idx};
else
wordToMorseCode = [wordToMorseCode ' ' morse{idx}];
end
end
end
set(handles.edit2,'String',wordToMorseCode);
You'll still not be able to encode numbers or lower-case letters, again, like I said before.
Thank you so much for this work really saved me,much appreaciated <3
You're welcome! If you have any questions, let me know. Otherwise, please click "Accept this Answer". Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 15 Apr 2022

Commented:

on 15 Apr 2022

Community Treasure Hunt

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

Start Hunting!