matlab如何创建callback函数_MATLAB教程-台大郭彦甫-第七节,含练习答案

07-图形界面_GUI_程式设计

一、Graphical user interface(图形用户界面)

(一)UI(User Interface)

1、User interface is a method of interaction between a person and a computer(用户界面是人与计算机之间的一种交互方法)

(二)Starting A GUI Program

1、Set your “current folder” where you want to store the GUI program(设置要在其中存储GUI程序的“当前文件夹”)

2、Type guide (graphical user interface design environment)in the command window to create a MATLAB GUI interactively(在命令窗口中键入guide(图形用户界面设计环境)以交互方式创建MATLAB GUI)

(三)GUI Figure

d1d02513773a2133eb984d4b85a8e196.png

768241bec4d56bbb0e2f52f6a75e9366.png

92abc38a88aaa320829e1b9fa9c6cc3c.png

(四)Align the Components(对齐组件)

Tools--Align Objects

3acdad46f8f524b7fcefd1d815e7c95c.png

(五)Label the Push Buttons(标记按钮)

View--Property Inspector,或者双击对象

82444b7dfdba4103d23acad4cf71803c.png

(六)GUI Scrip Structure(脚本结构)

function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
...
% Begin initialization code - DO NOT EDIT
...
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
...

%放入程式码的位置(1)

% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles) 
...
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

%放入程式码的位置(2)

...
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
...
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)

1、示例代码:(在上部分代码的粗体且斜体(1)的地方,即openingFcn之后)

handles.peaks = peaks(35);%handles为openingFcn的内参,cell型
handles.membrane = membrane;
%membrane是一个函数直接输入就会出三维图像,一般和mesh、surf一起用来绘制图像
[x,y] = meshgrid(-8:.5:8);
r = sqrt(x .^ 2 + y .^ 2) + eps;
sinc = sin(r) ./ r;
handles.sinc = sinc;
handles.current_data = handles.peaks;
surf(handles.current_data)

输出结果:

661ce5d0fd168092f27a48939dcb4c7e.png

注意:

(1)peaks是从高斯分布转换和缩放得来的包含两个变量的函数,在演示meshsurfpcolorcontour等函数中很有用。

79aa52cec0eb0fd9913d6e4e29ece104.png

(2)membrane是一个函数直接输入就会出三维图像,一权般和mesh、surf一起用来绘制图像

(3)[x,y]=meshgrid(-8:.5:8);生成二维或三维网格

beafbcea32bb4b43d96c6214a5d4112f.png

(4)eps是一个函数,它表示的是一个数可以分辨的最小精度。默认时它表示1到它下一个浮点数之间的距离的一半,而正好等于最大小于1的浮点数到最小大于1的浮点数之间的距离。eps=2.2204e-16

(5)surf()绘制曲面图

f4f373e3c9864d034c6a11f4fdc0ac45.png

2、示例代码:(在上部分代码的粗体且斜体(2)的地方,即pushbutton1_Callback之后)

示例代码:

handles.peaks = peaks(35);
handles.membrane = membrane;
%membrane是一个函数直接输入就会出三维图像,一般和mesh、surf一起用来绘制图像
[x,y] = meshgrid(-8:.5:8);
r = sqrt(x .^ 2 + y .^ 2) + eps;
sinc = sin(r) ./ r;
handles.sinc = sinc;
handles.current_data = handles.peaks;
surf(handles.current_data)

输出结果:

b4ed6951790e2a2ac8a8da19b2f25457.gif

3、What If We Have Two axes?(如果有两个axes,会咋样呢)

---会显示在第二个,即最后一个axes上

(七)handles - Parents of the GUI Object(GUI对象的父对象)

691a3995bc6f3a6f3e317a00180b5a9a.png

1、Setting the axes for Plotting(设定绘制的轴ID)

示例代码:

%将上面的代码中
surf(handles.current_data) 
%改为 
%(1)
axes(handles.axes1);%呼叫axes1,后面所作的操作都是在axes上操作
surf(handles.current_data);
%或者
%(2)
surf(handles.axes1,handles.current_data);%surf的操作在axes1上进行

输出结果:

7f1bea3b76b9365c4cb5c23e7375f138.gif

(八)Review - set() and get()

1、get() acquires properties:获取特性

2、set() sets properties:设置特性

示例代码:

a = get(handles.slider1,'Value');
set(handles.text2,'String',num2str(a));

输出结果:

beac053a8f2a90b64619e8cc0fc702cc.gif

注意:

·添加位置在 slider1_Callback

(九)Practice

1、Write a GUI program

2、It contains 2 sliders each of which is associated with a variable with value ranged from 0 to 100

3、Display the summation of the slider variables as an integer

4、You may need these function:get()、set()、 int16()、num2str()

81e22419c38b05e4ffc3437f916ee660.png

答案代码:

a = int16(get(handles.slider1,'Value'));
b = int16(get(handles.slider2,'Value'));
c = (a + b);
set(handles.text5,'String',num2str(c));
set(handles.text9,'String',num2str(a));
set(handles.text11,'String',num2str(b));

输出结果:

1a4f5b403bdf32817ee8357c75080d0e.gif

注意:

slider1_Callback slider2_Callback 中都要加入上述程序

(十)Using handles to Store Variables

1、Variables in callback functions are local variables(回调函数中的变量是局部变量)

2、handles is also used to pass variables from one GUI object to another(handles也用于将变量从一个GUI对象传递到另一个GUI对象)

3、Store variable a into handles

示例代码:(guidata()固定配合,不可变更)

handles.myData = a;
guidata(hObject,handles);

4、Retrieve variable a from handles

示例代码:

a = handles.myData;

(十一)Compiling the GUI Program(编译GUI程序)

deploytool

7a1849a5a8152bffa00bd140a135a948.png

db1791f54b46a741b6205868a415268b.png

c5d9c78808e54f4f7425c21cb7a9fa05.png

第七节结束

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值