|
整理下老帖, 顺便修改练习下!
-
- function GridColor(xc,yc)
- % GRIDCOLOR change the color of the axes
- % xc/yc is the color of x/y-axes direction
- % Example: plot(1:10); grid on; GridColor('r','b');
- if nargin==0, xc='k';yc='k';
- elseif nargin==1, yc=xc;
- if ~ischar(xc), error('the class of xc must char!'); end
- elseif nargin==2,
- if ~ischar(xc), error('the class of xc must char!'); end
- if ~ischar(yc), error('the class of yc must char!'); end
- else error('this program is applied to 2-D axes!'); end
- hold on; set(gca,'xgrid','off'); set(gca,'ygrid','off');
- changeC(xc,'x'); changeC(yc,'y'); hold off;
- function changeC(zc,ss)
- if ss~='x' & ss~='y', error('ss must "x" or "y"!'); end
- S=[ss,'tick']; Da=get(gca,S); Li=get(gca,[ss,'lim']);
- if ss=='x'
- for k=2:length(Da)-1, plot([Da(k),Da(k)],Li,':','color',zc); end
- else
- for k=2:length(Da)-1, plot(Li,[Da(k),Da(k)],':','color',zc); end
- end
复制代码-
- function [hx,hy]=GridXY(linewidth,xy)
- % GRIDXY add grid to the axes
- % linewidth is the width of grid's line; xy defines grid's direction
- % Example: plot(1:10); [hx,hy]=GridXY([2,1],'xy');
- xk=get(gca,'xtick'); yk=get(gca,'ytick'); xl=xlim; yl=ylim; hold on;
- if nargin==1, xy='x'; end
- if xy=='x', hx=plotx(xk,xl,yl,linewidth(1)); hy=[];
- elseif xy=='y', hy=ploty(yk,xl,yl,linewidth(end)); hx=[];
- else hx=plotx(xk,xl,yl,linewidth(1)); hy=ploty(yk,xl,yl,linewidth(end));
- end; hold off;
- function H=plotx(xk,xl,yl,linewidth)
- H=[];
- for k=1:length(xk), if xk(k)~=xl(1) & xk(k)~=xl(end),
- h=plot([xk(k),xk(k)],yl,'k:','linewidth',linewidth); H=[H,h];
- end; end
- function H=ploty(yk,xl,yl,linewidth)
- H=[];
- for k=1:length(yk), if yk(k)~=yl(1) & yk(k)~=yl(end),
- h=plot(xl,[yk(k),yk(k)],'k:','linewidth',linewidth); H=[H,h];
- end; end
复制代码 |
评分
-
1
查看全部评分
-
|