# What is the process in converting 45 mps to mph on matlab?

조회 수: 1(최근 30일)
A 2020년 10월 8일
댓글: Walter Roberson 2020년 10월 8일
I need help in converting 45 mps to mph on matlab my code is not going through?
x=input('45');
y=x.*3.6;
printf ('kilometer per second %d\n',y)
z=x.*2.23694;
fprintf ('miles per hour %d\n',z)
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Walter Roberson 2020년 10월 8일
input('45') does not mean that the value 45.0 is to be returned to the program. input('some character vector') means that the text inside the character vector is to be displayed and the user is expected to type in some input.
That input that the user typed is received as a character vector, and that character vector is eval()'d. If the user just entered a plain number then the result of the eval() is the numeric value. If the user entered an expression such as 1+2+3+4+5+6+7+8+9 then because of the eval() that expression will be computed just like the expression had been typed in at the MATLAB command line. The result of evaluating whatever the user typed is returned from input(); in the case of your code, it would then be assigned to the variable x .
In MATLAB, if you want to have a default value that is substituted if the user just presses return in response to the input prompt, then you do not put the value in the character vector that is the prompt. Instead you would need to do something like
x = input('enter mph value ');
if isempty(x)
x = 45; %substitute 45 if the user entered nothing
end

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

### 답변(1개)

x = input('MPH value? ');
% check 3.6? is it correct?
y= x*3.6;
printf ('kilometer per second %f\n',y)
z=x.*2.23694;
fprintf ('miles per hour %f\n',z)

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

### Community Treasure Hunt

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

Start Hunting!

Translated by