Genial 发表于 2006-3-31 20:31

[分享]MATLAB GUI编程中几个有用的程序段

<P>昨天找其他资料看到的,尽管简单,觉得不错,分享给各位<BR>不好意思没记下网址,对该内容总结者表示感谢<BR></P>
<P  align=left>MATLAB GUI编程中几个有用的程序段<o:p></o:p></P>
<P  align=left><FONT face="Times New Roman">1</FONT>、<FONT face="Times New Roman">  </FONT>启动<o:p></o:p></P>
<P  align=left>% 获取当前文件所在路径<o:p></o:p></P>
<P  align=left>currPath = fileparts(mfilename('fullpath'));<o:p></o:p></P>
<P  align=left>% 切换工作路径到当前位置<o:p></o:p></P>
<P  align=left> <o:p></o:p></P>
<P  align=left>cd(currPath);<o:p></o:p></P>
<P  align=left> <o:p></o:p></P>
<P  align=left>% 判断所用操作系统<o:p></o:p></P>
<P  align=left>if computer == 'PCWIN'<o:p></o:p></P>
<P  align=left>    % MATLAB版本号<o:p></o:p></P>
<P  align=left>    v = version;<o:p></o:p></P>
<P  align=left>    if v(1)~='7'        <o:p></o:p></P>
<P  align=left>        warndlg ('Only run in matlab 7.x(R14.x)');<o:p></o:p></P>
<P  align=left>        return<o:p></o:p></P>
<P  align=left>else<o:p></o:p></P>
<P  align=left>        % 添加当前路径下的所有子目录<o:p></o:p></P>
<P  align=left>        addpath(genpath(pwd));<o:p></o:p></P>
<P  align=left>        addpath(genpath(currPath));<o:p></o:p></P>
<P  align=left>    end<o:p></o:p></P>
<P  align=left>end<o:p></o:p></P>
<P  align=left> <o:p></o:p></P>
<P  align=left><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
<P  align=left><FONT face="Times New Roman">2</FONT>、<FONT face="Times New Roman">  </FONT>在<FONT face="Times New Roman">GUI</FONT>中使用<FONT face="Times New Roman">Axes</FONT>控件<o:p></o:p></P>
<P  align=left> <o:p></o:p></P>
<P  align=left>% 1.删除所有画线及对应图例<o:p></o:p></P>
<P  align=left>% 查找Axes控件中的画线<o:p></o:p></P>
<P  align=left>sameLines = findobj('type','line');<o:p></o:p></P>
<P  align=left>% 逐一删除这些画线<o:p></o:p></P>
<P  align=left>    for i = 1 :length(sameLines)<o:p></o:p></P>
<P  align=left>        delete(sameLines(i))<o:p></o:p></P>
<P  align=left>end<o:p></o:p></P>
<P  align=left>% 获取Axes控件中的图例(由于画线全部被删除,因此为空)<o:p></o:p></P>
<P  align=left>lgStr = get(legend(handles.ResultsAxes), 'String');<o:p></o:p></P>
<P  align=left>% 重新设置图例(为空)<o:p></o:p></P>
<P  align=left>    legend(handles.ResultsAxes,lgStr); <o:p></o:p></P>
<P  align=left><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
<P  align=left>% <FONT face="Times New Roman">2</FONT>.添加画线<o:p></o:p></P>
<P  align=left>% 获取原来的图例<o:p></o:p></P>
<P  align=left>lgStr = get(handles.hLegend, 'String');<o:p></o:p></P>
<P  align=left>% 设置下一个画线为添加方式<o:p></o:p></P>
<P  align=left>set(handles.ResultsAxes, 'Nextplot', 'add');<o:p></o:p></P>
<P  align=left>% 指定要画线的Axes<o:p></o:p></P>
<P  align=left>axes(handles.ResultsAxes);<o:p></o:p></P>
<P  align=left>% 画线<o:p></o:p></P>
<P  align=left>plot(distance_target, '-r')<o:p></o:p></P>
<P  align=left>% 添加图例<o:p></o:p></P>
<P  align=left>handles.hLegend = legend(handles.ResultsAxes, lgStr,'目标距离');<o:p></o:p></P>
<P  align=left><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
<P  align=left>% 3.删除某一画线<o:p></o:p></P>
<P  align=left>% 所删除画线对应的数据为distance_target,获取它的句柄<o:p></o:p></P>
<P  align=left>sameLines = findobj('type','line','YData', distance_target);<o:p></o:p></P>
<P  align=left>% 删除画线<o:p></o:p></P>
<P  align=left>    if ~isempty(sameLines)<o:p></o:p></P>
<P  align=left>        delete(sameLines);<o:p></o:p></P>
<P  align=left>end<o:p></o:p></P>
<P  align=left>% 获取原有的图例<o:p></o:p></P>
<P  align=left>lgStr = get(legend(handles.ResultsAxes), 'String');                    <o:p></o:p></P>
<P  align=left>% 从原有图例中删除对应图例<o:p></o:p></P>
<P  align=left>    legend(handles.ResultsAxes, setdiff(lgStr, {'目标距离'}));<o:p></o:p></P>
<P  align=left><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
<P  align=left><FONT face="Times New Roman">3</FONT>、<FONT face="Times New Roman">  </FONT>在<FONT face="Times New Roman">GUI</FONT>中使用<FONT face="Times New Roman">Excel</FONT>表格(<FONT face="Times New Roman">Activex</FONT>控件<FONT face="Times New Roman">Microsoft Office Spreedsheet</FONT>)<o:p></o:p></P>
<P  align=left>% 获取现在使用的spreedsheet的句柄<o:p></o:p></P>
<P  align=left>ActiveSheet = get(handles.activex1,'ActiveSheet');<o:p></o:p></P>
<P  align=left>   % 现在使用的spreedsheet的现在使用工作薄<o:p></o:p></P>
<P  align=left>   ActiveWorkbook = get(handles.activex1,'ActiveWorkbook');            <o:p></o:p></P>
<P  align=left>   % 现在使用的表<o:p></o:p></P>
<P  align=left>eSheets = handles.activex1.ActiveWorkbook.Sheets;<o:p></o:p></P>
<P  align=left>   % 使用表的第一个子表<o:p></o:p></P>
<P  align=left>   eSheet1 = eSheets.get('Item', 1);<o:p></o:p></P>
<P  align=left>   % 激活该表<o:p></o:p></P>
<P  align=left>   eSheet1.Activate;<o:p></o:p></P>
<P  align=left>% 获取当前单元格的句柄<o:p></o:p></P>
<P  align=left>ActiveCell = get(handles.activex1, 'ActiveCell');<o:p></o:p></P>
<P  align=left>% 选择坐标为A1的单元格<o:p></o:p></P>
<P  align=left>Select(Range(ActiveSheet,'A1'));<o:p></o:p></P>
<P  align=left>% 获取当前单元格的句柄<o:p></o:p></P>
<P  align=left>ActiveCell = get(handles.activex1, 'ActiveCell');<o:p></o:p></P>
<P  align=left>% 设置当前单元格中的内容<o:p></o:p></P>
<P  align=left>    set(ActiveCell, 'Value', '仿真结果报告');  <o:p></o:p></P>
<P  align=left><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
<P  align=left><FONT face="Times New Roman">4</FONT>、<FONT face="Times New Roman">  </FONT>为<FONT face="Times New Roman">Axes</FONT>控件设置图片<o:p></o:p></P>
<P  align=left>% 所有设置的Axes控件<o:p></o:p></P>
<P  align=left>axes(handles.MyLog);<o:p></o:p></P>
<P  align=left>% 读取图片数据<o:p></o:p></P>
<P  align=left>Image_tip1=imread('Setting/mylog.jpg');<o:p></o:p></P>
<P  align=left>% 显示图片<o:p></o:p></P>
<P  align=left>image(Image_tip1);<o:p></o:p></P>
<P ><o:p><FONT face="Times New Roman"> </FONT></o:p></P>

