Search code examples
matlabplotmatlab-figure

Remove XTickLabels partly but not Marks (matlab)


I want a plot with marks for every number, but label only every tenth mark. I tried to do it like this:

set(gca, 'XTick', 0:1:100);
set(gca, 'XTicklabel', [0 10 20 30 40 50 60 70 80 90 100]);

But this way it labels every mark, and it's not possible to read anything. I also tried to do this (delete all labels and than add only several):

set(gca, 'XTicklabel', []);
set(gca, 'XTicklabel', [0 10 20 30 40 50 60 70 80 90 100]);

but didn't work either. Any Suggestions how to solve this? I'm using Matlab R2014a. Thanks!


Solution

  • You can just use empty labels when you don't want to display them

    % Set up ticks and labels
    ticks = 0:1:100;
    labels = num2cell(ticks);
    % Remove all but every 10th label
    labels(mod(ticks,10)~=0) = {''};
    % Set
    set(gca, 'XTick', ticks);
    set(gca, 'XTickLabel', labels);
    

    Example using plot(1:100,1:100) then the above code

    plot