Format colorbar using engineering notation

18 ビュー (過去 30 日間)
Peter
Peter 2019 年 11 月 19 日
回答済み: Harry Dymond 2020 年 7 月 3 日
How can I set the scale of a color bar to use exponential notation, with all exponents being a multiple of 3?
I know that format shortEng will set the format I want for values printed to the Command Window, but I cannot find anything to format string printed elsewhere using this format.

採用された回答

Star Strider
Star Strider 2019 年 11 月 19 日
Try this:
engstr = @(x) [x(:).*10.^(-3*floor(log10(abs(x(:)))/3)) 3*floor(log10(abs(x(:)))/3)];
Q1 = logspace(-3, 3, 7);
Result2 = sprintfc('%.4fe%+04d', engstr(Q1))
Alternatively, use compose instead of sprintfc to create the tick labels for the color bar.
Experiment with the format string to get the result you want. This is just an example.
  2 件のコメント
Peter
Peter 2019 年 11 月 19 日
This is certainly an improvement. Is there a way to pass these formated strings back to the colorbar so that the order of magnitude is displayed once for the entire scale, instead of for each individual tick? I'd like the exponent at the top of the bar, the same as it is before changing the exponents.
Here is how I am using your formating currently:
engstr = @(x) [x(:).*10.^(-3*floor(log10(abs(x(:)))/3)) 3*floor(log10(abs(x(:)))/3)];
defaultTickVals=reshape(get(hColorBar, 'YTick'),[],1)
formatedTickVals=sprintfc('%.4fE%g', engstr(a))
set(hColorBar, 'YTickLabel', cellstr(b))
Star Strider
Star Strider 2019 年 11 月 19 日
I am not certain what you want. With Numeric Ruler Properties an Exponent option exists, however that does not apply to cololrbar objects. When I did that experiment just now:
figure
surf(rand(20))
hcb = colorbar;
hcb.Exponent = 3;
it threw this error:
Unrecognized property 'Exponent' for class 'matlab.graphics.illustration.ColorBar'.
It seems that doing the tick labels individually is the only option.
The only other option I can imagine is to just use the first column that ‘engstr’ returns, and then use a text object or annotation object to display the common exponent. This would likely be fragile code. I have never used text objects or annotation objects with colorbar objects.

サインインしてコメントする。

その他の回答 (1 件)

Harry Dymond
Harry Dymond 2020 年 7 月 3 日
A little late for the OP perhaps, but:
  1. My num2eng FEX submission will process colorbars to update the tick labels, and keep them updated if the data in the associated axes change. Just pass the handle of the colorbar to num2eng.
  2. It sounds like, from your comment to Star Strider, that this isn't actually what you want. You want to have a "global" exponent for the colorbar, and for this to be a multiple of three. For this, you need to use the colorbar's hidden .Ruler.Exponent property. e.g.:
barH = colorbar; barH.Ruler.Exponent = 9;

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by