Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving MATLAB axis ticks by a half step

I'm trying to position MATLAB's ticks to line up with my grid, but I can't find a good way to offset the labels.

Also, if I run set(gca,'XTickLabel',1:10), my x tick labels end up ranging from 1 to 5. What gives?

enter image description here

like image 565
Nick Sweet Avatar asked Mar 19 '23 18:03

Nick Sweet


1 Answers

You need to move the ticks, but get the labels before and write them back after moving:

f = figure(1)
X = randi(10,10,10);
surf(X)
view(0,90)

ax = gca;
XTick = get(ax, 'XTick')
XTickLabel = get(ax, 'XTickLabel')
set(ax,'XTick',XTick+0.5)
set(ax,'XTickLabel',XTickLabel)

YTick = get(ax, 'YTick')
YTickLabel = get(ax, 'YTickLabel')
set(ax,'YTick',YTick+0.5)
set(ax,'YTickLabel',YTickLabel)

enter image description here


Or if you know everything before, do it manually from the beginning:

[N,M] = size(X)

set(ax,'XTick',0.5+1:N)
set(ax,'XTickLabel',1:N)
set(ax,'YTick',0.5+1:M)
set(ax,'YTickLabel',1:M)
like image 131
Robert Seifert Avatar answered Mar 29 '23 07:03

Robert Seifert