Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Use the information given from preexisting Morse.mat data file in Matlab,

ID: 3812013 • Letter: M

Question

MATLAB

Use the information given from preexisting Morse.mat data file in Matlab, and create the following two functions:
(1) "morseEncoder.m" that reads a passcode in English characters and return
the passcode as a Morse code
(2) "morseDecoder.m" that reads a Morse passcode and return
the passcode in English characters.
Apply morseEncoder function on the passcode "AZ4M6NN0S789P" and print the morse code result in command window.
Next, apply the morseDecoder function to the result of previous step andreproduce the orginal passcode in English. Print the result in command window.

Explanation / Answer

morseEncoder.m:

load('morse.mat');
prompt = 'Enter the word to be converted to Morse Code: ';
str = input(prompt, 's');
ans = '';
for i = 1:length(str)
for j = 1:36
if(str(i) == morseChars{j,1})
b = morseChars{j,2};
break;
end
end
if(i ~= 1)
ans = strcat(ans, {' '}, b);
else
ans = strcat(ans, b);
end
end

disp(ans);

Output:

Enter the word to be converted to Morse Code:

AZ4M6NN0S789P

.- --.. ....- -- -.... -. -. ----- ... --... ---.. ----. .--.

morseDecoder.m:

load('morse.mat');
prompt = 'Enter the Morse Code to be converted: ';
str = input(prompt, 's');
C = strsplit(str,' ');

ans = '';
status = 0;
for i=1:length(C)
for j = 1:36
if(strcmp(C(i), morseChars{j,2}))
b = morseChars{j,1};
break;
end
end
ans = strcat(ans, b);
end

disp(ans);

Output:

Enter the Morse Code to be converted:

.- --.. ....- -- -.... -. -. ----- ... --... ---.. ----. .--.

AZ4M6NN0S789P