MATLAB: Only can detect the last word in edit text that need to be translated.

#lettertomorsemorse code

With this code in the pushbutton callback, i only can detect the edit text last word, which when i key in a it will be translated to '.-' but with ab it only translate the b which i '-…' same goes to morse to letter. How do i translate the whole thing? Should i make a new array and loop outside ? how do this actually works? Need help! Hope for replies asap.
a = get(handles.edit1,'string');
a = upper(a);
a = strjoin(strsplit(a));
b = get(handles.popupmenu1,'value');
k=0;
morse = {'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','.-.-.-','/'};
letter = {'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',''};
switch b
case 1
set(handles.edit1,'string','');
set(handles.text4,'string','');
case 2
for i=1:length(a)
[~,j] = ismember(a(i),letter);
if j > 0
set(handles.text4,'string',morse{j});
end
end
case 3
for i=1:length(a)
[~,j] = ismember(a(i),morse);
if j > 0
set(handles.text4,'string',letter{j});
end
end
end

Best Answer

a = upper(get(handles.edit1,'string')); % uppercase, char string array
b = get(handles.popupmenu1,'value');
morse=string({'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','.-.-.-','/'});
letter=string([cellstr(['A':'Z'].');{'Stop'};{''}]);
switch b
case 1
set({handles.edit1,handles.text4},{'String'},{''}); % set both handles one call
case 2 % convert character string to Morse code sequence
mrse=strjoin(arrayfun(@(c) morse(c==letter),a));
handles.text4.String=mrse;
case 3 % convert string of morse symbols to letter char string
str=char(join(arrayfun(@(s) lettter(s==morse),strsplit(a)),'')); % convert back to char() string
handles.text4.String=str;
end