Displaying a value in a sentence

Hello, I am very new to Matlab and I am trying to make a simple FPS to MPH calculator. The code I have now goes as follows:
function [] =Fps_to_Mph()
prompt = 'How fast? (in FPS): ';
x = input(prompt);
y=(x/5280)/0.000277;
end
However, I want the output to say "(value): MPH" and I am not sure how to do that.
Thanks for any help.

10 Comments

Try
res = sprintf('Your value is %s MPH ',sprintf('%0.1f',y));
disp(res);
If you want more decimals, change the 0.1 to the desired number
There's one sprintf too many there:
res = sprintf('Your value is %0.1f MPH', y);
Better name for the variables x and y would be fps and mph respectively, thus not forcing the reader to remember which variable was what.
Or, skip the intermediary variable and just write
fprintf('Your value is %0.1f MPH.\n',y)
That worked perfectly. My only issue is not understanding the need for the (%0.1f and /n) if you know what that is used for I would love to learn.
Thanks for the corrections :)
Andrew, as KALYAN said the number after the % determines the amount of floating numbers that will be used to "transform" the numeric value to a string/char.
On the other hand, the "\n" is to jump to the next line, so if there are another disp() they will not be shown together on the same line on matlab.
Now that I have is so I get my output, is there a way I can have it run again and keep asking without having to press "run"?
When in doubt about what a function does it's always a good idea to look at its documentation. The formatspec section of the documentation of sprintf explains what the % and following symbols mean.
\n (newline) is needed for fprintf as otherwise whatever is printed next will be on the same line. It's not necessary if disp is used as disp automatically insert newlines at the end of its output.
As for running it again, simply wrap your code in a loop.
while true %never ending loop
%your code
end
Works great, thanks!
"\n (newline) is needed for fprintf ..."
Yeah, would have been better to written
disp('Your value is %0.1f MPH.',y)
but I was just editing in-place...

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 18 Jul 2018
Edited: KALYAN ACHARJYA on 30 Jul 2018
%If you want to display y
fprintf('The value is %.2f MPH \n',y);
%here 2 means up to 2 floating points, if you need more for an exact value, change the value

6 Comments

Thank you very much. What is the meaning of the F after the %.2 as well as the \n. I am new to Matlab and would like to learn as much as I can.
floating points numbers
Thank you!
KALYAN ACHARJYA
KALYAN ACHARJYA on 30 Jul 2018
Edited: KALYAN ACHARJYA on 30 Jul 2018
Welcome
To clarify, the 2 in %.2f means two places to the right of the decimal points, and however many places to the left of the decimal place are needed. One floating point number, y, will be written into the string.
KALYAN ACHARJYA
KALYAN ACHARJYA on 30 Jul 2018
Edited: KALYAN ACHARJYA on 30 Jul 2018
True sir, I mistakenly mentioned the numbers, it should be up to two points. Thanks for the correction. I edited the comment

Sign in to comment.

Categories

Products

Asked:

on 18 Jul 2018

Edited:

on 30 Jul 2018

Community Treasure Hunt

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

Start Hunting!