MATLAB GUI设计快速入门实例

时间:2019.11.27
目的: 课题需要设计一个界面
为了实现课题的一个功能,需要设计一个图形界面,因此使用matlab GUIDE进行设计,但前期没有接触过,因此通过实例练习,理解MATLAB gui设计相关知识。

一、几个GUI设计需要了解的知识

1、进入GUI 设计界面:在命令行输入guide,创建一个空白的GUI,保存即可,在保存目录下会生成两个文件(.fig和.m)。然后进入编辑界面,如图所示:
在这里插入图片描述
2、在界面中有许多GUI 设计对象(按钮、滑动条等),选取相应的图表,往右侧编辑区域拉即可。相关功能的自定义可右键对象(如按钮),选择回调函数,进入.M文件,通过编写程序实现。
3、几个重要的参数,在.m文件中,回调函数一般有4个参数,handles你可以认为是所有图标的结构体,而hObject则是结构体中的成员。其他相关知识可通过一下实例进行练习。

二、实例代码

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

% Last Modified by GUIDE v2.5 27-Nov-2019 10:30:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @example_6_1_OpeningFcn, ...
                   'gui_OutputFcn',  @example_6_1_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 example_6_1 is made visible.
function example_6_1_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 example_6_1 (see VARARGIN)

% Choose default command line output for example_6_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


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

%% 实例1 设置标签的颜色
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
function pushbutton1_ButtonDownFcn(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)
c = get(hObject,'foregroundcolor');   %c是按钮标签颜色
c_user = uisetcolor(c,'选择颜色');    
set(hObject,'foreg',c_user);

%% 实例2 设置标签的字体
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text2.
function text2_ButtonDownFcn(hObject, eventdata, handles)  
% hObject    handle to text2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
font_user = uisetfont(hObject,'设置字体');

%% 实例3 切换按钮
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)  %切换按钮实例
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1
val = get(hObject,'Value');
if val
    set(handles.t1,'BackgroundColor','g');
else
      set(handles.t1,'BackgroundColor','r');
end

%% 实例4 滑动条
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles) 
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val = get(hObject,'Value');
set(handles.text1,'string',num2str(val,'3.0%f'));
function slider2_CreateFcn(hObject, eventdata, handles) %创建滑动条的函数

%% 实例5 单选按钮
% --- Executes on button press in save_data.
function save_data_Callback(hObject, eventdata, handles)
% hObject    handle to save_data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of save_data
val = get(hObject,'Value');
if val
    [FileName,PathName,Index] = uiputfile({'*.txt';'*.xls'},'数据另存为');
    if Index
        set(handles.text5,'string',[PathName FileName]);
    end
end

%% 实例6 可编辑文本 可允许用户修改文本内容,用于数据的输入和显示
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)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
str = get(hObject,'String');
val = str2num(str);
if ~isempty(val)&&(val>=0&val<=1)
    set(handles.val_disp,'value',val);
end
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function val_disp_Callback(hObject, eventdata, handles)

function val_disp_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

%% 实例7 复选框 与单选按钮类似,通过鼠标左键单击在选中与未选中两种状态切换
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1
val = get(hObject,'Value');
if val
    set(handles.slider4,'Enable','on');
else
       set(handles.slider4,'Enable','off');
end
% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
% hObject    handle to slider4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

%% 实例8 列表框 用于显示一组选项,通过鼠标左键单击,可选中任一选项
% --- Executes on selection change in subject.
function subject_Callback(hObject, eventdata, handles)
% hObject    handle to subject (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns subject contents as cell array
%        contents{get(hObject,'Value')} returns selected item from subject
%在列表框上双击时,figure的selectiontype的属性会更新为open
sel = get(gcf,'selectiontype'); %获取鼠标按键类型
if strcmp(sel,'open')           %若双击了鼠标,
    str = get(hObject,'string');%获取列表框所有文本
    val = get(hObject,'value'); %获取列表框当前索引值
    set(handles.sub_sel,'string',str{val});%将列表框的值写在静态文本中
end

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

% Hint: listbox 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

%% 实例9 弹起式菜单 即下拉菜单
% --- Executes on selection change in province.
function province_Callback(hObject, eventdata, handles)
% hObject    handle to province (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns province contents as cell array
%        contents{get(hObject,'Value')} returns selected item from province
sel = get(hObject,'Value');
stra = {'哈尔滨';'大庆';'黄冈';'阿城';'齐齐哈尔'};
strb = {'武汉';'襄阳';'黄冈';'荆州';'孝感'};
switch sel
    case 1 
        set(handles.city,'string','','value',1)
    case 2
        set(handles.city,'string',stra,'value',1)
    case 3
        set(handles.city,'string',strb,'value',1)
end
    
% --- Executes during object creation, after setting all properties.
function province_CreateFcn(hObject, eventdata, handles)
% hObject    handle to province (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


% --- Executes on selection change in city.
function city_Callback(hObject, eventdata, handles)
% hObject    handle to city (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns city contents as cell array
%        contents{get(hObject,'Value')} returns selected item from city


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

% Hint: listbox 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

%% 实例10 按钮组
function num_Callback(hObject, eventdata, handles)
% hObject    handle to num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of num as text
%        str2double(get(hObject,'String')) returns contents of num as a double


% --- Executes during object creation, after setting all properties.
function num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to num (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 when selected object is changed in bin_dec.
function bin_dec_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in bin_dec 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(handles.num,'string');
switch get(hObject,'tag') %必须为标量或者是字符串常量
    case 'bin'
        val = floor(str2double(str));%将输入值转换为整数
        if(~isempty(val)&&val>=0)    %输入的值为数值且非负数
            set(handles.num,'string',dec2bin(val))
        else
            set(handles.num,'string','输入错误');
        end
    case 'dec'
        if all(str == '0' | str == '1')
            set(handles.num,'string',numstr(bin2dec(str)));
        else
             set(handles.num,'string','输入错误');
        end
end

%% 实例11 面板 与按钮组一样都是GUI的容器,对象类型均为uipanel
%% 实例12 表格
%% 实例13 坐标轴   
% --- Executes on button press in load_pic.
function load_pic_Callback(hObject, eventdata, handles)
% hObject    handle to load_pic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile({'*.jpg';'*.bmp'},'选择图片');
if FilterIndex
    str = [PathName FileName];
    c = imread(str);
    image(c,'Parent',handles.axes1);
    axis off;
end


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

% Hint: place code in OpeningFcn to populate axes1


  • 26
    点赞
  • 132
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值