Going from switch case construct to loop?

1 vue (au cours des 30 derniers jours)
Pam
Pam le 29 Nov 2014
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.

Réponse acceptée

Mohammad Abouali
Mohammad Abouali le 29 Nov 2014
Modifié(e) : Mohammad Abouali le 29 Nov 2014
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 commentaires
Pam
Pam le 30 Nov 2014
oh ok is it possible to just have one code instead or would that cause a problem?
Mohammad Abouali
Mohammad Abouali le 30 Nov 2014
Modifié(e) : Mohammad Abouali le 30 Nov 2014
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multirate and Multistage Filters dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by