How can I specify the spacing between minor tick marks and minor grid lines in my figures in MATLAB?

533 views (last 30 days)
I make a plot in MATLAB and then turn on minor grid lines as follows:
plot(1:10)
grid minor
The minor grid lines are based on the placing of the minor tick marks. MATLAB automatically calculates the spacing between the minor tick marks. I would like to be able to set these manually in the same manner I can set the placing of major tick marks.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Jul 2016
The ability to set the number of minor tick marks and the spacing between them is not available in MATLAB (R2015a)
If you are using R2015a, here is the workaround,
you may use the LINE function to create your own minor tick marks, for example with:
figure
% specify the minor grid vector
xg = [0:0.025:1];
% specify the Y-position and the height of minor grids
yg = [0 0.1];
xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3)
yy = repmat([yg NaN],1,length(xg))
h_minorgrid = line(xx,yy,'Color','r')
axis([0 1 0 10])
If you are using R2015b, workaround as follows,
plot(1:10);
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = 1:0.5:10;
grid on;
ax.XMinorGrid = 'on';

More Answers (1)

Stefan Kiss
Stefan Kiss on 24 Jan 2016
I think I came across the minor tick (and therefore grid) variables inside the "Ruler" object (along with the major tick variables):
ax.XRuler.MinorTick = 'on' %or 'off'
ax.XRuler.MinorTickValues = [0.2 0.4 0.6 0.8]; %just like major ticks
ax.XRuler.MinorTickValuesMode = 'auto'; %or 'manual'
  2 Comments
Mike Garrity
Mike Garrity on 25 Jan 2016
That's true, but the XAxis property is documented in R2015b.
There were a couple of tweaks in addition to the name change. The biggest change in this case is that you give the values for all of the minor ticks, rather than one decade that gets repeated. It would look something like this:
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = ax.XAxis.Limits(1):.05:ax.XAxis.Limits(2);
More details on this doc page .

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!