how do i keep the format of xticklabels?

1 回表示 (過去 30 日間)
Odin Iversen
Odin Iversen 2020 年 3 月 30 日
コメント済み: Star Strider 2020 年 3 月 31 日
xticks(0:6)
xticklabels(10.^(xticks))
10.^(xticks)
xticklabels
10.^(xticks) returns:
ans =
1 10 100 1000 10000 100000 1000000
but from xticklabels i get:
ans =
7×1 cell array
{'1' }
{'10' }
{'100' }
{'1000' }
{'10000' }
{'100000'}
{'1e+06' }
how do i keep the format so that it keeps the last label as '1000000' and not be converted to '1e+06'.
this is part of a bigger script, wich tests for max an min values of the plots. so manually writing all the labels is not really an optin.
  2 件のコメント
Tommy
Tommy 2020 年 3 月 30 日
Hi! See if
xticks(0:6)
xticklabels(num2str(10.^xticks'))
works.
Odin Iversen
Odin Iversen 2020 年 3 月 31 日
here you can se italy done the way you said, and the world plot is done without the num2str. stange.

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

採用された回答

Star Strider
Star Strider 2020 年 3 月 30 日
編集済み: Star Strider 2020 年 3 月 31 日
Use compose or sprintfc to create the cell array of labels:
figure
plot(0:6, rand(1,7))
xtl = sprintfc('%d',10.^(0:6));
xtl = compose('%d',10.^(0:6));
Ax = gca;
Ax.XTickLabel = xtl;
The ‘xtl’ variable is the same for both, so choose one. (The sprintfc function is undocumented, however everyone has it. Not everyone may have the compose function.)
EDIT — (31 Mar 2020 at 18:00)
Try this:
F = openfig('test.fig');
Kids = F.Children;
Ax = findobj(Kids, 'Type','Axes');
Ax(2).XTickLabel = sprintfc('%d',10.^(0:6));
  2 件のコメント
Odin Iversen
Odin Iversen 2020 年 3 月 31 日
Thank you so much! this worked perfect.
do you know if this has anything similar to xtickformat('%,4.4g')
wich inserts a comma every three digits, so it becomes 1,000,000.
I cant get xtickformat to work in combination with xtick, or xticklabel. it just keeps the original values and intervals for the ticks and labels.
if there is no built in function for this i can make one my self.
Star Strider
Star Strider 2020 年 3 月 31 日
As always, my pleasure!
The xtickformat function appears to default to exponential notation, regardless of the format descriptor that I provide for it. The sprintfc call (or compose, that will provide the same result) are the only ways I can find to do what you want.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by