green
green

Reputation: 713

How to display output according to initial format set in matlab?

for instance I had set the display format of a program at the very beginning such as format shortG and then I would like to use the fprintf command to display a certain values.

For example I have

x = 1.23456789

y = 12.3456789

If under format shortG it will show x = 1.2346 and y = 12.346. My question is what format I should use when using the fprintf because like if using %.4f y will equal to 12.3457 which is not what I want. Thanks for every single help.

Upvotes: 0

Views: 63

Answers (1)

yuk
yuk

Reputation: 19870

Use g instead of f:

>> fprintf('%.5g\n',y)
12.346
>> fprintf('%.5g\n',x)
1.2346

Upvotes: 2

Related Questions