I recently had to program a simple GUI that controls some plots. I don't know exactly what your task is, but here's some basic code to get you started. This creates two figures; Figure 1 has controls, Figure 2 has a plot of y=x^p. You enter the value of p into the box and press enter to register it and replot; then press the button to reset to default p=1.
function SampleGUI()
x=linspace(-2,2,100);
power=1;
y=x.^power;
ctrl_fh = figure; % controls figure handle
plot_fh = figure; % plot figure handle
plot(x,y);
% uicontrol handles:
hPwr = uicontrol('Style','edit','Parent',...
ctrl_fh,...
'Position',[45 100 100 20],...
'String',num2str(power),...
'CallBack',@pwrHandler);
hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,...
'Position',[45 150 100 20],...
'String','Reset','Callback',@reset);
function reset(source,event,handles,varargin) % boilerplate argument string
fprintf('resetting...\n');
power=1;
set(hPwr,'String',num2str(power));
y=x.^power;
compute_and_draw_plot();
end
function pwrHandler(source,event,handles,varargin)
power=str2num(get(hPwr,'string'));
fprintf('Setting power to %s\n',get(hPwr,'string'));
compute_and_draw_plot();
end
function compute_and_draw_plot()
y=x.^power;
figure(plot_fh); plot(x,y);
end
end
The basic idea behind GUIs is that when you manipulate controls they call "Callback" functions, i.e. event handlers; these functions are able to interact through controls using the control handles and set/get methods to obtain or change their properties.
发布评论
评论(4)
您需要去的第一个地方是关于 创建图形用户界面的 Matlab 帮助
。
然后,您可以观看此教程视频或这个
这个教程也不错。
The first place you need to go is Matlab Help on Creating Graphical User Interfaces
.
Then, you can watch this tutorial video or this one
This tutorial is also good.
以下是我制作的有关制作 MATLAB GUI 的所有视频
http://blogs .mathworks.com/videos/category/gui-or-guide/
Here are all the videos that I have made about making MATLAB GUIs
http://blogs.mathworks.com/videos/category/gui-or-guide/
我最近不得不编写一个简单的 GUI 来控制一些绘图。 我不知道您的具体任务是什么,但这里有一些基本代码可以帮助您入门。 这会创建两个数字; 图 1 有控件,图 2 有 y=x^p 的图。 您在框中输入 p 的值,然后按 Enter 注册它并重新绘制; 然后按 按钮重置为默认 p=1。
GUI 背后的基本思想是,当您操作控件时,它们会调用“回调”函数,即事件处理程序; 这些函数能够使用控件句柄和 set/get 方法通过控件进行交互,以获取或更改其属性。
要获取可用属性列表,请仔细阅读 Matlab 文档网站 (http://www.mathworks.com/access/helpdesk/help/techdoc/infotool/hgprop/doc_frame.html); 单击 UI 对象(或您需要的任何其他对象)。
希望这可以帮助!
I recently had to program a simple GUI that controls some plots. I don't know exactly what your task is, but here's some basic code to get you started. This creates two figures; Figure 1 has controls, Figure 2 has a plot of y=x^p. You enter the value of p into the box and press enter to register it and replot; then press the button to reset to default p=1.
The basic idea behind GUIs is that when you manipulate controls they call "Callback" functions, i.e. event handlers; these functions are able to interact through controls using the control handles and set/get methods to obtain or change their properties.
To get to the list of available properties, peruse the very informative Handle Graphics Property Browser on Matlab's documentation website (http://www.mathworks.com/access/helpdesk/help/techdoc/infotool/hgprop/doc_frame.html); click on UI Objects (or whatever else that you need).
Hope this helps!
这些 41 个完整的 GUI 示例发布到 MathWorks 文件交换 作者:Matt Fig 是一个很好的起点。 提交的内容甚至是本周精选。
These 41 complete GUI examples posted to the MathWorks File Exchange by Matt Fig are a great place to start. The submission was even a Pick of the Week.