Morse code decoder, please help me

11 vues (au cours des 30 derniers jours)
minyeop jeon
minyeop jeon le 7 Juin 2018
Modifié(e) : DGM le 27 Août 2024
text1=string(input ('(Please Enter morse code: \n','s'));
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'};
for i=1:length(text1)
for j=1:length(letter)
if strcmpi(text1(i),morse(j))==1
disp(letter(j));
end
end
end
--------------------------------------------------------------------
This is my code.
When I enter '.-' on matlab commander it display 'A'. But if I enter '.- -...' to show 'A' 'B', Matlab can not read it.
Help.
  2 commentaires
Walter Roberson
Walter Roberson le 7 Juin 2018
Hint: once you have broken the input up into pieces, you can match by using ismember()
Adam
Adam le 7 Juin 2018
It would probably be more efficient to use
doc containers.Map
to put your morse letters as keys and ordinary letters as values (and vice versa if you want the reverse too)

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 7 Juin 2018
Or:
text1 = input('Please Enter morse code: \n', 's');
morse = {'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..', ...
'--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-', ...
'-.--','--..'};
letter = char('A':'Z');
symbol = strsplit(text1, ' ');
for i = 1:length(symbol)
disp(letter(ismember(morse, symbol{i}))
end
Or without a loop:
[~, index] = ismember(symbol, morse);
disp(letter(index))
  3 commentaires
minyeop jeon
minyeop jeon le 11 Juin 2018
Thank you so much. I want to know the meaning of ' [~, index] = ismember(symbol, morse);'. I'm not good at English so I can't find about the function of [~, name]. Could you inform links or explations of it to me?
Jan
Jan le 11 Juin 2018
The tilde ~ means, that this output argument is not used. It is equivalent to:
[dummy, index] = ismember(symbol, morse);
and then ignoring the value of the variable dummy. All you need for the decoding is the 2nd output of ismember.

Connectez-vous pour commenter.

Plus de réponses (2)

Rishabh Rathore
Rishabh Rathore le 7 Juin 2018
The problem with your solution is, you are comparing '.- -...' to '.-'.
First break the input string to individual morse letters and then run the loop.
Make the following changes in your code, it should work fine
text1=string(input ('(Please Enter morse code: \n','s'));
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'};
%-----Changes----
text1=textscan(text1,'%s','Delimiter',' '); %getting individual morse letters
text1=text1{1};
%----------------
for i=1:length(text1)
for j=1:length(letter)
if strcmpi(text1(i),morse(j))==1
disp(letter(j));
end
end
end
  2 commentaires
minyeop jeon
minyeop jeon le 11 Juin 2018
Thank you for your informations.
Maddie Long
Maddie Long le 12 Fév 2020
Hi, what if we want to send in mutiple letters? for example
test1 = .... ..
i get the error
"index exceeds the number of array elements"
thanks

Connectez-vous pour commenter.


Jair Abdiel
Jair Abdiel le 27 Août 2024
Modifié(e) : Walter Roberson le 27 Août 2024
%% Codigo Morse Jair Hdz.
% Enlaces Guía
% https://la.mathworks.com/help/matlab/ref/input.html
clear all; clc;
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..'};
letras={'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'};
text=input ('Texto: \n',"s");
for i=1:length(text)
for j=1:length(letras)
if strcmpi(text(i),letras(j))==1
disp(morse(j))
end
end
end
  1 commentaire
DGM
DGM le 27 Août 2024
This is an encoder, not a decoder. It also just dumps the results to console in an unusable format, one quoted symbol per line, but that's what all the other example do too. Regardless of whether everyone else is doing it that way, it means that the output of your encoder can't actually be used by any decoder.
Consider the example encoder/decoder pair. No loops, rudimentary support for numbers and interword spaces.
% the input
text = 'blah 123';
% the lookup tables
morse = {'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.', ...
'---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..', ...
'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.',' '};
letter = ['A':'Z' '0':'9' ' '];
% encode
text = regexprep(upper(text),'[^A-Z0-9 ]',''); % sanitize input
[~, index] = ismember(num2cell(text),num2cell(letter)); % lookup
encoded = strjoin(morse(nonzeros(index)),' ') % assemble output
encoded = '-... .-.. .- .... .---- ..--- ...--'
% decode
symbol = strrep(encoded,' ',' # '); % temporarily replace long spaces
symbol = strsplit(symbol,' '); % split
symbol = strrep(symbol,'#',' '); % recover inter-word spaces
[~, index] = ismember(symbol, morse); % lookup
decoded = letter(nonzeros(index)) % assemble output
decoded = 'BLAH 123'
Both the encoded and decoded outputs are actually stored in usable variables instead of just being displayed and immediately discarded.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Stability Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by