displaying a description and variable value in the same line

my knowledge isnt extensive in matlab, still a new user.
what i am trying to accomplish is displaying a variables value after a description.
what i want to display:
description : value
ex.
the highest point of the mountain is : 4563 ft
the variable hp = 4563 is already stored into the variables. (hp is the variable name for highest point)

답변 (2개)

Image Analyst
Image Analyst 2012년 11월 4일
편집: Image Analyst 2012년 11월 4일
Try this:
fprintf('The highest point of the mountain is : %d feet.\n', hp);
Just like with the C language, if you're familiar with that. Or, if hp is a floating point number instead of an integer, use %f instead of %d:
fprintf('The highest point of the mountain is : %.2f feet.\n', hp);

댓글 수: 6

Tyler
Tyler 2012년 11월 4일
편집: Tyler 2012년 11월 4일
ok thanks, that worked wonderfully except there are 2 more variables, so 3 of what you stated:
fprintf('The highest point of the mountain is : %d feet.\n', hp);
only thing is it puts all 3 on the same line:
The highest point of the mountain is : 4563 feetThe angle of.... and so on
i need "The angle of...." to display on the next line rather than having all on one line in essentially a run-on sentence if you get what i mean.
in the script i have it:
hp = 4653; fprintf('the highest point of the mountain.....)"
ang = 76; fprintf('the angle of the slope is.....)"
v0 = 150; fprintf('the initial velocity is.....)"
and it displays:
the highest point of the mountain is : 4563 feetThe angle....... 76 degreesThe initial.......
as you see, the are all diplayed in one line of text.
hp = 4653;
ang = 76;
v0 = 150;
fprintf(['\n\nThe highest point of the mountain is %i.\n',...
'The angle of the slope is %i.\n',...
'The initial velocity is %i.\n'],hp,ang,v0)
Can you do this, but perform math within the function? for example I defined x already, and I want to display exp(x).
I tried fprintf('For x=-20 the true value of e^x is: %f\n', exp(x)); and it just returned 0.000000.
Im assuming I just have to first define exp(x) as a variable first, but just checking.
Ben, that should work if you put enough significant digits:
x = -20;
y = exp(x)
fprintf('For x=-20 the true value of e^x is: %.22f\n', exp(x));
You'll see
y =
2.06115362243856e-09
For x=-20 the true value of e^x is: 0.0000000020611536224386
or try
fprintf('For x=-20 the true value of e^x is: %g\n', exp(x));
For x=-20 the true value of e^x is: 2.06115e-09
That is the correct behavior. What is exp(-20)?
y = exp(-20)
y = 2.0612e-09
That's a pretty small number. How does that number display using the %f format inside fprintf? How would it display using a different format? Or what if you modify the format to show more decimal places?
x = -20;
fprintf('For x=-20 the true value of e^x is: %f\n', exp(x));
For x=-20 the true value of e^x is: 0.000000
fprintf('For x=-20 the true value of e^x is: %g\n', exp(x));
For x=-20 the true value of e^x is: 2.06115e-09
fprintf('For x=-20 the true value of e^x is: %.10f\n', exp(x));
For x=-20 the true value of e^x is: 0.0000000021

댓글을 달려면 로그인하십시오.

Tyler
Tyler 2012년 11월 4일

0 개 추천

nevermind, i got it.
i had the \ mixed up with the / so the next line wasnt being created

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 11월 4일

댓글:

2021년 1월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by