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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 未来之路作为创业者_如何看待读图时代

    未来之路作为创业者_如何看待读图时代距离4月11日-14日百度联盟峰会已经过去一个多月了,这一段与许多站长谈论最多的是百度创始人李彦宏在峰会上的演讲,其中创业者三大机会尤最。演讲更多的是从战略角度的高度概括,因此笔者主要想在大家的帮助下再深入分析一下读图时代的创业机会,主要是交流,通过交流更具体一些。在分析、交流之前,还是先引用一些媒体报道,以免失之毫厘,谬以千里。    4月12日上午消息,百度公司…

    2025年10月18日
    3
  • jvm的垃圾回收机制是什么_垃圾回收过程图片

    jvm的垃圾回收机制是什么_垃圾回收过程图片如果大家对java架构相关感兴趣,可以关注下面公众号,会持续更新java基础面试题,netty,springboot,springcloud等系列文章,一系列干货随时送达,超神之路从此展开,BTAJ不再是梦想!垃圾回收的过程分为两步:1.判断对象是否死亡(1)引用计数器法:①每当有一个对象引用是,计数器加一,当计数器为0是对象死亡②缺点:无法解决循环引用的问题,假设A引用B,B引用A,那么这两个对象将不会被回收,造成内存泄漏(2)可达性算法分析①通过一系列可作为GCRoot

    2025年9月5日
    4
  • CSS3 opacity 属性

    CSS3 opacity 属性设置div元素的不透明级别1、属性opacity属性指定了一个元素的透明度。换言之,opacity属性指定了一个元素后面的背景的被覆盖程度。当opacity属性的值应用于某个元素上时,是把这个元素(包括它的内容)当成一个整体看待,即使这个值没有被子元素继承。因此,一个元素和它包含的子元素都会具有和元素背景相同的透明度,哪怕这个元素和它的子元素有不同的opacity属性值。2、语法op…

    2022年5月9日
    46
  • win10 命令行进入指定目录方法[通俗易懂]

    win10 命令行进入指定目录方法[通俗易懂]原文地址:https://blog.csdn.net/weixin_29207913/article/details/106616959方法一在资源管理器”地址“前面输入cmd。如下图:确认后cmd就会进入相关目录,进行其他cmd操作。如下图:方法二在资源管理器中,按住shift,同时右键空白地方,将会出现分别点击红框中选项,能分别使用PowerShell和WSL进入对应目录…

    2022年10月16日
    3
  • io电平转换芯片_一般plc均配置io电平转换

    io电平转换芯片_一般plc均配置io电平转换我们在使用ic2总线时,常常因为3.3v单片机与5v外围器件之间电压不匹配而需要进行电平转换,特将次转换电路记录一下,防止以后寻找时麻烦,同时此电路可以应用于大多数电平转换电路,如3.3V转12V也同样可以使用电路原理如下原理分析3.3V控制5V5V控制3.3V也可以用在3.3v单片机输出5v的TTL电平,时进行应用。可以使用BSS123代替2N7002特此记录,anlog…

    2022年8月30日
    3
  • 创建文件命令

    创建文件命令创建文件命令:// 创建文本文件的命令有很多,大部分是利用”重定向”的功能来实现的。这里分两种类型来说:1、创建非空文本文件:非空的文本文件很好创建,只要用有屏幕输出的命令就可以了

    2022年8月4日
    8

发表回复

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

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