use sprintf while selecting exponential power

7 views (last 30 days)
Hi, I am using sprintf to grab a vallue and print in a legend. They all vary with power, but i want them on the same scale. IE:
legend({sprintf('Sample A %.3E',P1),sprintf('Sample B %0.3E',P2})
Where P1 is 2.331E6 and P2 is 3.215E5, resulting in
Sample A 2.331E6 and Sample b 3.215E5
I would like them to (note exponent)
Sample A 2.331E6 and Sample b 0.321E6

Accepted Answer

Star Strider
Star Strider on 13 Sep 2020
I wrote a utility function a while back to do just that:
rfx = @(x,xpnt) [sign(x).*10.^(log10(abs(x))-xpnt), xpnt]; % Anonymous Function Creating Reformatted Number
P1 = 2.331E6;
P2 = 3.215E5;
xpnt = +6;
Out1 = sprintf('Sample A %.7fE%+d', rfx(P1,xpnt))
Out2 = sprintf('Sample A %.7fE%+d', rfx(P2,xpnt))
producing:
Out1 =
'Sample A 2.3310000E+6'
Out2 =
'Sample A 0.3215000E+6'
.
  2 Comments
Star Strider
Star Strider on 13 Sep 2020
I very much appreciate your compliment!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!