20.11M文件例子 精通 MATLAB工具箱含有许多实用函数,它们可以验证本章的许多概念。这些函数的基 本部分已经在二维和三维图形这些章阐述过了。有了前面对句柄图形的讨论,我们现在可以更 彻底地讨论这些函数 最简单的精通 MATLAB工具箱的函数之一提出了一个共同的问题。 MATLAB函数gcf返 回当前图形的句柄。但是,它有一个副作用。如果图形不存在,gcf就创建一个,并返回它的句 柄。如果想寻找一个图形是否存在于头一个位置,要是没有,又不得不创建,怎么办?函数mmgf 正好实现由其内容所描述的工作。 function HF=mmgcf %MMGCF Get Current Figure if it Exists MMGCF returns the handle of the current figure if it exists If no current figure exists, MMGCF returns an empty handle Note that the function gCF is different lt creates a figure and returns its handle if it does not st Copyright (c) 1996 by Prentice-Hall, Inc Hf-get(O, Children); check for figure children if isempty (Hf) else Hf=get(O, CurrentFigure') d 函数 meget首先检査根对象的子对象的图形是否存在,如至少有一个图形对象时,根对象 的 urrent Figure'属性就返回当前的图形 函数 mmgca为坐标轴对象执行同样的功能,如同在它的M文件内所描述的那样。 function Ha=mmgca MMGCA Get Current Axes if it exists MMGCA returns the handle of the current axes if it exists If no current axes exists, MMGCA returns an empty handle Note that the function gca is diffent lt create a figure and an axes and returns the axes handle if they do not exist. Copyright (c) 1996 by Prentice-Hall, Inc
20.11 M 文件例子 精通 MATLAB 工具箱含有许多实用函数,它们可以验证本章的许多概念。这些函数的基 本部分已经在二维和三维图形这些章阐述过了。有了前面对句柄图形的讨论,我们现在可以更 彻底地讨论这些函数。 最简单的精通 MATLAB 工具箱的函数之一提出了一个共同的问题。MATLAB 函数 gcf 返 回当前图形的句柄。但是,它有一个副作用。如果图形不存在,gcf 就创建一个,并返回它的句 柄。如果想寻找一个图形是否存在于头一个位置,要是没有,又不得不创建,怎么办?函数 mmgcf 正好实现由其内容所描述的工作。 function HF=mmgcf %MMGCF Get Current Figure if it Exists. % MMGCF returns the handle of the current figure if it exists. % If no current figure exists,MMGCF returns an empty handle. % % Note that the function GCF is different.It creates a figure and returns its handle if it does not % exist. % Copyright (c) 1996 by Prentice-Hall,Inc. Hf=get(0, ‘Children’ ); % check for figure children if isempty(Hf) return else Hf=get(0, ‘CurrentFigure’ ); end 函数 mmgcf 首先检查根对象的子对象的图形是否存在,如至少有一个图形对象时,根对象 的 ‘CurrentFigure’ 属性就返回当前的图形。 函数 mmgca 为坐标轴对象执行同样的功能,如同在它的 M 文件内所描述的那样。 function Ha=mmgca %MMGCA Get Current Axes if it exists. % MMGCA returns the handle of the current axes if it exists. % If no current axes exists,MMGCA returns an empty handle. % % Note that the function GCA is diffent.It create a figure and an axes and returns the axes % handle if they do not exist. % Copyright (c) 1996 by Prentice-Hall,Inc
Ha=findobj(o, Type, axes') if isempty(Ha) return Ha=get( get(O, Current Figfure'), 'CurrentAxes') 由于函数gco已经表现出当对象不存在时返回空矩阵的行为特性,就不需要函数 mmgco 在精通 MATLAB工具箱中的另一个函数是 mmap,在二维图形那一章里已作过介绍。如 下M文件中所示,它使用 mmgcf作错误检查,与 findobj和get一起删除一个指定的图形。 function mmzap(arg) %MMZAP Delete graphics object using mouse MMZAP waits for a mouse click on an object in a figure window and deletes the object MMZAP or MMZAP text erases text objects MMZAP axes erases axes objects MMZAP line erases line objects MMZAP surf erases surface objects %o MMZAP patch erases patch objects % Clicking on an object other than the selected type or striking a key on the keyboard aborts Copyright (c) 1996 by Prentice-Hall, Hf-mmgcf if isempty(Hf), error( No Figure Available. ),end if length(findobj(o, Type, figure)) figure( Hf) bring only figure forword key=waitforbuttonpress; pause untill user takes some action if key key on keyboard pressed return take no acti pe=get(object, Type'); if all(type( 1: 4)==arg( 1: 4))% delete only if Type is correct delete(object)
Ha=findobj(0, ‘Type’ , ‘axes’ ); if isempty(Ha) return else Ha=get(get(0, ‘CurrentFigfure’ ), ‘CurrentAxes’ ); end 由于函数 gco 已经表现出当对象不存在时返回空矩阵的行为特性,就不需要函数 mmgco 了。 在精通 MATLAB 工具箱中的另一个函数是 mmzap,在二维图形那一章里已作过介绍。如 下 M 文件中所示,它使用 mmgcf 作错误检查,与 findobj 和 get 一起删除一个指定的图形。 function mmzap(arg) %MMZAP Delete graphics object using mouse. % MMZAP waits for a mouse click on an object in a figure window and deletes the object. % MMZAP or MMZAP text erases text objects. % MMZAP axes erases axes objects. % MMZAP line erases line objects. % MMZAP surf erases surface objects. % MMZAP patch erases patch objects. % % Clicking on an object other than the selected type or striking a key on the keyboard aborts % the command. % Copyright (c) 1996 by Prentice-Hall,Inc. if nargin<1,arg= ‘text’ ;end Hf=mmgcf; if isempty(Hf),error( ‘No Figure Available.’ ),end if length(findobj(0, ‘Type’ , ‘figure’ ))==1 figure(Hf) % bring only figure forword end key=waitforbuttonpress; % pause untill user takes some action if key % key on keyboard pressed return % take no action else % object selected object=gco % get object selected by buttonpress type=get(object, ‘Type’ ); if all(type(1:4)==arg(1:4)) % delete only if ‘Type’ is correct delete(object)
在编写句柄图形函数的M文件时,函数mmap描述了一种很有用的技术。它利用函数 waitforbuttonpress和gco的结合用鼠标来获取所选定对象的句柄。 waifortbuttonpress是一个 MATLAB内置函数,它的功能是等待鼠标点击或按键。它的帮助文本如下: > help waitforbuttonpress WAITFOR BUTTONPRESS Wait for key/buttonpress over figure T- WAITFOR BUTTONPRESS stops program execution untill a key or mouse button is pressed over a figure window Returns when terminated by a mouse buttonpress, or when terminated by a keypress. Additional information about the terminating event is available from the current figure See alSo INPUT gCF 帮助信息 WAITFORBUTTONPRESS等待一个鼠标/按钮对图形按下 T= WAITFORBUTTONPRESS停止程序的执行,直到鼠标按钮或键在一个图形窗口按下。 当鼠标按钮按下时返回0:当键按下时返回1。其它的结束事件的信息可从当前的图形中窗口 获取。 参阅 GINPUT和GCF 鼠标按钮在鼠标指针指的图形上按下后,函数gco返回所点中对象的句柄。然后,该句柄 可用来操作选中的对象。在精通MA∏LAB工具箱中,用这种简单的选择技术的函数还有 mmline 和 mmaxes。其中, mmline的M文件描述如下 function mmline(argl, arg2, arg3, arg 4, arg 5, arg6) %MMLINE Set Line Properties Using Mouse MMLINE waites for a mouse click on a line then applies the desired properties to the selected line Properties are given in parts, e.g., MMLINE Name value %o Properties VaLUe default) o color TY m crgb wk]or an RGB in quotes: ' bl style mark points for linewidth 10.5)
end end 在编写句柄图形函数的 M 文件时,函数 mmzap 描述了一种很有用的技术。它利用函数 waitforbuttonpress 和 gco 的结合用鼠标来获取所选定对象的句柄。waifortbuttonpress 是一个 MATLAB 内置函数,它的功能是等待鼠标点击或按键。它的帮助文本如下: » help waitforbuttonpress WAITFOR BUTTONPRESS Wait for key/buttonpress over figure. T= WAITFOR BUTTONPRESS stops program execution untill a key or mouse button is pressed over a figure window.Returns 0 when terminated by a mouse buttonpress,or 1 when terminated by a keypress.Additional information about the terminating event is available from the current figure. See also GINPUT,GCF. 帮助信息: WAITFORBUTTONPRESS 等待一个鼠标/按钮对图形按下。 T=WAITFORBUTTONPRESS 停止程序的执行,直到鼠标按钮或键在一个图形窗口按下。 当 鼠标按钮按下时返回 0;当键按下时返回 1。其它的结束事件的信息可从当前的图形中窗口 获取。 参阅 GINPUT 和 GCF。 鼠标按钮在鼠标指针指的图形上按下后,函数 gco 返回所点中对象的句柄。然后,该句柄 可用来操作选中的对象。在精通 MATLAB 工具箱中,用这种简单的选择技术的函数还有 mmline 和 mmaxes。其中,mmline 的 M 文件描述如下: function mmline(arg1,arg2, arg3, arg4, arg5, arg6) %MMLINE Set Line Properties Using Mouse % MMLINE waites for a mouse click on a line then applies the desired properties to the % selected line. % Properties are given in parts,e.g.,MMLINE Name value... % Properties: % NAME VALUE{default} % color [Y m c r g b w k] or an RGB in quotes: ‘[r g b]’ % style [- -- ; -.] % mark [o + . * X] % width points for linewidth {0.5}
sIze points for marker size 16: (n a) delete selected line Examples MMLINE color r width 2 sets color to red and width to 2 points MMLINE mark size 8 sets marker type to and size to 8 points % Clicking on an object other than a line, or striking a key on the keyboard aborts the command Copyright (c) 1996 by Prentice-Hall, Inc if isempty(Hf), error( No Figure Avaiable. ), end if length(get(O, Children))==1 figure(Hf) bring only figure forword end key=waitforbuttonpress if key key on keyboard pressed object selecte if st for i-1: 2: max( nargin-1, I Name=eval(sprintf(arg%.Of', 1), ) get Name argument if strcmp( value=eval(sprintf(arg%.0f, 1+1)); get value t(hl, color set(HI, Linestyle t(HI, Linestyle elseif strcmp(Name, width) value=abs(eval( value)
% size points for marker size {6} % zap (n.a.) delete selected line % Examples: % MMLINE color r width 2 sets color to red and width to 2 points % MMLINE mark + size 8 sets marker type to + and size to 8 points % % Clicking on an object other than a line,or striking a key on the keyboard aborts the % command. % Copyright (c) 1996 by Prentice-Hall,Inc. Hf=mmgcf; if isempty(Hf),error( ‘No Figure Avaiable.’ ),end if length(get(0, ‘Children’ ))==1 figure(Hf) % bring only figure forword end key=waitforbuttonpress; if key % key on keyboard pressed return else % object selected Hl=gco if strcmp(get(Hl, ‘Type’ ), ‘line’ ) % line object selected for i=1:2:max(nargin-1,1) Name=eval(sprintf( ‘arg%.0f ‘ ,i),[]); get Name argument if strcmp(Name, ‘zap’ ) delete(Hl),return end value=eval(sprintf( ‘arg%.0f ‘ ,i+1),[]); % get value if strcmp(Name, ‘color’ ) set(Hl, ‘Color’ ,value) elseif strcmp(Name, ‘style’ ) set(Hl, ‘Linestyle’ ,value) elseif strcmp(Name, ‘mark’ ) set(Hl, ‘Linestyle’ ,value) elseif strcmp(Name, ‘width’ ) value=abs(eval(value)) set(Hl, ‘LineWidth’ ,value) elseif strcmp(Name, ‘size’ ) value=abs(eval(value)) set(Hl, ‘MarkerSize’ ,value) else
displl' Unknown Property Name: NameD) 精通MA∏LAB工具箱中的函数 mmpaper以简单的方式阐述了对纸张属性的使用。如下所 示,函数 mmpaper设置当前图形的纸张属性,并将所有以后的图形设成缺省值。函数 mmpage 在下一章讨论,它是一个 mmpaper友函数。 mmpage建立一个图形用户界面,设定图形在打印 页上的位置 function mmpaper(argl, arg2, arg3, arg 4, arg5, arg6) %MMPAPER Set Default Paper Properties MMPAPER Name value sets default paper properties for the current figure and succeeding figures based on Name value pairs Properties NAME VaLue default) Units [inches), centimeters, points, normal] orient [portrait), landscape] %o type Iusletter), uslegal, a3, a4letter, a5, b4, tabloid % Examples MMPAPER Units inch orient landscape MMPAPER type tabloid %o MMPAPER with no arguments returns the current paper defaults %o Copyright (c) 1996 by Prentice-Hall, Inc Hf=mmgcf flag=0 if isempty(Hf) Hf-figure('Visible, off) if nargin for i=1: 2 max( nargin-l, 1) Name=eval(sprintf(arg%.0f, i)o, 0); get Name argument valueseval(sprintf(arg%.0f, i+1)o, get Name argument if Name(1)==o
disp([ ‘Unknown Property Name: ‘ Name’]) end end end end 精通 MATLAB 工具箱中的函数 mmpaper 以简单的方式阐述了对纸张属性的使用。如下所 示,函数 mmpaper 设置当前图形的纸张属性,并将所有以后的图形设成缺省值。函数 mmpage 在下一章讨论,它是一个 mmpaper 友函数。mmpage 建立一个图形用户界面,设定图形在打印 页上的位置。 function mmpaper(arg1,arg2,arg3,arg4,arg5,arg6) %MMPAPER Set Default Paper Properties. % MMPAPER Name value... % sets default paper properties for the current figure and succeeding figures based on Name % value pairs. % Properties: % NAME VALUE {default} % Units [{inches},centimeters,points,normal] % orient [{portrait},landscape] % type [{usletter},uslegal,a3,a4letter,a5,b4,tabloid] % % Examples: % MMPAPER Units inch orient landscape % MMPAPER type tabloid % % MMPAPER with no arguments returns the current paper defaults. % Copyright (c) 1996 by Prentice-Hall,Inc. Hf=mmgcf; flag=0; if isempty(Hf) flag=1; Hf=figure( ‘Visible’ , ‘off ‘ ); end if nargin for i=1:2:max(nargin-1,1) Name=eval(sprintf( ‘arg%.0f ‘ ,i)0,[]); % get Name argument value=eval(sprintf( ‘arg%.0f ‘ ,i+1)0,[]); % get Name argument if Name(1)== ‘o’