How to change precision of variable output

14 views (last 30 days)
I have an array with 3 entries. When I print the array values, I get the full precision, down to several decimal points. When I view it in the Workspace, I see to only 4 decimal places. That would be fine, however;
I am plotting a graph, and want to add a value to the legend. I have done so using:
legend(['b=' num2str(Gauss(2))]
But it only displays the values as they are seen in the array, to 4 decimal places.
If I view the value using Gauss(2) in the command window, it prints 50.75625. The workspace shows the value as 50.7563 and so then the legend does too.
How can I make the legend show the FULL values?
Thank you

Answers (1)

Stephen23
Stephen23 on 3 Mar 2016
Edited: Stephen23 on 3 Mar 2016
Read about sprintf, it gives you lots of control over how numbers are displayed:
legend(sprintf('b=%.5f',Gauss(2)))
sprintf has many options, so read the documentation carefully.
  2 Comments
Steven Lord
Steven Lord on 3 Mar 2016
If you just want to change the number of displayed decimal places, NUM2STR can do that without the need to use a *PRINTF format string, by specifying a number as the second input. NUM2STR can also accept a *PRINTF format string as well.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!