displaying a description and variable value in the same line
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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)
0 comentarios
Respuestas (2)
Image Analyst
el 4 de Nov. de 2012
Editada: Image Analyst
el 4 de Nov. de 2012
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 comentarios
Image Analyst
el 17 de En. de 2021
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
Steven Lord
el 17 de En. de 2021
That is the correct behavior. What is exp(-20)?
y = exp(-20)
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));
fprintf('For x=-20 the true value of e^x is: %g\n', exp(x));
fprintf('For x=-20 the true value of e^x is: %.10f\n', exp(x));
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!