Switch case and a Menu Command

63 views (last 30 days)
Taylor
Taylor on 30 Nov 2012
Answered: R__S on 1 Mar 2017
I'm trying to create a code that calculates an output in one unit but displays the answer in six different unit (the answer is converted to different units). I have a menu command so that the user can choose from the six different units but I need a switch case as my logical structure. I'm completely lost on how to write the switch case (I'm very, very new to matlab, so forgive my ignorance).
choice = menu('Choose a unit','in','ft','yd','m','cm','mm');
R = input('Input a value for the radius:');
result = (pi*R^2);
switch choice
case 'in'
result = pi*R^2;
case 'ft'
result = result/12;
case 'yd'
result = result/36;
case 'm'
result = result * .0254;
case 'cm'
result = result * 2.54;
case 'mm'
result = result * 25.4;
end
This was my first attempt that failed miserably, so any help would be appreciated.
  1 Comment
Randy Souza
Randy Souza on 3 Dec 2012
I have restored the original text of this question.
Taylor, this question has a clear subject and an answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.
If you solved the problem on your own, please add your solution as an answer and accept it.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 30 Nov 2012
menu() returns the number of the item chosen. So instead of case 'in', use case 1, and instead of case 'ft' use case 2, and so on.

R__S
R__S on 1 Mar 2017
I am not 100% sure it this is what you were wanting, but I assumed you wanted to be able to enter one value in a specific unit and get out the values for all the other units. If that is what you wanted feel free to use this code as you please.
clc, clear %clears the command window and variables
Unit = menu('Choose a unit','in','ft','yd','m','cm','mm'); %pop up display
R = input('Input a value for the radius:');
clc%clears the command screen after imputing a radius value
switch Unit %switich is the command, unit is the vaiable that we want to use
case 1 %given inches %case must be given as a number, starting with 1=first option, 2=second option, and so on
in = pi*R.^2; %finds the area of a circle in inches
ft = in*.00694444; %takes the area in inches and coverts it into feet
yds = in*.000771605;%same idea as before
m = in*.00064516;
cm = in*6.4516;
mm = in*645.16;
fprintf('\nA circle with a %3.2f Inch radius has an area of %5.10f Inches squared\n',R,in) %displays the Radius in the origional units and the new units
fprintf('A circle with a %3.2f Inch radius has an area of %5.10f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Inch radius has an area of %5.10f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Inch radius has an area of %5.10f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Inch radius has an area of %5.10f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Inch radius has an area of %5.10f Millimeters squared\n',R,mm)
case 2 %given feet
ft = pi*(R.^2) ;
in = ft*144;
yds = ft*.111111;
m = ft* 9;
cm = ft* 64516;
mm = ft*836127;
fprintf('\nA circle with a %3.2f Foot radius has an area of %5.10f Inches squared\n',R,in)
fprintf('A circle with a %3.2f Foot radius has an area of %5.10f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Foot radius has an area of %5.10f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Foot radius has an area of %5.10f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Foot radius has an area of %5.10f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Foot radius has an area of %5.10f Millimeters squared\n',R,mm)
case 3 %given yards
yds = pi*R.^2;
ft = yds*9;
in = yds*1296;
m = yds*.836127;
cm = yds*8361.27;
mm = yds*836127;
fprintf('\nA circle with a %3.2f Yard radius has an area of %5.10f Inches squared\n',R,in)
fprintf('A circle with a %3.2f Yard radius has an area of %5.10f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Yard radius has an area of %5.10f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Yard radius has an area of %5.10f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Yard radius has an area of %5.10f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Yard radius has an area of %5.10f Millimeters squared\n',R,mm)
case 4 %given meter
m = pi*R.^2;
ft = m*10.7639;
yds = m*1.19599;
in = m*1550;
cm = m*10000;
mm = m*1000000;
fprintf('\nA circle with a %3.2f Meter radius has an area of %5.5f Inches squared\n',R,in)
fprintf('A circle with a %3.2f Meter radius has an area of %5.5f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Meter radius has an area of %5.5f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Meter radius has an area of %5.5f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Meter radius has an area of %5.5f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Meter radius has an area of %5.5f Millimeters squared\n',R,mm)
case 5 %given centimeters
cm = pi*R.^2;
ft = cm*.00107639;
yds = cm*.000119599;
m = cm*1000;
in = cm*.155;
mm = cm*100;
fprintf('\nA circle with a %3.2f Centimeter radius has an area of %5.10f Inches squared\n',R,in)
fprintf('A circle with a %3.2f Centimeter radius has an area of %5.10f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Centimeter radius has an area of %5.10f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Centimeter radius has an area of %5.10f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Centimeter radius has an area of %5.10f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Centimeter radius has an area of %5.10f Millimeters squared\n',R,mm)
case 6 %given millimeters
mm = pi*R.^2;
ft = mm*.000010764;
yds = mm*.000001196;
m = mm*.000001;
cm = mm*.01;
in = mm*.00155;
fprintf('\nA circle with a %3.2f Millimeter radius has an area of %5.10f Inches squared\n',R,in)
fprintf('A circle with a %3.2f Millimeter radius has an area of %5.10f Feet squared\n',R,ft)
fprintf('A circle with a %3.2f Millimeter radius has an area of %5.10f Yards squared\n',R,yds)
fprintf('A circle with a %3.2f Millimeter radius has an area of %5.10f Meters squared\n',R,m)
fprintf('A circle with a %3.2f Millimeter radius has an area of %5.10f Centimeters squared\n',R,cm)
fprintf('A circle with a %3.2f Millimeter radius has an area of %5.10f Millimeters squared\n',R,mm)
otherwise
disp('Something went wrong')
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!