Question about the axis of a figure

1 view (last 30 days)
On the x-axis I have 'number of subjects'. But the axis goes from 0 - 0.5 - 1 - 1.5 - ... Since 1.5 subjects doesn't exist, I want to get rid of these number with the comma. How do I do this?

Accepted Answer

Star Strider
Star Strider on 4 Jan 2015
Edited: Star Strider on 4 Jan 2015
You need to set the specific 'XTick' locations.
Example:
figure(1)
subplot(2,1,1)
scatter(rand(10,1)*3, rand(10,1)*2, 'pb')
grid
subplot(2,1,2)
scatter(rand(10,1)*3, rand(10,1)*2, 'pb')
grid
xt = get(gca, 'XTick');
set(gca, 'XTick', floor(min(xt)):ceil(max(xt)))
To illustrate:

More Answers (1)

Geoff Hayes
Geoff Hayes on 4 Jan 2015
Edited: Geoff Hayes on 4 Jan 2015
Sam - you will need to maipulate then ticks and tick labels for the x-axis. Try running the following statements
xticks = get(gca,'XTick');
xtickLabels = get(gca,'XTickLabel');
Both of these statements should return vectors/arrays of the positions and labels of the x-axis ticks respectively. Since you want to remove every other label (due to the 0.5), then just reset these two properties if the x-axis using every other element only as
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end));
Note that 1:2:end means start at the first element (at index 1) and jump by two until the end is reached. gca is the handle to the current axis (g is for get). The above may require some changes given your version of MATLAB, but try it out anyway.
EDIT
If xtickLabels is a character array, then the above set should be replaced with
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end,:));
  2 Comments
Sam
Sam on 4 Jan 2015
I tried it, but it doesn't work...
Geoff Hayes
Geoff Hayes on 4 Jan 2015
Sam - could you clarify what you mean by it doesn't work? If xtickLabels is a character array, then you may need to replace the set with
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end,:));

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!