Morse Decoder with Character Counter

7 views (last 30 days)
I'm trying to program a morse decoder, in which you enter morse code (letters separated by '/') which then counts the frequencies of each character in the output.
Here is where I've gotten stuck:
clear all, close all,
clc
userMorse = '-.-./---/...-/../-../.----/----./..--.- /.--./.-/-./-.././--/../-.-./..--.- /...-/.-/-.-./-.-./../-././';
code = {'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-',...
'-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---',...
'.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..', '..--.-'};
letter = {'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','-'};
codeTable = cell(2,37);
codeTable(1,:) = letter;
codeTable(2,:) = code;
splitUserMorse = strsplit(userMorse,'/');
for i = 1:length(splitUserMorse)
indexC = strfind(code,cell2mat(splitUserMorse(i)));
Index{1,i} = find(not(cellfun('isempty',indexC)));
end

Answers (1)

Alamanda Ponappa Poovaya
Alamanda Ponappa Poovaya on 9 Nov 2021
Edited: Alamanda Ponappa Poovaya on 9 Nov 2021
Hi,
First of all, there are a few errors with your userMorse variable. Every time you have typed "..--.-", which indicates a '-' character, there is an additional space before the /. This will cause errors with string matching.
Another change needed is the last / at the end of the userMorse variable must be omitted, otherwise, the split function returns one blank element at the end.
You can see the code below, where I have made some changes. It should work now. In the end, I am saving your results in a table, as it would visually appear nicely
clear all, close all,
clc
userMorse = '-.-./---/...-/../-../.----/----./..--.-/.--./.-/-./-.././--/../-.-./..--.-/...-/.-/-.-./-.-./../-./.';
code = {'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-',...
'-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---',...
'.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..', '..--.-'};
letter = {'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','-'};
splitUserMorse = strsplit(userMorse,'/');
indexC = zeros(1,length(splitUserMorse));
%Convert morse code to string
for i = 1:length(splitUserMorse)
idx = find(strcmp(code,splitUserMorse{i}));
indexC(i) = idx;
end
s = letter(indexC);
freq = zeros(1,37);
%find frequency of each character in string
for i = 1:length(letter)
freq(i) = nnz(find(strcmp(s,letter{i})));
end
%SAve results in table
sz = [37 2];
varTypes = ["string","double"];
varNames = ["Letter","Frequency",];
tlb = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
tlb(:,1) = letter';
tlb(:,2) = num2cell(freq)';

Community Treasure Hunt

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

Start Hunting!

Translated by