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.

 채택된 답변

Star Strider
Star Strider 2021년 2월 8일

1 개 추천

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

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.
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

질문:

2021년 2월 8일

댓글:

2021년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by