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)
上一篇 2022年4月27日 下午12:20
下一篇 2022年4月27日 下午12:20


相关推荐

  • 字符串的匹配算法_多字符串匹配

    字符串的匹配算法_多字符串匹配文章目录BF算法RK算法编辑器中的全局替换方法:BM算法坏字符好后缀规则代码实现KMP算法一说到字符串匹配算法,不知道会有多少小伙伴不由自主的想起那个kmp算法呢?想到是很正常的,谁让它那么优秀呢。BF算法不要被事物的表面现象所迷惑,这个算法全称:BruteForce,有个拉风的中文名:暴力匹配算法。能想明白了吧。如果模式串长度为m,主串长度为n,那在主串中,就会有n-m+1个长度为m的子串,我们只需要暴力地对比这n-m+1个子串与模式串,就可以找出主串与模式串匹配的子串。.

    2022年8月21日
    9
  • 阿里吴建军:千问AI眼镜月底将上线“AI办事”等更多功能

    阿里吴建军:千问AI眼镜月底将上线“AI办事”等更多功能

    2026年3月15日
    3
  • ARMv6 ARMv7 架构 整体介绍

    ARMv6 ARMv7 架构 整体介绍文章目录市场应用领域市占率常见产品架构编程模型运行状态指令集寄存器非特权级别 usr 模式 下不建议访问的指令与寄存器 ABI 异常模型特权级异常种类内存模型 VMSA amp MMU 内存顺序模型 cache 默认内存一致性模型 debug 模型 ExternalDebu hostedDebugT 模型虚拟化模型安全模型开发工具 gcctoolchain amp gdbqemu 常见开发环境 除 qemu 外 软件开发裸机 baremetal RTOSLinuxboo 架构相关代码通用知识 如何新增

    2026年3月20日
    2
  • Windows server 80端口被占用怎么办_sqlserver基本介绍

    Windows server 80端口被占用怎么办_sqlserver基本介绍安装sqlserver导致80端口被占用解决方法系统占用的端口一般都是微软官方的产品占用的。所以这个时候主要考虑到几个服务:SQLServer导致。其中很有可能是SQLServerReportingServices(MSSQLSERVER),它是SQLServer的日志系统。IIS服务。如果你电脑安装了这个,很有可能它在运行着,那么它就占用着80端口当然如果都不…

    2025年7月1日
    7
  • 🔥讯飞星火网页版怎么用?小白必看,手把手教你玩转神器!

    🔥讯飞星火网页版怎么用?小白必看,手把手教你玩转神器!

    2026年3月14日
    2
  • CAS单点登录(三)–服务端改造(登录页及登录方式的自定义)

    CAS单点登录(三)–服务端改造(登录页及登录方式的自定义)上一篇文章(http://blog.csdn.net/u012116457/article/details/52161201)提到,为了更好的满足我们的要求,还需要对服务端进行改造。最近发现了一个巨牛的人工智能教程,不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!所以分享给大家,感兴趣的童鞋可以看看。点这里可以跳转到教程。1.新建cas_server为了方便,首先我们现在…

    2022年6月5日
    98

发表回复

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

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