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

4 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 8 Oct 2020
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

Sign in to comment.

Answers (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 8 Oct 2020
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)

Tags

Community Treasure Hunt

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

Start Hunting!