Displaying a value in a sentence
Afficher commentaires plus anciens
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
Jesus Sanchez
le 18 Juil 2018
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
Guillaume
le 18 Juil 2018
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
le 18 Juil 2018
Or, skip the intermediary variable and just write
fprintf('Your value is %0.1f MPH.\n',y)
Andrew Whyte
le 18 Juil 2018
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
le 18 Juil 2018
Andrew Whyte
le 18 Juil 2018
Guillaume
le 18 Juil 2018
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
le 18 Juil 2018
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...
Réponses (1)
KALYAN ACHARJYA
le 18 Juil 2018
Modifié(e) : KALYAN ACHARJYA
le 30 Juil 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 commentaires
Andrew Whyte
le 18 Juil 2018
KALYAN ACHARJYA
le 18 Juil 2018
floating points numbers
Andrew Whyte
le 18 Juil 2018
KALYAN ACHARJYA
le 30 Juil 2018
Modifié(e) : KALYAN ACHARJYA
le 30 Juil 2018
Welcome
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
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
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!