Friday, August 24, 2012

[Matlab] show 2d mesh grid

function ShowGridLine(lon,lat,NSkip,LineC,LineS,LineM)
% To show a 2d mesh-grid defined by given grid points
% similar to the function mesh?
% Example:
%          ShowGridLine(lon,lat,2,'k','-','.')
%          http://scriptdemo.blogspot.com
%

if (nargin>3)
    if exist('LineC','var')~=1 LineC='k'; end
    if exist('LineS','var')~=1 LineS='-'; end
    if exist('LineM','var')~=1 LineM='none'; end
elseif (nargin>=2)
    if exist('NSkip','var')==1
        if ischar(NSkip) LineC=NSkip; clear NSkip; end
    end
    if exist('NSkip','var')~=1 NSkip=5; end
    if exist('LineC','var')~=1 LineC='k'; end
    if exist('LineS','var')~=1 LineS='-'; end
    if exist('LineM','var')~=1 LineM='none'; end
elseif (nargin==0)
    close all;
    [xx,yy]=meshgrid(1:19,1:19);
    ShowGridLine(xx,yy,1,'k','-');
    plot(4,4,'o','markerfacecolor','k','markersize',4,'markeredgecolor','k');
    plot(16,16,'o','markerfacecolor','k','markersize',4,'markeredgecolor','k');
    plot(4,16,'o','markerfacecolor','k','markersize',4,'markeredgecolor','k');
    plot(16,4,'o','markerfacecolor','k','markersize',4,'markeredgecolor','k');
    set(gcf,'color','w');
    set(gca,'xtick',2:2:18,'ytick',2:2:18,'ydir','r','xaxislocation','top')
    axis equal; axis tight;
    xlabel('Go Board','fontweight','bold','fontsize',18,'color','red');
    return
else
    help ShowGridLine
    return
end

[N1,N2]=size(lat);
if ~ishold hold on; end
for NL=1:NSkip:N2
    hp=plot(lon(:,NL),lat(:,NL));
    set(hp,'color',LineC,'linestyle',LineS,'Marker',LineM)
end
for NL=1:NSkip:N1
    hp=plot(lon(NL,:),lat(NL,:));
    set(hp,'color',LineC,'linestyle',LineS,'Marker',LineM,'tag','gridLines')
end
Demo:

No comments:

ShowCalendar