function hbarplot=barplot(x,y,z,varargin) % plot a 3d surface/tube centered at (x,y,z) % usage: % hbarplot=barplot(x,y,z,varargin) % varargin: % 'v' : the cdata values, same size as z % 'n' : N-isogon as the basic shape % 'r' : radius of the N-isogon % e.g., % zz=0:0.5:10; % hh=barplot(cos(zz/10*pi),5,zz,'v',randn(1,numel(zz))+sin(zz/10*pi),'n',4,'r',sin(zz/10*pi)); view(3) % http://scriptdemo.blogspot.com clc; myN=3; myR=0.5; isV=0; if nargin>3 while(size(varargin,2)>0) switch lower(varargin{1}) case {'v','cdata','colordata','facecolordata'} v=varargin{2}; varargin(1:2)=[]; isV=1; case {'n','nshape'} myN=varargin{2}; varargin(1:2)=[]; case {'r','radius'} myR=varargin{2}; varargin(1:2)=[]; otherwise error(['Not defined properties!',varargin{1}]) end end elseif nargin==0 % demo case zz=0:0.5:10; hbarplot=barplot(cos(zz/10*pi),5,zz,'v',randn(1,numel(zz))+sin(zz/10*pi),'n',4,'r',sin(zz/10*pi)); view(-150,30); return elseif nargin<3 help barplot return end [xx,yy,zz]=getMeshN(x,y,z,myR,myN); if isV==0 hbarplot=surface(xx',yy',zz'); else if numel(v)==numel(z) vv=repmat(reshape(v,[],1),1,size(xx,2)); end hbarplot=surface(xx',yy',zz'); set(hbarplot,'cdata',vv'); end set(hbarplot,'tag','barplot','linestyle','none','facecolor','interp'); axis off equal set(gcf,'color','w'); function [xx,yy,zz]=getMeshN(x0,y0,z,myR,N) if nargin==3 myR=0.5; N=3; elseif nargin==4 N=3; elseif nargin~=5 error('Usage: [xx,yy,zz]=getMeshN(x,y,z,myR,N)'); end z=reshape(z,[],1); numZ=numel(z); myAng=linspace(0,2*pi,N+1); xslab=cos(myAng); yslab=sin(myAng); %N=numel(xslab)-1; % could be other shape as well. if numel(x0)==1 x0=repmat(x0,numZ,N+1); elseif numel(x0)==numZ x0=repmat(reshape(x0,[],1),1,N+1); end if numel(y0)==1 y0=repmat(y0,numZ,N+1); elseif numel(y0)==numZ y0=repmat(reshape(y0,[],1),1,N+1); end if numel(myR)==1 xx=repmat(xslab*myR,numZ,1)+x0; yy=repmat(yslab*myR,numZ,1)+y0; else myR=repmat(reshape(myR,[],1),1,N+1); xx=repmat(xslab,numZ,1).*myR+x0; yy=repmat(yslab,numZ,1).*myR+y0; end zz=repmat(z,1,N+1); |
Monday, August 27, 2012
[Matlab] bar/tube plot along a 3d path
Sunday, August 26, 2012
[Matlab] fill a specific-value region with a given color
function FillIt(lon,lat,Land,FillC,TagType,FillValue) % To fill a specific-value region with a given color % Usage: % FillIt(lon,lat,Land,FillC,TagType,FillValue) % TagType: could be 'RU' or 'LD', % to fill right-up square or left-down square % FillValue: nan [default] or specific value to fill with color [FillC] % Note: lon, lat, Land must be the same size % e.g., % FillIt(lon,lat,lsmask,[0.0 0.3 0.3],'RU') % http://scriptdemo.blogspot.com if nargin==0 %demo load topo topo; topo(topo>0)=nan; xx=1:360; yy=1:180; [xx,yy]=meshgrid(xx,yy); FillIt(xx,yy,topo,[0.4 0.3 0.4],'RU'); set(gca,'tickdir','out','linewidth',2,'xminortick','on','yminortick','on','box','on','fontweight','bold'); set(gcf,'color','w'); axis equal; axis tight; return end if nargin==3 FillC=[0.3 0.3 0.3]; TagType='RU'; FillValue='nan'; elseif nargin==4 TagType='RU'; FillValue='nan'; elseif nargin==5 FillValue='nan'; elseif nargin~=6 help FillIt; return end if numel(lon)~=numel(lat) disp('lon and lat must be the same size') return end if numel(lon)~=numel(Land) disp('lon/lat and land must be the same size') return end [Ny,Nx]=size(Land); if ischar(FillValue) if strcmpi(FillValue,'nan') [Indy,Indx]=find(isnan(Land)); else disp(['invalid fill value: ',FillValue]) return end elseif isnumeric(FillValue) [Indy,Indx]=find(Land==FillValue); else disp('invalid fill value') return end switch upper(TagType) case 'RU' % Right Up % 4----3 % | | % 1----2 Indx(Indy==Ny)=[];Indy(Indy==Ny)=[]; Indy(Indx==Nx)=[];Indx(Indx==Nx)=[]; Fillx(1,:)=lon((Indx-1)*Ny+Indy); Fillx(2,:)=lon((Indx)*Ny+Indy); Fillx(3,:)=lon((Indx)*Ny+Indy+1); Fillx(4,:)=lon((Indx-1)*Ny+Indy+1); Fillx(5,:)=lon((Indx-1)*Ny+Indy); Filly(1,:)=lat((Indx-1)*Ny+Indy); Filly(2,:)=lat((Indx)*Ny+Indy); Filly(3,:)=lat((Indx)*Ny+Indy+1); Filly(4,:)=lat((Indx-1)*Ny+Indy+1); Filly(5,:)=lat((Indx-1)*Ny+Indy); case 'LD' % Left Down % 2----1 % | | % 3----4 Indx(Indy==1)=[];Indy(Indy==1)=[]; Indy(Indx==1)=[];Indx(Indx==1)=[]; Fillx(1,:)=lon((Indx-1)*Ny+Indy);Filly(1,:)=lat((Indx-1)*Ny+Indy); Fillx(2,:)=lon((Indx-2)*Ny+Indy);Filly(2,:)=lat((Indx-2)*Ny+Indy); Fillx(3,:)=lon((Indx-2)*Ny+Indy-1);Filly(3,:)=lat((Indx-2)*Ny+Indy-1); Fillx(4,:)=lon((Indx-1)*Ny+Indy-1);Filly(4,:)=lat((Indx-1)*Ny+Indy-1); Fillx(5,:)=lon((Indx-1)*Ny+Indy);Filly(5,:)=lat((Indx-1)*Ny+Indy); otherwise disp('Not defined Fill Type') help FillIt return end if ~ishold; hold on; end hf=patch(Fillx,Filly,FillC); set(hf,'linestyle','none','tag','fillIt'); |
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 |
Subscribe to:
Posts (Atom)