MATLAB: Going from switch case construct to loop

constructfor looploopmorse codescriptswitch

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.

Best 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.
Related Question