Going from switch case construct to loop?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Mohammad Abouali
2014-11-29
编辑:Mohammad Abouali
2014-11-29
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 个评论
Mohammad Abouali
2014-11-30
编辑:Mohammad Abouali
2014-11-30
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multirate and Multistage Filters 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!