Comments on: MATLAB Basics: ‘Switch case’ vs. ‘If elseif’ https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/?s_tid=feedtopost Stuart uses video to share his experiences solving problems with MATLAB day-to-day, interesting new features, plus tips and tricks he has picked up along the way. Mon, 04 Apr 2016 20:29:01 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: dhull https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2527 Tue, 18 Jan 2011 16:22:08 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2527 While I do not understand your code, I am convinced that with a combination of IF THEN ELSE, SWITCH CASE, and other flow control in MATLAB that you do not need a GOTO.

Doug

]]>
By: Nimisha Nidhi https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2526 Tue, 18 Jan 2011 13:45:55 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2526 Hi Doug,

I need a substitute for goto statement in Matlab. Is there anything like that?

More precisely, I need to code the following in Matlab:

 1  if Positive_Sentiment<0.169994 then node 2 else node 3
 2  class = Up
 3  if Positive_Sentiment<0.171732 then node 4 else node 5
 4  class = Down
 5  if Negative_Sentiment<0.021006 then node 6 else node 7
 6  class = Up
 7  if Negative_Sentiment<0.02358 then node 8 else node 9
 8  class = Down
 9  if Negative_Sentiment<0.0261051 then node 10 else node 11
10  class = Up
11  if Negative_Sentiment<0.0296845 then node 12 else node 13
12  class = Down
13  class = Up
]]>
By: Steve Eddins https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2030 Thu, 22 Apr 2010 21:17:24 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2030 Michael—That’s an RGB triplet of floating-point values in the range [0,1]. [0,0,0] is black and [1,1,1] is white.

]]>
By: Michael Claughton https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2028 Thu, 22 Apr 2010 18:27:59 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2028 Thanks Steve.That makes alot of sense. I appreciate it, and the matlab community.
For what its worth, I ended up creating a GUI with pop-up menus, to ensure my input was always going to be the same.
I do have another silly little question. I used the get command to see a list of properties of a text box, my interest being the background colour, and I noticed it was in the format [0.XXX,0.XXX,0.XXX]. Do you, or does anyone, know what format that is, and how it relates to HEX codes? Cheers.
PS – I am really starting to enjoy using matlab.

]]>
By: Steve Eddins https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2025 Thu, 22 Apr 2010 11:28:38 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2025 Michael—

switch lower(str)
  case 'string1' % enter all case strings in lower case
    do_this;
  case 'string2'
    do_this;
  otherwise
    et_phone_home;
end

]]>
By: Michael Claughton https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2024 Thu, 22 Apr 2010 03:59:19 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-2024 Doug, with switch, is there a way to implement ignore case? In a string compare, you would use strcmpi, which is fine if you were using an if else, using a switch, how would you implement it?
Thanks in advacnce,
– Michael.

]]>
By: Rick Stauf https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-746 Tue, 29 Jul 2008 00:42:43 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-746 Never Mind, I found a more efficient way to do it. Instead of searching the time area I simply multiply the time by the delta time to acquire the index number in the array. Thanks for your web tutorials. They are very helpful.

]]>
By: Rick Stauf https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-745 Mon, 28 Jul 2008 23:07:45 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-745 Doug,

I’m looking for a GoTo function for Matlab. I have an efficient search routine that I like to use in a number of areas that requires GoTos. Is there anything like this in matlab procedures?

Thank you for your help,
Rick

]]>
By: Doug https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-751 Thu, 27 Mar 2008 13:32:02 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-751 Phillip,

You need this for your input statement:

n=input(‘Please input the figure:’,’s’)

This interprets whatever is typed as a string. Without that switch, MATLAB is taking what you type and interpreting it and thus looks for a function called “triangle” or whatever and does not find it.

Doug

]]>
By: Phillip Diedrick https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-752 Thu, 27 Mar 2008 11:08:01 +0000 https://blogs.mathworks.com/videos/2008/01/02/matlab-basics-switch-case-vs-if-elseif/#comment-752 I want to use the switch/case for the following. I want to prompt the user to input one the following strings: triangle, square,pentagon or hexagon. Depending which figure you input the program will then calculate the sum of the interior angles of the figure using (n-2)(180). I tried the following and I’m prompted with an error message. Here is my code.
clear;
clc;
n=input(‘Please input the figure:’)
switch n
case (‘triangle’)
n=3;
sum=(n-3).*(180)
case (‘square’)
n=4;
sum=(n-3).*(180)
case(‘pentagon’)
n=5;
sum=(n-3).*(180)
case (‘hexagon’)
n=6;
sum=(n-3).*(180)
end
Please could you assist me.
Thanks

]]>