GUI图形用户界面入门4Word文档格式.docx
《GUI图形用户界面入门4Word文档格式.docx》由会员分享,可在线阅读,更多相关《GUI图形用户界面入门4Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
;
arg2='
w'
arg3='
style'
arg4='
:
nargin=4;
end
ifisstr(arg1)%initialcall,setthingup
ifisempty(Hf),error('
NoFigureAvailable.'
),end
Ha=findobj(Hf,'
Type,'
axes'
);
ifisempty(Ha),error('
NoAxesinCurrentFigure.'
set(Hf,'
Pointer'
Crossh'
,...%setupcallbackforlinestar
BackingStore'
off'
WindowButtonDownFcn'
mmdraw
(1)'
)
figure(Hf)
MMDRAW_EVAL='
mmdrw(99);
%setupstringtosetattributes
fori=1:
nargin
argi=eval(sprintf('
arg%.of'
,i));
MMDARW_EVAL=[MMDARW_EVAL'
arg'
];
end
MMDARW_EVAL=[MMDARW_EVAL,)'
elseifarg1==1%callbackislinestartpoint
fp=get(gca,'
CurrentPoint'
%startpoint
set(gca,'
Userdata'
,fp(1,1:
2))%storeinaxesuserdata
set(gcf,'
WindowButtonMotionFcn'
mmdraw
(2)'
WindowButtonUpFcn'
mmdraw(3)'
elseifarg1==2%callbackismousemotion
cp=get(gca,'
cp=cp(1,1:
2);
H1=line('
Xdata'
,[fp
(1);
cp
(1)],'
Ydata'
,[fp
(2);
cp
(2)],...
EraseMode'
Xor,...
Color'
LineStyle'
"
Clipping'
if~isempty(MMDRAW_HL)%deletepriorlineifitexsts
delete(MMDRAW_HL)
MMDRAW_HL=H1;
%storecurrentlinehandle
elseifarg1==3%callbackislineendpoint,finishup
Point'
arrow,...
on'
mmtext
(2)'
,[])
set(MMDRAW_HL,'
normal'
)%renderlinebetter
eval(MMDRAW_EVAL)
MMDRAW_EVAL=[];
elseifarg1==99%processlineproperties
fori=2:
2:
nargin-1
name=eval(sprintf('
,i),[]);
%getnameargument
vale=eval(sprintf('
,i+1),[]);
%getvalueargument
ifstrcmp(name,'
ifvalue
(1)=='
['
,value=eval(value);
,value)
elseifstrcmp(name,'
Lineatyle'
mark'
Linestyle'
width'
value=abs(eval(value));
LineWidth'
size'
MarkerSize'
MMDRAW_HL=[];
虽然这里说明太长,但精通MATLAB工具箱中的函数mmsetc和mmsetf都是使用递归、全局变量和'
UserData'
属性的GUI函数的直观例子。
也许愿意看一下mmsetclrM文件,它是函数mmsetc文件的脚本M文件。
比较这两个文件,可以了解到,为实现各种GUIM文件要做出一些折衷。
21.9对话框和请求程序
MATLAB具有建立对话框和'
请求'
的几个有用工具。
对话框是弹出显示的单独窗口,它显示信息字符串。
对话框含有一个或多个按钮键以供用户输入。
请求框是在弹出显示的单独窗口,利用鼠标或键盘获得输入,并返回信息给调用函数。
对话框
所有MATLAB的对话框都是基于函数dialog,它的帮助文本如下
>
helpdiolog
DIALOGdisplaysadialogbox.
FIG=DIALOG(pl,vl....)displaysadialogbox.
validparam/valuepairsinclude
Styleerror|warning|help|question
Namestring
Replaceon|off
Resizeon|off
BackgroundColorColorSpec
ButtonString'
Button1String|Button2String|...'
ButtonCalls'
ButtonCallback|Button2Callback|...'
ForegroundColorColorSpec
Position[xywidthheight]
[xy]-centersaroundscreenpoint
TextStringstring
Unitspixels|normal|cent|inches|points
UserDatamatrix
Note:
Untildialogbecomesbuilt-in,setandget
arenotvalidfordialongobjects.
Atmostthreebuttonsareallowed.
Thecallbacksareignoredfor"
question"
dialogs.
IfButtonStrings/ButtonCallsareunspecifiedthenit
defaultstoasingle"
ok"
buttonwhichremovesthefigure.
There'
sstillproblemswithmakingthequestiondialogmodal.
Theentireparameternamemustbepassedin.
(i.e.noautomaticcompletion).
Nothingbeepsyet.
SeealsoERRORDLG,HELPDLG,QUESTDLG
注意:
对话窗本身不是句柄图形对象,而是由一系列句柄图形对象构成的M文件。
对话窗口是图形,包括与框架、编辑和按钮uicontrol对象共存的坐标轴。
在将来的版本中,dialog可能成为具有更多功能的内置函数。
缺省的对话框是一个帮助对话框,它是由'
Defaulthelpstring'
字符串和标有'
OK'
的按钮键组成的编辑文本框。
作为例子,键入>
dialog。
预先定义的对话框是由函数helpdlg,enordlg,warndlg和gucstdlg建立。
helpdlg和warndlg接受文本字符串和窗口标题字符串作为输入参量。
errordlg接收'
Replace'
变量作为输入。
除了questdlg,所有上述函数都产生类似的对话框,它有各自缺省的标题和文本字符串。
标有'
的单个按钮,则关闭对话框。
helpdlg帮助文本是:
helphelpdlg
HELPDLG:
Displaysahelpdialogbox.
HANDLE=HELPDLG(HELPSTRING,DLGNAME)displaysthe
messageHelpStringinadialogboxwithtitleDLGNAME.
ifahelpdialogwiththatnameisaldeadyonthescreen,
itisbroughttothefront.Otherwiseitiscreated.
Seealso:
DIALOG
帮助信息
Helpdlg:
显示一个帮助文本框
HANDLE=HELPDLG(HELPSTRING,DLGNAME)在对话框中显示标题为dlgname的帮助信息helpstring。
如果名为dlgname的帮助对话框已在屏幕上显示,则引到屏幕正面,否则就建立该帮助对话框。
参阅:
dialog
warndlg的帮助文本是:
helpwarndlg
WARNDLGCreatesawarningdialogbox.
HANDLE=WARNDLG(WARNSTR,DLGNAME)createsawarning
diologboxwhichdisplaysWARNSTRinawindownamed
DLGNAME.ApushbuttonlabeledOKmustbepressedto
makethewarningboxdisappear.
errordlg的帮助文本是
helperrordlg
ERRORDLGCreatesanerrordialogbox
HANDLE=ERRORDLG(ERRORSTR,DLGNAME,Replace)createsanerrordialogboxwhichdiplaysERRORSTRinawindownnnamedDLGNAME.ApushbuttonlabeledOKmustbepressedtomaketheerrorboxdisappear.IfREPLACE='
andanerrordialogwithNameDLGNAMEalreadyexits,itissimplybroughttothefront(nonewdialogiscreated).
Seealso:
ERRORDLG:
建立出错对话框
HANDLE=ERRORDLG(ERRORSTR,DLGNAME,REPLACE)建立显示出错信息errorstr、名为dlgname的出错对话框,要消除出错信息对话框,必须揿下标记为OK的按钮,如果replace='
并且名为digname的出错对话框已经存在,则就引到屏幕正面,不再建立新的对话框。
提问的对话框稍有不同,前三种函数只显示一个按钮键并返回对话框图形对象的句柄。
函数questdlg显示两个或三个按钮键,返回由用户所选的按钮的标志字符串。
questdlg的帮助文本如下:
helpqeustdlg
QEUSTDLGCreatesaquestiondialogbox.
CLICK=QUESTDLG(Q,YES,O,CANCEL,DEFAULT)createsaquestion
dialogboxwhichdisplaysQ.Uptothreepushbuttons,
withstringsgivenbyYES,NO,andCANCEL,willappear
alongwithQinthedialog.Thedialogwilbedestroyed
returningthestringCLICKdependingonwhichbuttonis
clicked.DEFAULTisthedefaultbuttonnumber.
QUESTDLG:
建立问题对话框
CLICK=QVESTDLG(Q,YES,NO,CANCEL,DEFAULT)建立显示信息Q的问题对话框。
至多有三个按钮具有由YES,NO和CANCEL给定的字符串,按钮与Q一起,显示在对话框中。
根据所击的按钮返回字符串CLICK对话框消失。
DEFAULT是缺省的按钮数。
下面是函数的片断,说明了函数中提问对话框的应用:
question1='
Changecolormaptocopper?
response1=questdlg(question1,'
Sure'
Nope'
Maybe'
,2);
ifstromp(response1,'
colormap(copper);
elseifstromp(response1,'
wandlg('
Thatresponsedoesnotcompute!
response2=questdly(['
Pleasemakeupyourmind.|'
question1],'
Yes'
No'
ifstromp(response2,'
end
请求程序
请求程序通过对话框获取用户的输入。
请求程序是内置式GUI函数,它使用平台原有的窗口系统建立外观熟悉的请求程序。
函数uigecfile和uiputfile是所有平台上都有的内置式函数,用于交互地获得文件名,从而调用函数用它读取文件中数据或将数据存于文件中。
uigetfile的帮助文本是:
helpuigetfile
UIGETFILEInteractivelyretrieveafilenamebydisplayingadialogbox.
[FILENAME,PATHNAME]=UIGETFILE('
filterSpec'
dialogTitle'
,X,Y)
displaysadialogboxfortheusertofillin,andreturnsthe
filenameandpathstrings.Asuccessfulreturnoccursonlyif
thefileexists.Iftheuserselectsafilethatdoesnotexist,
anerrormessageisdisplayed,andcontrolreturnstothedialogbox.
Theusermaythenenteranotherfilename,orpresstheCancelbutton.
Allparametersareoptional,butifoneisused,allprevious
parametersmustalsobeused.
ThefelterSpecparameterdeterminestheinitialdisplayoffilesin
thedialogbox.Forexample'
*.m'
listsalltheMATLABM-files.
Parameter'
isastringcontainingthetitleofthedialog
box.
TheXandYparametersdefinetheinitialpositionofthedialog
boxinunitsofpixels.Somesystemsmaynotsupportthisoption.
TheoutputvariableFILENAMEisastringcontainingthemaneofthe
fileselectedinthedialogbox.IftheuserpressestheCancelbutton
orifanyerroroccurs,itissetto0.
TheoutputparameterPATHNAMEisastringcontainingthepathof
thefileselectedinthedialogbox.IftheuserpressestheCancel
buttonorifanyerroroccurs,itissetto0.
SeealsoUIPUTFILE.
UIGETFILE:
通过显示对话框交互式地检索文件名
[FILENAME,ATHNAME]=UIGETFILE('
filterspec'
dialogtitle'
,x,y)显示一个对话框,让用户输入,并返回路径和文件名字符串。
仅当文件存在时,才成功地返回。
如果用户选择了一个并不存在的文件,就显示出错信息,控制框返回到对话框。
用户可以输入另一个文件名或揿下Cancel按钮。
所有输入参数都是可任选的,如果用其中之一个,也必须使用所有先前参数。
参数filteospec决定对话框中文件的初始显示。
例如'
列出的所有M文件。
参数'
是对话框标题字符串。
以象素为单位参数x,y定义对话框的初始位置,有些系统可能不支持这个选项。
输出变量filename是对话框内所选文件的名称字符串。
如果用户揿了取消按钮或有错误发生,filename的值设置为0。
输出参数pathname是对话框内所选文件的路径名字符串。
如果用户揿了取消按钮或有错误发生,pathname的值设置为0。
参阅uiputfile
下面的例子说明了在函数中如何利用uigetfile,交互式地检索ASCII码数据文件,并绘制正弦数据
%Asktheuserforafilename.
[datafiledatapath]=uigetfile('
.dat'
Chooseadatafile'
%Iftheuserselectedanexistingfile,readthedata.
%(Theextraquotesavoidproblemswithspacesinfileorpathnames
%ontheMacintoshplatform.)
%Thendeterminethevariablenamefromthefilename,
%copythedatatoavariable,andplotthedata.
ifdatafile
eval(['
load('
datapathdatafile'
)'
]'
x