Matlab Switch w/ string function

In summary, the code creates a vector x with five elements and then uses it to display the year numbers for each case.
  • #1
ineedhelpnow
651
0
hey hey :)

View attachment 4843

So this is the code i came up with
Code:
%% part 2
  x=[1 2 3 4 5];
  % 1 is freshman
  % 2 is softmore
  % 3 is junior
  % 4 is senior 
  % >4 is super senior
  for i=1:5
      s=input('number: ');
      switch s
          case 1
              yearNumber='freshman';
          case 2
              yearNumber='softmore';
          case 3
              yearNumber='junior';
          case 4
              yearNumber='senior';
          otherwise
              yearNumber='super senior';
      end
      disp(yearNumber)
  end

Needless to say, I have absolutely no idea what I'm doing so I'd really appreciate help :)
 

Attachments

  • 123.png
    123.png
    11.8 KB · Views: 64
Physics news on Phys.org
  • #2
Hi,

It seems that there is no compilation error (I didn't run it, but looks good) but you did something different from what the exercise says.

You can notice that by looking at the fact that you create a vector x=[1 2 3 4 5] but you don't use it any more, so why do you need to create it?

I think the way to solve this is replace the instruction
Code:
s=input('number: ');
by
Code:
s=x(i);
which means that you are taking the number at position $i$ in the vector $x$ and so you are taking one number of the vector at a time, just as the exercise is stated :)
 
  • #3
Thank You! I was confused about where to put that x back in :eek: But how can I get it to also display "number: x " (whatever x may be) for each case?
 
  • #4
Well, I think our code now fits quite good what the exercise says but, if you want to put some text on it, I would do the following.

Inside the for loop and instead of the disp sentence you can write
Code:
fprintf('\n  Number: %8i\n', yearNumber)

frpintf do more or less the same than disp, but have a few more options.

The ' ... ' means that there is an string inside.
\n is for writing in a new line.
And %8i is a declaration of the type of the next argument yearNumber, which in this case may be displayed as an 8 characters integer. (The italics aren't exactly correct, but you can think on it that way)
 
  • #5
Code:
%% part 2
  x=[1 2 3 4 5];
  % 1 is freshman
  % 2 is softmore
  % 3 is junior
  % 4 is senior 
  % >4 is super senior
  for i=1:5
      s=x(i);
      switch s
          case 1
              yearNumber='freshman';
          case 2
              yearNumber='softmore';
          case 3
              yearNumber='junior';
          case 4
              yearNumber='senior';
          otherwise
              yearNumber='super senior';
      end
      fprintf('\n  Number: %8i\n', yearNumber)
  end
Code:
  Number:      102

  Number:      114

  Number:      101

  Number:      115

  Number:      104

  Number:      109

  Number:       97

  Number:      110

  Number:      115

  Number:      111

  Number:      102

  Number:      116

  Number:      109

  Number:      111

  Number:      114

  Number:      101

  Number:      106

  Number:      117

  Number:      110

  Number:      105

  Number:      111

  Number:      114

  Number:      115

  Number:      101

  Number:      110

  Number:      105

  Number:      111

  Number:      114

  Number:      115

  Number:      117

  Number:      112

  Number:      101

  Number:      114

  Number:       32

  Number:      115

  Number:      101

  Number:      110

  Number:      105

  Number:      111

  Number:      114

This is what Matlab gave me
 
  • #6
Oh sorry, I mixed up what was going on.Instead of %8i you should write %s since yearNumber is not an integer, is a string. (So you want to wirte things like Number: Junior ?:confused:)

Anyhow I don't know why you have 10 sentences instead of 5, I don't have Malab right now so I can't run the program.

My recommendation for you is to debug the program and run it step by step in order to see what Matlab is writing on. Try to do it, I will try to come back to this thread when I get Matlab again if you can't solve it.
 

Related to Matlab Switch w/ string function

1. What is the purpose of the "switch" function in Matlab?

The "switch" function in Matlab is used for control flow in a program, similar to an if-else statement. It allows for different actions to be taken based on the value of a variable or expression.

2. How do I use the "switch" function with strings in Matlab?

To use the "switch" function with strings in Matlab, you must first specify the variable or expression that will be evaluated. Then, you can list different cases using the "case" keyword followed by the string you want to match. Finally, include the code you want to execute if that case is matched.

3. Can I have multiple cases for one action in a "switch" statement?

Yes, you can have multiple cases for one action in a "switch" statement by separating them with a comma. For example, "case 'red','blue','green': code to execute;"

4. What happens if none of the cases in a "switch" statement are matched?

If none of the cases in a "switch" statement are matched, the code will branch to the "otherwise" statement, if one is included. If there is no "otherwise" statement, the code will simply continue to the next line of code after the "switch" statement.

5. Can I use the "switch" function with other data types besides strings?

Yes, the "switch" function can be used with other data types in Matlab, such as integers, characters, and logical values. However, each case must match the data type of the variable or expression being evaluated.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
595
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
144
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
764
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top