MATLAB: How do you label every other tick

axistickticklabelxticklabelsxticks

I'm trying to scatter plot some data and would like to know how to label only every 'x' tick. I've been using xticks and xticklabels, but to no avail. This is the part of my code that produces the plot in MATLAB R2017a:
scatter(x_vals,y_vals,[],c); xlim([n x_max]);
xticks(n:1:x_max); % xticklabels(n:x:x_max); I'm aware this is not proper xticklabels syntax,
% I just wrote it this way to show what I wanted.
hold on
vline(average,'b',sprintf(['Average\n(' num2str(average) ')']));
Which for some specific data may look like this:
So is there a way to still have every tick mark show while only having every second or third tick mark labeled? Like in the case above, keep every tick mark, but only have labels "3, 6, 9,[…], 27". Keep in mind the data that is plotted will change, so manually inputting xticklabels({'blah', ' ', 'blah' , ' ', 'blah', etc…}) each time won't work for me.
I've tried looking around for questions like mine but I kept seeing things like "set(gca,…", which is both something I've never seen and wasn't able to get to work. I'm somewhat new to MatLab and I'm not familiar with a whole lot (had to research most of this above code too), so you might have to dumb your answers down for me, sorry 😛

Best Answer

Something along these lines:
plot(1:10);
ax = gca;
labels = string(ax.XAxis.TickLabels); % extract
labels(2:2:end) = nan; % remove every other one
ax.XAxis.TickLabels = labels; % set