Why does my code looks weird (solved, thanks everyone for helping)
2 views (last 30 days)
Show older comments
2 Comments
Stephen23
on 20 Apr 2021
Edited: Stephen23
on 20 Apr 2021
Original question by Ashley Siu Cheng Kok retrieved from Google Cache:
Why does my code only capture the last letter instead of full sentence?
for i=1:length(msg)
[~, index] = ismember(msg(i), letter);
if index > 0
set(handles.text2, 'string',morse{index});
end
end
Answers (1)
Walter Roberson
on 20 Apr 2021
You set the String to one letter. Then you do the next iteration and set the string to the next letter, overwriting the previous.
You need to accumulate the letters into something and then set that as the string after the loop
6 Comments
Walter Roberson
on 20 Apr 2021
letters = {};
for K=1:length(msg)
[~, index] = ismember(msg(K), LC);
if index > 0
letters{k} = morse{index};
else
letters{K} = '?';
end
end
converted_morse_words = strjoin(letters, ' ');
set(handles.text2,'string',converted_morse_words);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!