MATLAB Matlab Switch w/ string function

AI Thread Summary
The discussion centers on a MATLAB code snippet designed to categorize students by their academic year based on numerical input. Initially, the user creates a vector representing different academic years but does not utilize it effectively. Feedback suggests replacing the user input command with an indexed value from the vector to streamline the code. The conversation also highlights the need to format the output correctly using `fprintf` instead of `disp`, emphasizing the importance of using the correct format specifier for strings. A mistake in the output format is identified, where an integer specifier is incorrectly used for a string variable. The user is encouraged to debug the code step by step to understand the output better. Overall, the discussion focuses on improving code efficiency and output formatting in MATLAB.
ineedhelpnow
Messages
649
Reaction score
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: 91
Physics news on Phys.org
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 :)
 
Thank You! I was confused about where to put that x back in :o But how can I get it to also display "number: x " (whatever x may be) for each case?
 
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)
 
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
 
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.
 

Similar threads

Replies
4
Views
1K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
8
Views
3K
Back
Top