Displaying a value in a sentence
Show older comments
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
Jesus Sanchez
on 18 Jul 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
on 18 Jul 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
on 18 Jul 2018
Or, skip the intermediary variable and just write
fprintf('Your value is %0.1f MPH.\n',y)
Andrew Whyte
on 18 Jul 2018
Jesus Sanchez
on 18 Jul 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
on 18 Jul 2018
Andrew Whyte
on 18 Jul 2018
Guillaume
on 18 Jul 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
on 18 Jul 2018
dpb
on 18 Jul 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...
Answers (1)
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
Andrew Whyte
on 18 Jul 2018
KALYAN ACHARJYA
on 18 Jul 2018
floating points numbers
Andrew Whyte
on 18 Jul 2018
KALYAN ACHARJYA
on 30 Jul 2018
Edited: KALYAN ACHARJYA
on 30 Jul 2018
Welcome
Image Analyst
on 30 Jul 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
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
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!