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 commentaires

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.
dpb
dpb le 18 Juil 2018
Or, skip the intermediary variable and just write
fprintf('Your value is %0.1f MPH.\n',y)
Andrew Whyte
Andrew Whyte le 18 Juil 2018
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.
Jesus Sanchez
Jesus Sanchez le 18 Juil 2018
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.
Andrew Whyte
Andrew Whyte le 18 Juil 2018
Thanks!
Andrew Whyte
Andrew Whyte le 18 Juil 2018
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
Andrew Whyte
Andrew Whyte le 18 Juil 2018
Works great, thanks!
dpb
dpb le 18 Juil 2018
"\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...

Connectez-vous pour commenter.

Réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 18 Juil 2018
Modifié(e) : KALYAN ACHARJYA le 30 Juil 2018

0 votes

%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 commentaires

Andrew Whyte
Andrew Whyte le 18 Juil 2018
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.
KALYAN ACHARJYA
KALYAN ACHARJYA le 18 Juil 2018
floating points numbers
Andrew Whyte
Andrew Whyte le 18 Juil 2018
Thank you!
KALYAN ACHARJYA
KALYAN ACHARJYA le 30 Juil 2018
Modifié(e) : KALYAN ACHARJYA le 30 Juil 2018
Welcome
Image Analyst
Image Analyst le 30 Juil 2018
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 le 30 Juil 2018
Modifié(e) : KALYAN ACHARJYA le 30 Juil 2018
True sir, I mistakenly mentioned the numbers, it should be up to two points. Thanks for the correction. I edited the comment

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by