Going from switch case construct to loop?

I wrote a switch case script to convert characters to morse code. My script only accepts one letter or number at a time and displays it's morse code but now I am trying to figure out a wat for it to accept whole words and convert them. I began writing the loop but I am quite lost as I was only recently introduced to loops so I don't really know how to go about it. Any examples out there that could help? I would place my original script here but it's long so I don't think it's convenient.

 Accepted Answer

put your current code in a function such as
function outputMorse=char2morse(inputeChar)
So this function gets a character and returns the morse code. Put your code that you have there. Name that file char2morse.m (or whatever you gonna name that function.
Then write another code
function outputMorse=string2morse(inputString)
validateattributes(inputString,{'char'},{'vector'})
for i=1:numel(inputString)
outputMorse{i}=char2morse(inputString(i));
end
put this in a file called string2morse.m (or again whatever you want to name this function.)
Now you can call this function in other codes like:
morseCode=string2morse('SOS')
NOTE: Based on your code and output type you need to change the above code. But the general structure would be like that.

14 Comments

I am afraid not.
Download the attached files and store them in a folder of your choice.
Make sure MATLAB working directory is in the same folder as you downloaded these two files.
use char2MorseCode() to convert one character at a time.
use string2MorseCode() to convert on string at a time.
Some thing like this:
>> char2MorseCode('a')
ans =
.-
>> char2MorseCode('b')
ans =
-...
>> char2MorseCode('c')
ans =
-.-.
>> string2MorseCode('abc')
ans =
.--...-.-.
>> string2MorseCode('sos')
ans =
...---...
============================= The content of these two files is listed here:
FILE: char2MorseCode.m
function output=char2MorseCode(s)
validateattributes(s,{'char'},{'size',[1 1]})
% some intializations
MC_1='.----'; MC_2='..---'; MC_3='...--';
MC_4='....-'; MC_5='.....'; MC_6='-....';
MC_7='--...'; MC_8='---..'; MC_9='----.';
MC_0='-----'; MC_A='.-'; MC_B='-...';
MC_C='-.-.'; MC_D='-..'; MC_E='.';
MC_F='..-.'; MC_G='--.'; MC_H='....';
MC_I='..'; MC_J='.---'; MC_K='-.-';
MC_L='.-..'; MC_M='--'; MC_N='-.';
MC_O='---'; MC_P='.--.'; MC_Q='--.-';
MC_R='.-.'; MC_S='...'; MC_T='-';
MC_U='..-'; MC_V='...-'; MC_W='.--';
MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';
index=['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'];
morseCode={MC_1 MC_2 MC_3 ...
MC_4 MC_5 MC_6 ...
MC_7 MC_8 MC_9 ...
MC_0 ...
MC_A MC_B MC_C MC_D MC_E MC_F MC_G ...
MC_H MC_I MC_J MC_K MC_L MC_M MC_N ...
MC_O MC_P MC_Q MC_R MC_S MC_T MC_U ...
MC_V MC_W MC_X MC_Y MC_Z};
output=morseCode{index==upper(s)};
end
FILE: string2MorseCode.m
function output=string2MorseCode(s)
validateattributes(s,{'char'},{'row'});
output=cell2mat(arrayfun(@(c) char2MorseCode(c),s,'UniformOutput',false));
end
this is what I have so far, I appreciate the help but i dont want to completely copy, does this look like its headed in the right track, I want to still use the switch construct in the for section but i think i have done it incorrectly. How can I fix/add on to this. Again I appreciate the answer but I think its best I stick to commands I'm more used to.
check my answer just before your last post. I think it did not refreshed while I was typing.
So pretty much it's better to code it something like those two files that I sent you.
Now you can write some more code that gets a string from the user and returns the morse code.
If you want an space between the parts of the morse code then add a space at the end of each of the morse codes in char2MorseCode.m for example change
MC_1='.----'; to MC_1='.---- '; note the extra space at the end right before the last quotation mark.
and
MC_2='..---'; to MC_2='..--- ';
and so on for the rest of the codes.
Doing this will cause string2MorseCode('abc') to output '.- -.. .-.-. ' instead of '.--...-.-.'
I just do not understand the string2morsecode.m file I haven't been taught yet how to do such in depth programming but I am trying my best to write a loop, I understand your first script best.
arrayfun applies a function to each element of an array. In this case it applies char2MorseCode to each character of string in s.
so let's say s='abc'. it has three characters 'a', 'b', 'c';
arrayfun gets s and then applies char2MorseCode to each of its elements, i.e. each character.
you can change the string2MorseCode.m as follow (in case you don't want to use arrayfun):
function output=string2MorseCode(s)
validateattributes(s,{'char'},{'row'});
output=[];
for i=1:numel(s)
output=[output char2MorseCode(s(i))];
end
end
okay but what does validateattributes(s,{'char'},{'row'}); mean? also, for function output is that similar to asking for an input?
that checks input arguments.
So in this case it checks s
First it checks to make sure that S is of type character. so if you pass an array of numbers it would complain and doesn't run the code. this is the {'char'} part. NOTE: '1' is ok but 1 is not ok.
then it checks that the size of input array s is something like 1xN where N is the number of characters in s. this is the {'row'} part.
so validateattributes can be used to make sure that the input arguments are of the required type and size and other assumptions that you may have.
By the way, I just check that if you do
string2MorseCode('My long string')
the code will crash.
That is because there is no morse code for ' ', i.e. spaces. In general if you have a character in S that we don't have the morse code it would crash.
I changed to code to adjust for this so that it won't crash. and it just ignores the unknown characters.
Get the new version of char2MorseCode.m in attached here.
==============
FILE: char2MorseCode.m
function output=char2MorseCode(s)
validateattributes(s,{'char'},{'size',[1 1]})
% some intializations
MC_1='.---- '; MC_2='..--- '; MC_3='...-- ';
MC_4='....- '; MC_5='..... '; MC_6='-.... ';
MC_7='--... '; MC_8='---.. '; MC_9='----. ';
MC_0='----- '; MC_A='.- '; MC_B='-... ';
MC_C='-.-. '; MC_D='-.. '; MC_E='. ';
MC_F='..-. '; MC_G='--. '; MC_H='.... ';
MC_I='.. '; MC_J='.--- '; MC_K='-.- ';
MC_L='.-.. '; MC_M='-- '; MC_N='-. ';
MC_O='--- '; MC_P='.--. '; MC_Q='--.- ';
MC_R='.-. '; MC_S='... '; MC_T='- ';
MC_U='..- '; MC_V='...- '; MC_W='.-- ';
MC_X='-..- '; MC_Y='-.-- '; MC_Z='--.. ';
index=['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'];
morseCode={MC_1 MC_2 MC_3 ...
MC_4 MC_5 MC_6 ...
MC_7 MC_8 MC_9 ...
MC_0 ...
MC_A MC_B MC_C MC_D MC_E MC_F MC_G ...
MC_H MC_I MC_J MC_K MC_L MC_M MC_N ...
MC_O MC_P MC_Q MC_R MC_S MC_T MC_U ...
MC_V MC_W MC_X MC_Y MC_Z};
mask=index==upper(s);
if any(mask)
output=morseCode{mask};
else
output='';
end
end
im still a bit c.onfused but I am trying to understand. this is what I have that I have grasped from your script, this is what I understand thus far:
MC_1='.----'; MC_2='..---'; MC_3='...--';
MC_4='....-'; MC_5='.....'; MC_6='-....';
MC_7='--...'; MC_8='---..'; MC_9='----.';
MC_0='-----'; MC_A='.-'; MC_B='-...';
MC_C='-.-.'; MC_D='-..'; MC_E='.';
MC_F='..-.'; MC_G='--.'; MC_H='....';
MC_I='..'; MC_J='.---'; MC_K='-.-';
MC_L='.-..'; MC_M='--'; MC_N='-.';
MC_O='---'; MC_P='.--.'; MC_Q='--.-';
MC_R='.-.'; MC_S='...'; MC_T='-';
MC_U='..-'; MC_V='...-'; MC_W='.--';
MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';
Word=input('Please enter text:','s');
Word=upper(Word);
for Index=['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'];
Morsecode=[MC_1 MC_2 MC_3 MC_4 MC_5 MC_6 MC_7 MC_8 MC_9 MC_0 MC_A MC_B MC_C MC_D MC_E MC_F MC_G MC_H MC_I MC_J MC_K MC_L MC_M MC_N MC_O MC_P MC_Q MC_R MC_S MC_T MC_U MC_V MC_W MC_X MC_Y MC_Z];
end
is this somewhat what you are explaining?
The code that you are putting here is not correct. It won't work. This is pretty much what you had before.
Use the latest char2MorseCode.m (not the first version I sent) and then if you don't want to use arrayfun for string2MorseCode edit string2MorseCode.m to look likes:
function outputMorse=string2MorseCode(inputString)
validateattributes(inputString,{'char'},{'vector'})
for i=1:numel(inputString)
outputMorse{i}=char2morse(inputString(i));
end
That's how you should use the for loop.
I really appreciate it that you are not handing in a code that you don't understand.
That is really a very admirable attitude. (some students just copy/paste their homework and they think we don't find it).
On that account, I can say you are on right track.
So try this line as your code:
Word=input('Please enter text:','s');
MorseCodeString=string2MorseCode(Word);
disp('Converting your string to morse code:')
disp(MorseCode);
Now let's dissect this code.
Word=input('Please enter text:','s');
Get's an input string from the user.
MorseCodeString=string2MorseCode(Word); passes the input string to string2MorseCode() function that is written stored in a file called with the same name, i.e. string2MorseCode.
Within string2MorseCode() function, each character of the input string is sent to another function called char2MorseCode();
Within char2MorseCode, the character is first compared to index. Index stores '1' '2' ... to 'Z'.
within char2MorseCode there is another variable also called MorseCode which stores the morse code for '1' '2' ... 'Z'. The morse code and the index match each other, i.e. the first element of MorseCode is related to '1' the second is related to '2' and so on. So there is one-to-one match between index and MorseCode. It pretty much works like your switch cases. but in a more compact way.
The last part of the code in char2MorseCode(), i.e.:
mask=index==upper(s);
if any(mask)
output=morseCode{mask};
else
output='';
end
works as this. First mask would be a boolean array checking which character was input was it '1' or '2' or '3' or ... 'Z' or none-of them.
any(mask) check if there was any match. if it was morseCode{mask} returns the morse code for the given character if there is no match then output is set to an empty string.
string2MorseCode() pretty much performs the above tasks for each character of the string that the user input and then outputs the results, which is the morse code of the string in MorseCodeString.
The next two lines, i.e.:
disp('Converting your string to morse code:')
disp(MorseCode);
displays the results.
thank you for a more in depth explanation, so this is basically kind of like a code within a code is what I am seeing?
Correct Instead of having everything in one code in one file, you divide your code into parts. This makes it more manageable and debugging easier.
oh ok is it possible to just have one code instead or would that cause a problem?
Well in this case doesn't cause any problem really. You can have them all in one code. Something like this:
%%some intializations
MC_1='.---- '; MC_2='..--- '; MC_3='...-- ';
MC_4='....- '; MC_5='..... '; MC_6='-.... ';
MC_7='--... '; MC_8='---.. '; MC_9='----. ';
MC_0='----- '; MC_A='.- '; MC_B='-... ';
MC_C='-.-. '; MC_D='-.. '; MC_E='. ';
MC_F='..-. '; MC_G='--. '; MC_H='.... ';
MC_I='.. '; MC_J='.--- '; MC_K='-.- ';
MC_L='.-.. '; MC_M='-- '; MC_N='-. ';
MC_O='--- '; MC_P='.--. '; MC_Q='--.- ';
MC_R='.-. '; MC_S='... '; MC_T='- ';
MC_U='..- '; MC_V='...- '; MC_W='.-- ';
MC_X='-..- '; MC_Y='-.-- '; MC_Z='--.. ';
index=['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'];
morseCode={MC_1 MC_2 MC_3 ...
MC_4 MC_5 MC_6 ...
MC_7 MC_8 MC_9 ...
MC_0 ...
MC_A MC_B MC_C MC_D MC_E MC_F MC_G ...
MC_H MC_I MC_J MC_K MC_L MC_M MC_N ...
MC_O MC_P MC_Q MC_R MC_S MC_T MC_U ...
MC_V MC_W MC_X MC_Y MC_Z};
%%Get users string
Word=input('Please enter text:','s');
MorseCodeString=string2MorseCode(Word);
%%Converting to Morse Code
MorseCode='';
for i=1:numel(Word)
mask=index==upper(Word(i));
if any(mask)
MorseCode=[MorseCode morseCode{mask}];
end
end
%%Displaying the results
disp('Converting your string to morse code:')
disp(MorseCode);

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!