use sprintf while selecting exponential power

7 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2020-9-13
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'
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by