在matlab命令行上输入guide或者从菜单中选择New GUI可以创建matlab的图形用户界面。
创建空白的GUI界面(默认),其界面如下:
未命名情况下matlab将会在当前工作目录下,同时将会生成untitled.fig和untitled.m文件。其中untitled.fig是图形用户界面文件,用于生成GUI界面,和用户进行交互,untitled.m是实现文件,用来处理用户的请求,函数实现在这里。
左边是工具箱,右边是画布,上面的工具栏是一些常用操作的快捷方式,例如撤销恢复,拷贝粘贴,对齐,对象浏览,运行。有过其他语言GUI编程的朋友应该不陌生。
再来看看matlab在untitled.m生成哪些内容。
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help untitled% Last Modified by GUIDE v2.5 27-Aug-2014 20:54:04% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @untitled_OpeningFcn, ...'gui_OutputFcn', @untitled_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)% Choose default command line output for untitled
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structure
varargout{1} = handles.output;
这些是matlab初始化的一些代码,从其中注释的可知,前面一段是初始化的一些代码,最好不要做编辑更改。后面两个是gui开始时调用函数,和退出时调用函数。
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @untitled_OpeningFcn, ...'gui_OutputFcn', @untitled_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
其中比较重要的就是gui_mainfcn(gui_State, varargin{:})函数,GUI的主函数,用来获取用户操作,处理调用相应的回调函数,处理这些请求。
然后现在说怎么编写这些回调函数。回到fig界面,拖动控件到窗口画布,右键单机property Inspector,弹出属性设置对话框,在这里可以设置控件的属性,指定触发事件函数。比较重要的就是Callback,在这里指定回调函数。
在Matlab GUI编程里面,每个控件都有唯一的Tag(也就是matlab里面的变量名)。通过Tag可以获取这些控件上的字符串或者数值。
可以通过get去获取控件上的属性,例如str=get(handles.num1,'string'),就是用来获取Tag名为num1的控件的字符创值赋值给str变量。同样适用set方法可以设置这些值。在此不再多叙述。
示例:编写一个加法计算器
1.首先设置界面,对各个属性最好起自己熟悉的Tag名,设置按钮的回调函数。
2.在m文件里面相应的回调函数写下如下代码:
num1=str2num(get(handles.num1,'string'));
num2=str2num(get(handles.num2,'string'));
set(handles.sum,'string',num2str(num1+num2));
这样就完成了。保存,运行就可以看到效果。
附MATLAB的一些常用函数:
1.figure函数:创建一个新的图形对象。
2.newplot函数:做好开始画新图形对象的准备。
3.axes函数:创建坐标轴图形对象。
4.line函数:画线。
5.patch函数:填充多边形。
6.surface函数:绘制三维曲面。
7.image函数:显示图片对象。
8.uicontrol函数:生成用户控制图形对象。
9.uimenu函数:生成图形窗口的菜单中层次菜单与下一级子菜单。
几个实用的小函数:
uigetfile 选择文件对话框
uiputfile 保存文件对话框
uisetcolor 设置颜色对话框
fontsetcolor 设置字体对话框
msgbox 消息框
warndlg 警告框
helpdlg 消息框
更多内容可以参考
1.《数字图像处理的MATLAB实现》
2. Matlab GUIDE使用总结--Matlab GUI界面