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.