How to place figure labels

4 views (last 30 days)
wejdan Albanna
wejdan Albanna on 12 Apr 2018
Commented: dpb on 13 Apr 2018
I need to change the x-axis labels, and the following format I wrote is not working, any help!
x = [ 1.3404 1.3404 1.5011;
2.1141 2.1141 2.1591;
2.4665 2.4665 2.4935;
2.8187 2.8187 2.8591;
3.1711 3.1711 3.2464];
figure bar(x,'FaceColor','flat')
hold on
set(gca,'xticklabels')
xticklabels ({'5','5+1','5+2','5+3','5+4'})
xlabel('Number of Nodes')
ylabel('Total Power')
legend('CL1','CL2', 'CL3')
title('clusters')
hold off
  3 Comments
wejdan Albanna
wejdan Albanna on 13 Apr 2018
I tried to change the labels in the x-axis, I did it previously using 'Figure Properties', but it only last for one figure. I am asking about the following code and what need to be rewritten/change
xticklabels ({'5','5+1','5+2','5+3','5+4'})
dpb
dpb on 13 Apr 2018
Of course it only "lasts" for one figure; the tick labels are properties of the axes and are refreshed any time a new axes is created.
You don't want to set the default labels to something for a specific figure/plot; then you've essentially broken the PLOT() command for anything else without fixing it up every time.
Instead, if you're doing this multiple times, write a script or function that you execute/call for each case and include setting the tick labels appropriately in it. You do it once in writing the function but then it's done for you automagically when you use the function/script.
In general you would not hardcode text into the function but either derive the values for the labels from the data or pass in what is appropriate for the particular case; if this is processing a bunch of data for a very specific and narrow-scope kind of analysis then it could make sense for that purpose if number and text for the labels really won't ever change.

Sign in to comment.

Accepted Answer

Von Duesenberg
Von Duesenberg on 12 Apr 2018
Is this what you are trying to achieve ? :
x = [ 1.3404 1.3404 1.5011;
2.1141 2.1141 2.1591;
2.4665 2.4665 2.4935;
2.8187 2.8187 2.8591;
3.1711 3.1711 3.2464];
figure; bar(x,'FaceColor','flat')
set(gca,'xticklabels', {'5','5+1','5+2','5+3','5+4'})
xlabel('Number of Nodes')
ylabel('Total Power')
legend('CL1','CL2', 'CL3')
title('clusters')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!