Simple Unit Conversion, Undefined variable Error

I'm trying to write a function that should convert inch, feet, mm and cm to meters. Code is below;
function MetricWorks
x= input('Enter the Input Distance: ');
y= input('Enter the input Units(cm,mm,in,ft): ');
in=x*0.0254;
ft=x*0.3048;
cm=x*0.01;
mm=x*0.001;
switch y
case 'in'
fprintf('%0.1f inches is equal to %0.4f m \n',x,in)
case 'feet'
fprintf('%0.1f feet is equal to %0.4f m \n',x,ft)
case 'cm'
fprintf('%0.1f cm is equal to %0.4f m \n',x,cm)
case 'mm'
fprintf('%0.1f mm is equal to %0.4f m \n',x,mm)
endswitch
end
As you can guess it gives 'Undefined function or variable 'in'.' error. I don't know if there are any other possible errors after that.
Thanks in advance.

 Respuesta aceptada

Star Strider
Star Strider el 8 de Feb. de 2021
The ‘y’ response needs to be a string (actually, character vector):
y = input('Enter the input Units(cm,mm,in,ft): ','s');
See the input documentation section on Request Unprocessed Text Input.

2 comentarios

Ferhat Bastug
Ferhat Bastug el 8 de Feb. de 2021
It works perfectly now thanks to you, I guess all I had to do was string but I never knew about it before, thanks again.
Star Strider
Star Strider el 8 de Feb. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 8 de Feb. de 2021

Comentada:

el 8 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by