## MATLAB switch syntax

*switch expression
case
case
otherwise
end*

## MATLAB switch description and examples

The conditional switch statement controls program execution by jumping to a case from a list of case options. The control of this jump is defined by a variable contained in the switch argument and by a predefined constant in each case. If this constant is not predefined in any cases, the switch statement jumps to the “otherwise” option. Once the arguments in the case have been executed, the conditional switch is released, and the program continues its execution from “end”. This conditional switch does not accept relational operators when parsing variables.

## How to use the conditional switch to control program flow in MATLAB.

In this simple example, we create a console application and enter a value via prompt() to direct the program flow to the case number we selected.

In all cases of conditional change, the disp() function is called, which displays the message “** You chose the case**” in the command console, followed by the number of the case we selected and showed you the result of a simple MATLAB function. If the value entered into the variable “a” via the console does not correspond to a previously specified constant in the cases, the switch forwards the jump to the option “otherwise”, where it displays the following message:

**: followed by its value. The objective of this example is to understand conditional change better. To this end, we will set breakpoints on the first line of each case, which will stop the program’s execution and allow us to see in real-time where the execution of the program is being directed.**

*“a” out of scale, “a” is*Breakpoints are a highly recommended tool in the programming task to debug this type of conditionals.

Create a script and paste the following code. To exit the application, press Ctrl+c.

prompt = 'Enter a value and press enterâ€¦';

a = input (prompt);

switch a

case 1

disp(['You chose the case ',num2str(a),' little magic square for you']);

you= magic(3)

case 2

disp(['You chose the case ',num2str(a),' an array of zeros for you']);

you= zeros(5)

case 3

disp(['You chose the case ',num2str(a),' an array of ones for you']);

you= ones(5)

case 4

disp(['You chose the case ',num2str(a),' a magic square for you']);

you= magic(7)

otherwise

disp(['â€ťaâ€ť out of scale, â€śaâ€ť is: ', num2str(a)])

end

end

Run the script, and from the command console, enter different values in “a” to direct the flow of the program towards the different cases

## How to use the conditional switch to make a unit of measure converter in MATLAB

In the following example, we will make a console application in MATLAB where we will use the conditional switch to convert units of measurement. This application will display a list of items with conversion options.

Using the prompt() function, we enter the number of the selected item, which is stored in the “item” variable, and the value we want to convert, which is stored in the “value” variable.

The variable “element” will be the argument of the conditional switch that will direct the program to the case that matches the element number or conversion unit selected by the user.

case 1

Cm to inch

case 2

Inch to cm

case 3

Km to Mi

case 4

Mi to km

otherwise

Item not recognized

end

In each case, the mathematical operation to carry out the conversion will be applied to the value stored in “value”. We will create a string with a message to the user that will show the result after the program is released from the conditional switch. We will also use the “otherwise” option to inform the user in case of selecting an item that is not recognized.

Create a script, paste this code and press run. To exit the application, press Ctrl+c.

clc();

disp 'Unit converter in MATLAB. Exit press Ctrl+c.'

disp 'Cm to Inch [1]';

disp 'Inch to cm [2]';

disp 'Km to Mi [3]';

disp 'Mi to Km [4]';

prompt = 'Select conversion unit';

item = input (prompt);

prompt = 'Enter the value for the conversion';

value = input (prompt);

switch item

case 1

x = value.* 0.3937;

out= ([num2str(value),' Cm are equivalent to ' ,num2str(x), ' Inch']);

case 2

x = value.* 2.54;

out= ([num2str(value), ' Inch are equivalent to ' ,num2str(x), ' Cm.']);

case 3

x = value.* 0.6214;

out= ([num2str(value), ' Km are equivalent to ' ,num2str(x), ' Mi']);

case 4

x = value.* 1.6093;

out= ([num2str(value), ' Mi are equivalent to ' ,num2str(x),' Km']);

otherwise

disp 'The conversion item is not recognized '

end

disp (out);

prompt = 'Press enter to continueâ€¦';

input (prompt);

end

Once we have selected the conversion type and value, the application displays the result in the unit of measurement we have chosen.

## How to use the switch conditional on getting the correct process in a square root operation.

For square root calculation, MATLAB has three different functions, incorrect use of these can cause inaccurate results or unexpected error messages.

To avoid this, it is necessary to perform the operation with the correct function, depending on the type of data whose square root is determined. In this example, we will create a simple console application using the switch conditional on performing the square root operation with the most appropriate function depending on the type and sign.

The scalar “a” will be the variable from which we want to obtain the square root, and we will enter its value and sign using the prompt() function. To get the sign of the value in “a”, we will use the sign() function, which will return a 1 in “s” if the value of “a” is positive, 0 if the value is 0, and -1 if it is negative. The variable ” s” is sent to the conditional switch

Create a script and paste the following code. To exit the application, press Ctrl+c.

prompt ='Enter a positive or negative sign value and press enter...';

a = input (prompt);

s= sign(a);

switch s

case 0

disp( 'The scalar "a" is 0 and does not have a square root ' )

case 1

r= realsqrt(a)

disp( [ 'The scalar "a" has a positive sign. its square root is: ' , num2str(r)])

case -1

r=sqrt(a)

disp( [ 'The scalar "a" has a negative sign. its square root is: ' , num2str(r)])

end

end

Run the script and enter various positive and negative sign values in the command console to get the results.

In this way, the conditional switch directed the square root operation to the correct function depending on the sign of the value of “a”.

## Conclusion:

In this article, the use of the conditional jump function switch in MATLAB has been explained, and some practical examples have been given to demonstrate the use of this function. The argument types supported by this conditional function and the accepted data types have also been explained in detail. We hope that you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.