Siti - in your PB_M_Callback callback (which presumably converts the alphanumeric text to morse code), the code is
function PB_M_Callback(hObject, eventdata, handles)
Letter=handles.Letter
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
NumberOrLetter={'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',' '};
for i=1:length(Letter);
[~, index] = ismember(Letter(i), NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
I suspect that the handles.Letter is the handle to the edit text control that the user types the alphanumeric string into. Is this the case? If true, and you want to get the string you (may) need to get the string value as handles.Letter is a handle). Once found, you will want to iterate over each character in the string and convert it to the morse code equivalent. The code
set(handle.Morse,sprintf,'string');
is incorrect - you need to specify the 'String' property before the value you want to assign to it, and sprintf is a built-in MATLAB function so I'm not sure what your intent is here.
I think you could do something like
function PB_M_Callback(hObject, eventdata, handles)
word = char(get(handles.Letter, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
NumberOrLetter={'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',' '};
for k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' morse(idx)];
end
end
set(handle.Morse, 'String', wordToMorseCode);
I've haven't tested the above but i think that you get the idea. I am assuming that word can be an array of one or more characters...if it is only one character, then the code should still work. I'm also assuming that there is a 1-1 mapping in the NumberOrLetter array to morse array. So we iterate over each character in the word and check to see if it is a member of NumberOrLetter. If it is, then we append the morse code equivalent to the wordToMorseCode string. Once we have iterasted over each character, then we set this string to the handle.Morse control.
The code will be similar when converting from morse code to the alphanumeric characters...but you will need to be a bit more aware that there are multiple characters in a single morse code that needs to get mapped to a single alphanumeric character.
Best Answer