abingchem 发表于 2006-4-1 23:28

恩,好东西,收了:)

hanghan6659 发表于 2006-4-10 10:38

有没有好一点的程序实例呀

renmeng 发表于 2006-4-30 23:40

<P>谢了</P>

chunxueaomei 发表于 2006-5-10 17:41

谢谢![em25][em30]

chunxueaomei 发表于 2006-5-10 17:42

很有用的![em30]

chunxueaomei 发表于 2006-5-10 17:51

我想知道gui进行函数回调时的相关语法!刚入门,一点思路都没有!<BR>可否有高人帮助一下!<BR>最好举个小例子说明!谢谢!

chunxueaomei 发表于 2006-5-10 17:55

能否提供一个数字识别的gui主窗体啊?太郁闷了!作了窗体,回调函数不知怎么写!谢谢帮忙![em25][em30]

pcb 发表于 2006-5-29 11:30

<P>有没有关于从 装载数据,并用该数据在设计的GUI界面绘图的例子啊,谢谢<BR>如果有的话,麻烦发我邮箱谢谢了<BR><a href="mailto:yqlwyqlw@yahoo.com.cn" target="_blank" >yqlwyqlw@yahoo.com.cn</A><BR></P>

shanlog 发表于 2006-7-5 09:24

谢谢,真棒![em01]

fylddy 发表于 2006-9-8 10:23

好东西!谢谢!!

jcfan 发表于 2006-9-26 22:06

好东西!谢谢!![em07]

polyteck 发表于 2006-10-23 16:07

[em17]

liuhaixiao 发表于 2007-1-7 00:26

<P>% 指定要画线的Axes</P>
<P>axes(handles.ResultsAxes);<BR><BR>楼主能解释一下这个嘛?我设置了几个坐标系,怎样指定其中一个在其上画图呢?</P>

hahaworld 发表于 2007-1-8 21:07

leki 发表于 2007-4-15 10:00

这个我有了,不过还是支持一下

zp_xm 发表于 2007-5-25 21:16

好东西  爱司你了

llxz103 发表于 2008-5-9 10:52

有没有关于GUI方面的例子?比如界面链接,点击一个界面上的按钮,可以进入下一个界面的

jcf 发表于 2008-7-15 17:25

xie xie ti gong !!

yuqian19861115 发表于 2008-8-21 10:57

谢谢 很好 很有用的

页: [1]

编程论坛