Switch-case In Matlab Programming With Examples

‘switch-case-otherwise’ is a very useful command in Matlab® to create conditional programs. For a situation that must be coded in Matlab®, there can be various kinds of cases that needed to have different statements in Matlab® code. In these cases, the use of a ‘switch-case’ statement can be very useful. Take a look at the examples below about the use of the ‘switch-case’ command and its syntax in Matlab®.

MATLAB For Beginners: A Gentle Approach

If you are interested to learn MATLAB® at an engineering level, click on the given link or the ‘Shop Now’ button to check the recommended book by Mechanicalland, from Amazon!

How To Use ‘switch-case’ In Matlab®?

>> a = input('Enter the mark that you took from exam(0-10):');
switch(a)
    case{0, 1, 2}
        disp('E')
   case{3,4}
        disp('D')
    case{5,6}
        disp('C')
    case{7,8}
        disp('B')
    case{9,10}
        disp('A')
    otherwise
        disp('You have entered an invalid value.')
end
Enter the mark that you took from exam(0-10):5
C
>> 

There is a code that includes the use of ‘switch-case’ which is executed in the Matlab® command window. With the input() command,we are taking the examination mark from users.

After that, we used switch-case to corresponding letter grade to the user. Inside the ‘switch()’ command, we put the variable ‘a’ inside parentheses. Beneath the ‘switch’ command, we typed all the cases. For example, for the first case, if the user enters 0, 1, or 2, the program will display the letter grade ‘E’. So, we are typing the values of ‘a’ inside curly brackets of ‘cases’ commands.

At the end of ‘cases’, we put a command called ‘otherwise’ inside which we can put a code if the user enters a value for ‘a’ which is not inside any of the cases.

This is another example to understand the use of the ‘switch-case-otherwise’ command in Matlab®.

>> a = input('Enter the degree of angle:');
switch fix(a/90)
    case 0
        disp('Angle is in the first area of trigonometric zone.')
    case 1
        disp('Angle is in the second area of trigonometric zone.')
    case 2
        disp('Angle is in the third area of trigonometric zone.')
    case 3
        disp('Angle is in the fourth area of trigonometric zone.')
    otherwise
        disp('You have entered an invalid value.')
end
Enter the degree of angle:45
Angle is in the first area of trigonometric zone.
>> 

In this example executed in Matlab®, we take a degree of angle from the user with input() command, But there is a difference in the use of switch-else use in this example.

If you look at the use of the switch, fix() is used to round the angle that is taken from the user. We divided the angle ‘a’ with 90 to obtain the zone. Then we round this number to the closest minimum number.

YOU CAN LEARN Matlab® IN MECHANICAL BASE; Click And Start To Learn Matlab®!

Also, the uses of ‘cases’ are different. We used direct numbers after ‘cases’ which represent the cases of written mathematical expression on ‘switch’.

For example, we typed ’45’ for angle degree at the command window, and the result is the first area of the trigonometrical zone.

Conclusion For ‘switch-case’ Command In Matlab®

So, the ‘switch-case’ command is very useful in Matlab®.

If you want further programming examples that include ‘switch-case’ in Matlab, inform us in the comments below.

Do not forget to leave your comments and questions about the use of ‘switch-case’ in Matlab® below.

Your precious feedbacks are very important for us.


Posted

in

by

Comments


Leave a Reply

Your email address will not be published. Required fields are marked *