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

As posted,
figure bar(x,'FaceColor','flat')
is a syntax error, but presuming you meant and used
figure; bar(x,'FaceColor','flat')
or other variant that is correct, the code runs and produces what appears to be the expected result here. What's the question/problem?
NB: The line
set(gca,'xticklabels')
is of no use in a script; it will, however, echo the result for the empty property content to the command window. Just remove the line from your script to make the message go away.
For what you're doing, there's no need for the hold lines, either, you can change axes properties at will; the purpose of hold on is to add additional data lines or other plotting objects to an axes without overwriting existing; that's no happening here.
So, other than some minor syntactic issues, what's the question behind the question?
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'})
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

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!