Help with for loops?

3 views (last 30 days)
Pam
Pam on 30 Nov 2014
Edited: Pam on 14 Dec 2014
I am trying to turn my switch construct into a for loop but I know I have to use it somewhere in my for loop. I am a beginner so I am trying to stick to simple commands as I do not understand much. I did recieve an answer before and I appreciate the help but I am just very frustrated I cannot figure this out. This is what I have so far:
...
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'];
switch Word
case '1'
Code=MC_1;
case '2'
Code=MC_2;
...
case 'Z'
Code=MC_Z;
otherwise
disp('invalid input');
end
This is how I am supposed to be going about it, and I know it's not the fastest or simplest but this is how I understand it. Thank you to anyone that helps and to the person that tried to help me before.
--dpb edit took liberty of shortening case statement simply for brevity. Perhaps more folks will take some time when code isn't quite so long...--
  2 Comments
dpb
dpb on 30 Nov 2014
The above loop will simply repeat the same case for 37 times --
I recommend after searching for the previous referenced thread that you return to it and work through the explanation given there instead of starting another thread at the same place as before.
That is a very complete explanation and I don't see any sense in rehashing the same plowed ground -- any other response is going to be essentially the same as what has been provided to date.
For others' convenience, here's the preceding thread link--
Pam
Pam on 30 Nov 2014
yes they did give me a very thorough explanation and I appreciate it but its just I dont understand what it means at all, I am new to all this which is why I was trying to find simpler explanation. Thank you for linking the other post to help others.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 30 Nov 2014
Pam,
I think you should grab a good introductory book on matlab or go through the tutorials in the help and on Mathworks website.
Probably, also what you should do is write down your algorithm in plain words, forgetting about matlab's syntax. What you want to do:
1. Get a word (or is it a full sentence?, your prompt is not clear) from the user
2. For each character in the word,
2a. Convert the character into its morse code equivalent
2b. Add that conversion to some sort of output
3. Display that output
Now, in 2b, since you're adding to some output, there's a step missing to create that output (you can't add to nothing). So:
1b. Create empty output.
You can then convert that to code. 1 is your
Word=input('Please enter a single word:','s');
Word=upper(Word);
1b could be:
WordInMorse = '';
With 2, you should see where you go wrong. I wrote, for each character in the word, so:
for character = Word
2a. is your switch statement (although I would replace that by Adam's answer). It's a character you want to convert, so you should switch based on that:
switch character
case ...
code = ...
...
end
For 2b, you could use:
WordInMorse = [WordInMorse ' ' code];
And for 3,
disp(WordInMorse);
  5 Comments
Guillaume
Guillaume on 1 Dec 2014
If you think about it, on the loop iteration before the invalid character you set a value for Code. Then you go around the loop. Obviously Code is still set. You then go into the switch. Because the character is invalid, you end up in the otherwise where you display the error message. You do not do anything with Code so it stays as it was, still the value of the previous character. You then exit the switch and do your fprintf, so display the previous value.
That also shows that there is a bug in your code. If the first character is invalid, Code is never set, and the fprintf statement will fail.
The simplest way to fix both issues is to exit the for loop prematurely when you hit otherwise, which is done with break:
%...
otherwise
disp('invalid input')
break; %further statements in the loop won't be executed
end
Pam
Pam on 1 Dec 2014
Oh ok thank you and I actually just learned about break today

Sign in to comment.

More Answers (1)

Adam
Adam on 30 Nov 2014
If I were you I would use a map as follows:
keys = {'1'; '2'; '3';...
'X'; 'Y'; 'Z'};
values = {'.----'; '..---'; '...--';...
'-..-'; '-.--'; '--..'; }
morseCodeMap = containers.Map( keys, values );
I know you seem to be saying you want to do it the way you have above, but it is hard for someone to give help if you are only interested in doing it one way. A map is built for exactly this type of purpose and will solve your problem very neatly. Obviously above I abbreviated your alphabet, you would need to put all your morse code values in the keys and all your alphabet in the values, making sure they match up to each other in order.
Then you need no loops or huge switch statements, just the following:
Word=input('Please enter text:','s');
Word=upper(Word);
Code = morseCodeMap( Word );
  4 Comments
Pam
Pam on 30 Nov 2014
I want to accept more than one character now, instead of just inputting 'a' and geting its morsecode, I want to input'hi' and get its morsecode. and yes I have looked thorough the examples to aid me
Guillaume
Guillaume on 30 Nov 2014
Certainly, Pam, once you've sorted out your loop, you should consider Adam's answer. Your implemented a Look-Up Table (LUT). While a switch works, it's a lot of typing (and thus a higher risk of bugs). LUTs can be implemented using just plain arrays or, in this case, easily using a map.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!