MATLAB Answers


Switch case and a Menu Command

Asked by Taylor
on 30 Nov 2012

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;

This was my first attempt that failed miserably, so any help would be appreciated.

  1 Comment

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.


No products are associated with this question.

1 Answer

Answer by 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.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!