Changing scientific notation on plot

7 ビュー (過去 30 日間)
Colby
Colby 2015 年 4 月 22 日
コメント済み: Mike Garrity 2016 年 2 月 19 日
Hello, I'm having issues changing the default scientific notation used on some plots I'm making. I've tried the following lines of code. yt=get(ax(2), 'ytick');
set(ax(2), 'YTickLabel',sprintf('%.0d',yt));
When I use these lines, it plots every tick value for that axis, at every tick on that axis. Does anyone know what I'm doing wrong? I can't seem to figure it out. Attached is an image of the plot after the above two lines of code are executed.
<<
>>
Thanks for your time

採用された回答

Guillaume
Guillaume 2015 年 4 月 22 日
編集済み: Guillaume 2015 年 4 月 22 日
set(ax(2), 'YTickLabel', arrayfun(@(v) sprintf('%.0d', v), yt, 'UniformOutput', false));
should work. Your sprintf call returns a single string that is the concatenation of all your tick values, whereas YTickLabel expects a cell array of strings.
  2 件のコメント
Colby
Colby 2015 年 4 月 22 日
編集済み: Guillaume 2015 年 4 月 22 日
Excellent! This works very well for my situation!
Thank you for your time and expertise.
Just incase someone in the future tries to replicate these results, note that the above code is missing quotation and a parenthesis. The same code with the typos corrected is below.
set(ax(2), 'YTickLabel', arrayfun(@(v) sprintf('%.0d', v), yt, 'UniformOutput', false));
Thanks again
Guillaume
Guillaume 2015 年 4 月 22 日
Yes, sorry about the typos. Fixed now.

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

その他の回答 (2 件)

Stephen23
Stephen23 2015 年 4 月 22 日
編集済み: Stephen23 2015 年 4 月 22 日
You need to provide a cell array of strings.
The output of sprintf is a simple string, so MATLAB repeats this string for each tick mark. If you want to define each string uniquely, then supply them in a cell array. The axes properties documentation states this clearly: "Tick mark labels, specified as a cell array of strings. If you do not specify enough strings for all the ticks marks, then the axes cycles through the specified strings"
If yt is a numeric vector (which it should be!), then this will convert those values to a cell array of strings:
arrayfun(@(n)sprintf('%.0d',n), yt, 'UniformOutput',false)

hongyu zhai
hongyu zhai 2016 年 2 月 19 日
編集済み: Stephen23 2016 年 2 月 19 日
There is a simple way to change the Axis scientific notation by change the basic property of the XAxis or YAxis.
These two example will help you.
  1 件のコメント
Mike Garrity
Mike Garrity 2016 年 2 月 19 日
We should note that this is a feature that was introduced in R2015b. It is a lot nicer than messing around with the TickLabels by hand as seen above, but you won't be able to use it with earlier versions of MATLAB.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by