Matlab GUI界面设计

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

大家好,又见面了,我是你们的朋友全栈君。

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

1.启动GUI设计

命令行输入guide,回车。

Matlab GUI界面设计

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

Matlab GUI界面设计

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

Matlab GUI界面设计Matlab GUI界面设计

2..fig文件控件布局

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

Matlab GUI界面设计

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

Matlab GUI界面设计

3.GUI初始程序编写

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

Matlab GUI界面设计

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

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.弹出式菜单编写

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

Matlab GUI界面设计             Matlab GUI界面设计

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

该部分函数编写,右击弹出式菜单->查看回调->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

 

欢迎交流指正。。。
 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130031.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 消息队列

    消息队列

    2021年3月12日
    158
  • setCompoundDrawables与setCompoundDrawablesWithIntrinsicBounds的区别

    setCompoundDrawables与setCompoundDrawablesWithIntrinsicBounds的区别

    2021年9月3日
    77
  • 配置Log4j(非常具体)

    配置Log4j(非常具体)

    2021年12月7日
    36
  • vim编辑器,可以实现保存退出()_vim进入编辑模式如何保存并退出

    vim编辑器,可以实现保存退出()_vim进入编辑模式如何保存并退出目录1.Vim模式2.在Vim/Vi中打开文件3.在Vim/Vi中保存文件4.保存文件并退出Vim/Vi5.退出Vim/Vi而不保存文件1.Vim模式启动Vim编辑器时,您处于正常模式。在这种模式下,您可以使用vim命令并浏览文件。要输入文字,您需要按i键进入插入模式。使用此模式,您可以像在常规文本编辑器中一样插入和删除字符。要从其他任何模式返回正常模式,只需按Esc键。2.在Vim/Vi中打开文件要使用Vim打开文件,请键入vim,然后输入要编辑或创建的文件的.

    2022年8月24日
    3
  • ArrayList扩容机制JDK1.8

    ArrayList扩容机制JDK1.8本题的所有的讲解都是基于JDK8这道题考察了ArrayList的构造器和对扩容机制的了解,本篇博客基于此出发讲解ArrayList的扩容机制想要做出这道题必须了解ArrayList的构造函数,ArrayList的构造函数总共有三个:ArrayList()构造一个空的数组。JDK7中构造一个初始容量为10的空列表但是JDK8中只是构造一个空的数组ArrayList(Collection<?extendsE>c)构造一个包含指定collection的元素的数组,这些元素是按.

    2022年6月9日
    25
  • fisher最优分割法_a0裁切三次

    fisher最优分割法_a0裁切三次给定一个无向图 G=(V,E),每个顶点都有一个标号,它是一个 [0,231−1] 内的整数。不同的顶点可能会有相同的标号。对每条边 (u,v),我们定义其费用 cost(u,v) 为 u 的标号与 v 的标号的异或值。现在我们知道一些顶点的标号。你需要确定余下顶点的标号使得所有边的费用和尽可能小。输入格式第一行有两个整数 N,M,N 是图的点数,M 是图的边数。接下来有 M 行,每行有两个整数 u,v,代表一条连接 u,v 的边。接下来有一个整数 K,代表已知标号的顶点个数。接下来的 K

    2022年8月11日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号