Matlab GUI界面设计

摘要:本篇博文基于MATLAB2014a进行GUI设计。

1.启动GUI设计

命令行输入guide,回车。

即可得到下面的对话框,进行相关的选择和设置

点击确定,得到两个文件,一个是.m文件,另一个是.fig文件,需要说明的是,.m文件用于编辑GUI中控件所需要的回调代码,.fig文件可以用鼠标拖拽等比较简单的操作进行初始的界面设计。

2..fig文件控件布局

本篇暂时涉及按钮、可编辑文本、静态文本、弹出式菜单、轴的实现,拖拽这些控件到界面中,可以点击绿色三角形运行GUI看看效果。

双击任意控件可以弹出控件的属性检查器,这里可以更改控件的初始属性,并且可以查看控件的tag值,用于回调程序的句柄调用。

3.GUI初始程序编写

3.1.初始程序是界面运行时最先执行的程序,用于对控件等的一些初始设置,该部分的代码应该添加在.m文件的test_OpeningFcn(hObject, eventdata, handles, varargin)函数中。

插入如下代码,使得可编辑文本失效

set(handles.edit1,'enable','off');

3.2.按钮程序编写,使得可编辑文本生效,右击按钮->查看回调->Callback,输入

set(handles.edit1,'enable','on');

3.3.可编辑文本程序编写,当可编辑文本框中输入文字后,在静态文本中显示出来

右击可编辑文本->查看回调->Callback,输入

set(handles.text1,'string',get(handles.edit1,'string'));

3.4.弹出式菜单编写

双击弹出式菜单,调出属性编辑器,进行以下操作

             

输入相应文字,点击确定。

该部分函数编写,右击弹出式菜单->查看回调->Callback,输入

3.5.轴部分程序编写

为了简单起见,这部分内容写在程序开头,作用是显示了一个正弦曲线

代码如下:

axes(handles.axes1)
t=0:0.001:4*pi;
f=sin(t);
plot(t,f,'g')
axis([0 4*pi -1 1])
grid on
xlabel('t')
ylabel('sin(t)')
title('正弦函数图像')
legend('f=sin(t)')

至此,基本功能均可实现

4.总结

①每个控件均可在属性编辑器里面设置初始值;

②如果需要全局变量,在定义和使用的时候都需要写关键字global。

5.附上全部代码。

function varargout = test(varargin)
% TEST MATLAB code for test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST.M with the given input arguments.
%
%      TEST('Property','Value',...) creates a new TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_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 test

% Last Modified by GUIDE v2.5 26-Jan-2017 16:20:09

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_OpeningFcn, ...
                   'gui_OutputFcn',  @test_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before test is made visible.
function test_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 test (see VARARGIN)
set(handles.edit1,'enable','off');
axes(handles.axes1)
t=0:0.001:4*pi;
f=sin(t);
plot(t,f,'g')
axis([0 4*pi -1 1])
grid on
xlabel('t')
ylabel('sin(t)')
title('正弦函数图像')
legend('f=sin(t)')
% Choose default command line output for test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_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;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'enable','on');


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text1,'string',get(handles.edit1,'string'));
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(handles.popupmenu1, 'String');
val = get(handles.popupmenu1,'Value');
switch str{val};
case '选项一'
    set(handles.text1,'string','选项一触发');
case '选项二'
    set(handles.text1,'string','选项二触发');
case '选项三'
    set(handles.text1,'string','选项三触发');
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

相关下载如下:

test.fig

test.m


 

欢迎交流指正。。。
 


 

  • 134
    点赞
  • 1148
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值