MATLAB: How to specify the spacing between minor tick marks and minor grid lines in the figures in MATLAB


I make a plot in MATLAB and then turn on minor grid lines as follows:
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.

Best Answer

  • 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:
    % 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,
    ax = gca;
    ax.XAxis.MinorTick = 'on';
    ax.XAxis.MinorTickValues = 1:0.5:10;
    grid on;
    ax.XMinorGrid = 'on';