I have a loop that gives a variable a different name:
EX
i = 1;
while i < 5
if i == 1
name = 'CRHS';
elseif i == 2
name ='ELLY';
elseif i == 3
name = 'LGWD';
elseif i ==4
name = 'ECFS';
elseif i ==5
name = 'THMS';
end
i = i + 1;
end
I want to plot a figure using the 'name' variable as the corresponding X tick. For example for tick 1, use 'CRHS', tick 2 use 'ELLY', etc.
How can I do this?

 採用された回答

dpb
dpb 2014 年 4 月 9 日

23 投票

names = {'CRHS'; 'ELLY'; 'LGWD'; 'ECFS'; 'THMS'};
plot(...
set(gca,'xtick',[1:5],'xticklabel',names)
Set the tick values vector to appropriate numerical values dependent on the axis limits, of course. I just used the position values for demo porpoises...

9 件のコメント

Sandy
Sandy 2014 年 4 月 9 日
Perfect! Thank you!
S. Gopal
S. Gopal 2018 年 5 月 11 日
Thanks...It really works... I was searching of such a function ..Thanks again
Oded Scharf
Oded Scharf 2018 年 6 月 30 日
hey, how can I write the label vertically and not horizontaly? the ticks go on top of each other when they're horizontal.
thank you
dpb
dpb 2018 年 6 月 30 日
Use the [X|Y]TickLabelRotation property.
Anna Stafford
Anna Stafford 2020 年 2 月 3 日
Thanks so much!
Vijay
Vijay 2020 年 5 月 26 日
How can I get each name that has more than one row? Something like...
'CRHS
Age-10yrs'
The expectation is that each string contains 2 or 3 pieces of information.
Thanks
dpb
dpb 2020 年 5 月 26 日
Embed the newline '\n' in the string. Building with sprintf is often handy here.
Jack
Jack 2021 年 7 月 22 日
Is there any way to label all the tics but dynamically display them? For example you label x points 1:10 but if the plot is sized down by the user, some of the labels dissapears to fit? Similar to the default functionality matlab has with automatic axis resizing
dpb
dpb 2021 年 7 月 22 日
Once you write the tick labels manually you've broken the link between the tick marks and the tick labels; no automagic updating will happen after that.
You would have to write callback routines to handle the tick labels to go along with the ticks as wanted.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 4 月 9 日

コメント済み:

dpb
2021 年 7 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by