如何在Matlab中用户界面中取得鼠标坐标.docVIP

  • 81
  • 0
  • 约1.63千字
  • 约 6页
  • 2016-11-22 发布于江苏
  • 举报

如何在Matlab中用户界面中取得鼠标坐标.doc

如何在Matlab中创建用户界面(GUI)的相关帮助请参见:MATLAB--Creating Graphical User Interfaces 下面举例如何在用户界面中取得鼠标点击时的坐标。 创建一个简单的GUI 在MATLAB命令行敲入“guide”,可以看到下面的对话框 选择GUI with Axes and Menu后,点击“OK”。 将它另存为TestFig.fig 此时MATLAB会自动生成一个TestFig.m文件,回头我们要修改其中的代码,现在先不管它。 回到TestFig.fig界面,可以点击右上角的绿色三角按钮,看看运行情况。 为按钮设置被点击后执行的代码 找到点击Update按钮后执行的代码:用鼠标右击Update按钮后,选择View Callbacks(Callback。 此时MATLAB自动定位到该按钮被按下后执行的代码处。 我们现在修改掉原来的行为,变成记录下用户每次点击鼠标的坐标(用ginput这个函数可以获得鼠标的坐标),并在界面上画出每个点。 我们首先删除这些代码(或者用百分号%注释掉这些代码) popup_sel_index = get(handles.popupmenu1, Value); switch popup_sel_index case 1 plot(rand(5)); case 2 plot(sin(1:0.01:25.99)); case 3 bar(1:.5:10); case 4 plot(membrane); case 5 surf(peaks); end 然后在cla;这句代码后面加上下面的代码 % we use points[x, y] to store each point points = []; pointNum = 0; % we want to show all points on the same fig hold on; % we also want to fix the region in which points are located axis([0 1 0 1]); % 1,2,3 means left, middle, right mouse button is clicked button = 1; while(button ~= 3) % user use right click to input the last point % use ginput to get graphical input from mouse [x, y, button] = ginput(1); % plot the point plot(x,y,bo); % save pointNum = pointNum + 1; points(pointNum, :) = [x y]; end disp([These points are stored:]); disp(points); 完成后像这个样子 现在可以测试一下看看了,点击绿色小三角后,先点击Update按钮,然后用鼠标左键点击界面画点,用右键输入最后一个点。并且可以在MATLAB的命令行窗口看到程序输出的所有点的坐标。 其他问题 如果要运行程序,在MATLAB命令行窗口输入”TestFig”即可。 如果要调出界面窗口并修改之,在MATLAB命令行窗口输入”guide”后打开”TestFig.fig”文件即可。 由于我们直接删除了部分代码,关闭程序的时候可能会提示错误。 剩下的工作就看你们的了。

文档评论(0)

1亿VIP精品文档

相关文档