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
(四)Align the Components(对齐组件)
Tools--Align Objects
(五)Label the Push Buttons(标记按钮)
View--Property Inspector,或者双击对象
(六)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)
输出结果:
注意:
(1)peaks
是从高斯分布转换和缩放得来的包含两个变量的函数,在演示mesh
、surf
、pcolor
、contour
等函数中很有用。
(2)membrane是一个函数直接输入就会出三维图像,一权般和mesh、surf一起用来绘制图像
(3)[x,y]=meshgrid(-8:.5:8);生成二维或三维网格
(4)eps是一个函数,它表示的是一个数可以分辨的最小精度。默认时它表示1到它下一个浮点数之间的距离的一半,而正好等于最大小于1的浮点数到最小大于1的浮点数之间的距离。eps=2.2204e-16
(5)surf()绘制曲面图
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)
输出结果:
3、What If We Have Two axes?(如果有两个axes,会咋样呢)
---会显示在第二个,即最后一个axes上
(七)handles - Parents of the GUI Object(GUI对象的父对象)
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上进行
输出结果:
(八)Review - set() and get()
1、get() acquires properties:获取特性
2、set() sets properties:设置特性
示例代码:
a = get(handles.slider1,'Value');
set(handles.text2,'String',num2str(a));
输出结果:
注意:
·添加位置在 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()
答案代码:
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));
输出结果:
注意:
在 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
第七节结束