句柄图形对象(1)function my_plotx=1:10y=peaks(10)%Z = peaks(n);returns an n-by-n matrix.cax=newplot%newplot prepares a figure and axes for subsequent graphics commands.%h = newplot prepares a figure and axes for subsequent graphics commands and returns a handle to the current axes.LSO=['- ';'--';': ';'-.']%此处第一项与第三项要加空格,使得各行元素相等set(cax,'FontName','Times','FontAngle','italic')%设置图形坐标轴的字体与字体倾斜角度set(get(cax,'Parent'),'MenuBar','none')%关闭工具栏line_handles=line(x,y,'Color','b')%将10条曲线利用直线全部画出,并返回直线句柄向量,10条曲线对应10个句柄值%所以该句柄向量一共有10个元素style=1for i=1:length(line_handles)if style>length(LSO)style=1endset(line_handles(i),'LineStyle',LSO(style,:)) %设置直线属性style=style+1endgrid on(2)function my_plot3(x,y,z)cax=newplothold_state=ishold%检测当前的hold状态LSO=['- ';'--';': ';'-.']narginif nargin==2%nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。
%通常可以用他来设定一些默认值,如下面的函数。
例子,函数test1的功能是输出a和b的和。
%如果只输入一个变量,则认为另一个变量为0,如果两个变量都没有输入,则默认两者均为0。
%function y=test1(a,b)%if nargin==0%a=0;b=0;%elseif nargin==1%b=0;%endy=a+b;hlines=line(x,y,'Color','k')if ~hold_state%如果hold为off时,改变视图view(2)endelseif nargin==3hlines=line(x,y,z,'Color','k')if ~hold_state%如果hold为off时,改变视图view(3)endendls=1for hindex=1:length(hlines)if ls>length(LSO)ls=1endset(hlines(hindex),'LineStyle',LSO(ls,:)) ls=ls+1end(3)function visit_handlex=0:15y=[1.5*cos(x);4*exp(-0.1*x).*cos(x);exp(0.05*x).*cos(x)]h=stem(x,y')%注意转换成列向量axis([0,16,-4,4])set(h(1),'Color','black','Marker','o','Tag','Decaying Exponentail')%h(1)表示Y第一行元素在图形中的属性set(h(2),'Color','black','Marker','square','Tag','Growing Exponentail')%Marker表示元素点表示方法,三列标识都不同set(h(3),'Color','black','Marker','*','Tag','Steady State ') %图形对象有句柄,是一个数;GUI里的控件有tag,是一个字符串。
都是独一无二的标识。
%h = plot(sin(0:.01:2*pi);返回的h是曲线的句柄,它没有tag 比如某个文本的tag值为t1,设置其内容为hello,则可以这么写set(handles.t1,'string','hello');set(findobj(gca,'-depth',1,'Type','line'),'LineStytle','--' )%将图形中所有属性为直线的对象变为虚线h=findobj('-regexp','Tag','^(?!Steady State$).')%返回所有正则表达式Tag为Steady State属性的对象set(h,{'MarkerSize'},num2cell(cell2mat(get(h,'MarkerSize')) +2))%改变上述属性对象的标记大小h=findobj('type','line','Marker','none','-and','-not','Line Style','--')%返回线性为直线没有标记并且线性不是虚线的对象set(h,'Color','red')%改变上述对象的颜色(4)function setup_axesh=axes('Color',[0.9,0.9,0.9],...'GridLineStyle','--',...'ZTickLabel','-1|Z=0 Plane|+1',...'FontName','times',...'FontAngle','italic',...'FontSize',14,...'XColor',[0 0 0.7],...'YColor',[0 0 0.7],...'ZColor',[0 0 0.7])set(get(h,'XLabel'),'String','Value of X')set(get(h,'YLabel'),'String','Value of Y')set(get(h,'Title'),'String','\fontname{times}\itZ=f(x,y)') %设置标题Z=f(x,y)的字形set(get(h,'XLabel'),'String','Value of X',...'FontName','times',...'FontAngle','italic',...'FontSize',14)(5)function onefigure_severeaxes%在坐标轴外放置文本h=axes('Position',[0 0 1 1],'Visible','off') %对该坐标轴不可见axes('Position',[0.25 0.1 0.7 0.8])t=0:900plot(t,0.25*exp(-0.005*t))str(1)={'Plot of the function:'}str(2)={' y=A{\ite}^{-\alpha{\itt}} '}%注意字符串书写格式str(3)={'With the values:'}str(3)={' A=0.25'}str(4)={' \alpha=0.005 '}%特殊字符前加\str(5)={' t=0:900 '}set(gcf,'CurrentAxes',h)text(0.025,0.6,str,'FontSize',12)%在同一个图形中显示不同缩放尺度的图形h(1)=axes('Position',[0 0 1 1])sphereh(2)=axes('Position',[0 0 0.4 0.6]) sphereh(3)=axes('Position',[0 0.5 0.5 0.5]) sphereh(4)=axes('Position',[0.5 0 0.4 0.4]) sphereh(5)=axes('Position',[0.5 0.5 0.5 0.3]) sphereset(h,'Visible','off')%显示双坐标轴x1=[0:0.1:40]y1=4.*cos(x1)./(x1+2)x2=[1:0.2:20]y2=x2.^2./x2.^3%显示第一个坐标轴对象h11=line(x1,y1,'Color','r') ax1=gcaset(ax1,'XColor','r','YColor','r')%添加第二个坐标轴显示对象ax2=axes('Position',get(ax1,'Position'),...'XAxisLocation','top',...'YAxisLocation','right',...'Color','none',...'XColor','k','YColor','k')h12=line(x2,y2,'Color','k','Parent',ax2)xlimist1=get(ax1,'Xlim')ylimist1=get(ax1,'Ylim')xlinc1=(xlimist1(2)-xlimist1(1))/5ylinc1=(ylimist1(2)-ylimist1(1))/5xlimist2=get(ax2,'Xlim')ylimist2=get(ax2,'Ylim')xlinc2=(xlimist2(2)-xlimist2(1))/5ylinc2=(ylimist2(2)-ylimist2(1))/5%设置标度显示set(ax1,'XTick',[xlimist1(1):xlinc1:xlimist1(2)],... 'YTick',[ylimist1(1):ylinc1:ylimist1(2)])set(ax2,'XTick',[xlimist2(1):xlinc2:xlimist2(2)],... 'YTick',[ylimist2(1):ylinc2:ylimist2(2)])grid on(6)t=0:pi/20:2*pis=sin(t)c=cos(t)%%设置axes对象的Color属性figh=figure('Position',[30 100 800 350],...'DefaultAxesColor',[0.8 0.8 0.8])%%position [left bottom width height],set(gca,'Units') %%[inches | centimeters | points | pixels]axh1=subplot(1,2,1)grid on%%设置第一个Axes对象的LineStyle属性set(axh1,'DefaultLineLineStyle','-.')line('XData',t,'YData',s)line('XData',t,'YData',c)text('Position',[3 0.4],'String','Sine')text('Position',[2 -0.3],'String','Cosine',...'HorizontalAlignment','right') %文字水平对齐axh2=subplot(1,2,2)grid on%%设置第二个Axes对象的文字旋转属性set(axh2,'DefaultTextRotation',90)%文字旋转90度line('XData',t,'YData',s)line('XData',t,'YData',c)text('Position',[3 0.4],'String','Sine')text('Position',[2,0.3],'String','Cosine',...'HorizontalAlignment','right')Z=peaksplot(1:49,Z(4:7,:))closeset(0,'DefaultAxesColorOrder',[0 0 0],...'DefaultAxesLineStyleOrder','-|--|:|-.') %设置属性默认值,坐标轴颜色顺序,曲线线性顺序plot(1:49,Z(4:7,:))set(0,'DefaultAxesColorOrder','remove',...'DefaultAxesLineStyleOrder','remove')%删除属性默认值set(0,'DefaultSurfaceEdgeColor','k') h=surface(peaks)set(gcf,DefaultSurfaceEdgeColor','g') set(h,'EdgeColor')(7)function copy_detele_objx=0:0.01:6.28y=sin(x)figureplot(x,y)text_handle=text('String','\{5\pi\div4,sin(5\pi\div4)\}\rightarrow',...'Position',[5*pi/4,sin(5*pi/4),0],'HorizontalAlignment','right') %创建text对象句柄,将该句柄复制到其他图形中。