Search code examples
matlabplotmatlab-figure

Add 0.5x0.5 degree grid on drawing of country outline


How do I draw a 0.5 degree x 0.5 degree grid over the country map in a MATLAB figure?

The code below gives me a gridded figure but not with 0.5x0.5 degree spacing.

borders('Iran Islamic Republic of')
grid on
ax.GridLineStyle = '-';

Can anyone tell me how to add 0.5x0.5 grid along x and y-axis to this figure?

The borders function is taken from the MATLAB File Exchange


Solution

  • You can use xticks() and yticks() functions (matlab tutorial). Your code should be something like:

    borders('Iran Islamic Republic of')
    grid on
    ax.GridLineStyle = '-';
    
    % Modify the X and Y ticks positions
    xticks([44:.5:65]);
    yticks([25:.5:40]);
    

    This creates ticks every 0.5 degrees (from degree 44 until 65 in x, and from 25 to 40 in y). If the tick labels are overlaping, you can delete some. For example for the x-axis:

    %Delete some labels, otherwise overcrowded 
    xlabels = xticklabels();
    for i=2:2:length(xticks())
        xlabels(i)={''};
    end
    xticklabels(xlabels)