Matlab写的第一个GUI程序(程序设计,界面设计)

写在前面:博主用的Matlab版本是2016b,属于旧版本,大概在2019版本开始属于新版本叭,新版本写程序在APP菜单有设计APP这个选项,旧版本的则是在主页的新建选项下有GUI选项

目录

1.新建项目

1.1找到入口

 1.2项目模板选择

2.界面设计

2.1初识整体

2.2控件拖入

 2.3修改控件属性

3.打开编辑器,生成相应模板代码

4.认识代码模板

5.编译程序(写代码)

5.1给input1Edit写Callback回调的函数内容

5.2给input2Edit写Callback回调的函数内容

5.3answerTxt不需要单独写代码是写在ADD里面的,直接赋值给了它

5.4addPushbutton的Callback回调函数内容

6.运行代码+完整代码展示

6.1运行程序

 6.2完整代码show


1.新建项目

1.1找到入口

新版本界面设计入口,APP->设计APP

在这里插入图片描述

 旧版本界面设计入口,主页->新建->app->GUIDE

 1.2项目模板选择

可以选择如图所示的默认模板,选择新建GUI中的第一个default模板

 另一个选项打开现有GUI的使用情况是:01.不小心把没有编辑完成的界面关闭了,在这里找到之前没完成的界面打开,继续编辑;02.调试程序时发现界面设计被关掉了,也在此处打开,重新编辑;03.打开别人的开源程序

界面的文件后缀是flg,它要搭配源代码.m才是一个有功能性的完整程序,记得把两个文件都保存在同一个全是英文路径的文件夹下哦

将新图形另存为是针对后期稍微复杂一点的有图形输出的程序,把生成的图形保存到一个路径下,我们的第一个代码不涉及图形,纯数字,所以不用勾选此选项

2.界面设计

2.1初识整体

点击确定之后,进入到一个新建好的界面

大致如下(新旧版本之间此界面有细微差异,但不影响)

 左边是控件区域,待会儿我们会通过拖入控件,调整并且赋予相应控件以代码

中间是画布(也可以叫布局区),拖动右下角可以调整显示区域大小(鼠标放到这里会呈现双向箭头)放大画布后如下

 

 上方灰色字部分是菜单栏,可以实现界面文件GUI的.flg文件的保存和另存等

紧挨着菜单栏的下方就是工具栏,待会儿我们需要用到这里面的一个工具实现生成相应程序代码模板

2.2控件拖入

我们需要用到的控件是可编辑文本,静态文本以及按钮(从左到右依次对应名称图标)

写着EDIT,TXT,OK

看下目标文件界面,MY FIRST GUIAPP是静态文本,13是可编辑文本,14是可编辑文本,+号是静态文本,=号是静态文本,27是运算结果由代码赋予结果也是静态文本,下方的Add!是按钮。

 控件拖入完成之后,如下图所示,2个可编辑文本,4个静态文本,1个按钮

 2.3修改控件属性

接下来就开始修改控件属性了,改成我们需要呈现出来的样子,双击控件会在左边弹出一个属性编辑器,比如我们双击一个可编辑文本,就点左上角的那个叭,弹出的界面如下,我们主要用到的是第一个Background(修改控件背景颜色),FontAngle(修改字体大小,此处用的是30磅)

FontWeight(字重,加粗blod)

ForegroundC...(文本颜色,字体颜色,可以任意选择)

HorizontalAli...(文本对齐方式,left居左,center居中,right居右)

Style是控件类型(比如edit是可编辑文本)

String(界面控件上要显示的文字,这个控件是可编辑文本,我们待会儿需要用户输入数字,初始显示给它一个0)

Tag(此时它默认的是edit1,我们可以把它改成input1Edit,寓意为用户输入的第一个数字,且类型是edit可编辑文本,待会儿写相应函数代码也方便找到,待会儿各个控件之间会有互相调用关联,记得改一个自己容易识别的名称)

 你还在一个个地拖入控件一个个地改背景,字号,对齐方式....这些么,其实,可以ctrl+C&Ctrl+V大法,比如我想在界面上显示的两个可编辑文本都是橙色底,15号,加粗,string都是0,那么我复制粘贴一下,还不用担心按钮方框大小不一致,拖动到合适的位置

PS:复制好的控件Tag名称并不会跟被复制的控件一样,而是回到默认(n,第几个edit就是第几个),所以我们需要动动小手指重新修改一下Tag,第二个就改成input2Edit叭

划重点:改完每一个属性都不要敲回车键,而是应该点击空白的地方来确定你的修改,因为敲回车会重新生成一个函数,到时候太多冗余控件模板了会降低代码的美观性(应该叫可读性叭)

我们只改即将会有变化或者要赋予某样功能的控件,此处只有两个可编辑文本input1Edit和input2Edit,以及最后的输出结果=号后面的静态文本Tag:answerTxt(同理意思是结果,类型txt静态文本,至于为什么要用静态文本,是因为防止用户篡改,哦不修改),button按钮的Tag:addPushbutton

出现这种字号太大又显示不完全内容的,选中控件四周有四个黑点,可以任意拉宽拉长

 

改好,排列完成之后大致如下(等于的结果底色之所以用灰色,是因为防止与可编辑文本的两个inputEdit混淆)

 到此为止,控件编辑的工作就完成啦

3.打开编辑器,生成相应模板代码

在界面工具栏的右数第四个

 单击,就会让我们选择一个路径保存并且命名(建议全英文,路径,文件名)我的桌面是desktop不是中文路径哦

 

 会自动生成一个flg文件和一个.m文件

4.认识代码模板

 

% --- Executes just before myGUIAPP is made visible.
function myGUIAPP_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 myGUIAPP (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

function myGUIAPP_OpeningFcn(hObject, eventdata, handles, varargin)

这一整块是我们以后深入开发学习会用到的,相当于读取文件

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

function varargout = myGUIAPP_OutputFcn(hObject, eventdata, handles) 

这一整块儿是整体输出,也是我们以后会用到的,现在这里用不到,因为只有像读写文件夹,输出执行结果数据等等才会用到OpeningFcn和OutputFcn

之后的就是我们添加的各种控件的相应模板了,比如

function input1Edit_Callback(hObject, eventdata, handles)%我们的第一个可编辑文本,用户需要输入的第一个文字
% hObject    handle to input1Edit (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 input1Edit as text
%        str2double(get(hObject,'String')) returns contents of input1Edit as a double
%从这里开始写我们的代码


% --- Executes during object creation, after setting all properties.
function input1Edit_CreateFcn(hObject, eventdata, handles)%这个是自动生成的,不用管
% hObject    handle to input1Edit (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

 这个就是我们在Tag里面命名的input1Edit而它的事件是Callback回调

CreateFcn事件目前用不着

5.编译程序(写代码)

5.1给input1Edit写Callback回调的函数内容

function input1Edit_Callback(hObject, eventdata, handles)%我们的第一个可编辑文本,用户需要输入的第一个文字
% hObject    handle to input1Edit (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 input1Edit as text
%        str2double(get(hObject,'String')) returns contents of input1Edit as a double
%从这里开始写我们的代码

input = str2num(get(hObject,'String'));%把用户输入的字符串转换为可以进行运算的数字
if(isempty(input))%如果用户没有输入,那么就是0
    set(hObject,'String','0')%把控件的string属性设为0
end%If...end

%这个控件的代码只用一个执行目的,就是管用户的输入

 写好之后是完整的这样的,后面还包含了一个自动生成的CreateFcn事件,暂且不用我们打理这部分,

function input1Edit_Callback(hObject, eventdata, handles)%我们的第一个可编辑文本,用户需要输入的第一个文字
% hObject    handle to input1Edit (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 input1Edit as text
%        str2double(get(hObject,'String')) returns contents of input1Edit as a double
%从这里开始写我们的代码

input = str2num(get(hObject,'String'));
if(isempty(input))
    set(hObject,'String','0')
end

% --- Executes during object creation, after setting all properties.
function input1Edit_CreateFcn(hObject, eventdata, handles)%这个是自动生成的,不用管
% hObject    handle to input1Edit (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

 

如果是复制这里的代码的话,set左边会有红色虚线,只需要往往左边退格到上一行,再直接回车就行了 

5.2给input2Edit写Callback回调的函数内容

 其实跟input1Edit是一样的,具体原理可以看5.1的代码注释

function input2Edit_Callback(hObject, eventdata, handles)%我们的第二个可编辑文本
% hObject    handle to input2Edit (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 input2Edit as text
%        str2double(get(hObject,'String')) returns contents of input2Edit as a double

input = str2num(get(hObject,'String'));
if(isempty(input))
    set(hObject,'String','0')
end

% --- Executes during object creation, after setting all properties.
function input2Edit_CreateFcn(hObject, eventdata, handles)%这个也是自动生成的
% hObject    handle to input2Edit (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

5.3answerTxt不需要单独写代码是写在ADD里面的,直接赋值给了它

5.4addPushbutton的Callback回调函数内容

直接在相应的块儿后边输入会自动被编译器识别他们是一起的,不用花括号等...

 注释

a = get(handles.input1Edit,'String');%获取input1Edit的文本,并且赋值给变量a
b = get(handles.input2Edit,'String');%获取input2Edit的文本,并且赋值给变量b
total = str2num(a)+str2num(b);%和total为a+b的结果终值,用matlab自带的str2num字符串转数字函数转成数字进行加法运算
c = num2str(total);%用MATLAB自带的函数num2str把计算结果数字转换成string字符串,便于后续对string属性值的更改
set(handles.answerTxt,'String',c);%把转换成功的字符串,handles.answerTxt,调用我们的answerTxt控件的'string'属性,把c的结果填入

 至此,所有代码都写完了

6.运行代码+完整代码展示

6.1运行程序

我个人更偏向于使用界面上的那个绿色播放器运行,工具栏右起第一个

 弹出如下弹窗的话,添加到路径就好了

运行结果,可以修改数字(PS发现了一个问题,=号的string+没改过来)

如果不能add执行加法运算的话,那肯定就是等号后面的Tag没改过来,answerTxt,如果是报错一大长串红字的话,那就是代码敲错了,或者直接复制的代码,没有按照我的控件名字来,把有控件名字的地方改成你自己命名的Tag就好了

 

 6.2完整代码show

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

% Last Modified by GUIDE v2.5 01-Nov-2022 14:42:21

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

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

% Update handles structure
guidata(hObject, handles);

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


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



function input1Edit_Callback(hObject, eventdata, handles)%我们的第一个可编辑文本,用户需要输入的第一个文字
% hObject    handle to input1Edit (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 input1Edit as text
%        str2double(get(hObject,'String')) returns contents of input1Edit as a double
%从这里开始写我们的代码
input = str2num(get(hObject,'String'));%把用户输入的字符串转换为可以进行运算的数字,matlab自带的函数str2num
if(isempty(input))%如果用户没有输入,那么就是0
    set(hObject,'String','0');%把控件的string属性设为0
end%If...end%这个控件的代码只用一个执行目的,就是管用户的输入


% --- Executes during object creation, after setting all properties.
function input1Edit_CreateFcn(hObject, eventdata, handles)%这个是自动生成的,不用管
% hObject    handle to input1Edit (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 button press in addPushbutton.
function addPushbutton_Callback(hObject, eventdata, handles)%执行加法的按钮
% hObject    handle to addPushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a = get(handles.input1Edit,'String');%获取input1Edit的文本,并且赋值给变量a
b = get(handles.input2Edit,'String');%获取input2Edit的文本,并且赋值给变量b
total = str2num(a)+str2num(b);%和total为a+b的结果终值,用matlab自带的str2num字符串转数字函数转成数字进行加法运算
c = num2str(total);%用MATLAB自带的函数num2str把计算结果数字转换成string字符串,便于后续对string属性值的更改
set(handles.answerTxt,'String',c);%把转换成功的字符串,handles.answerTxt,调用我们的answerTxt控件的'string'属性,把c的结果填入


function input2Edit_Callback(hObject, eventdata, handles)%我们的第二个可编辑文本
% hObject    handle to input2Edit (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 input2Edit as text
%        str2double(get(hObject,'String')) returns contents of input2Edit as a double
input = str2num(get(hObject,'String'));
if(isempty(input))
    set(hObject,'String','0')
end

% --- Executes during object creation, after setting all properties.
function input2Edit_CreateFcn(hObject, eventdata, handles)%这个也是自动生成的
% hObject    handle to input2Edit (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

看在博主肝得那么详细的份上就点个赞再走呗~

  • 53
    点赞
  • 202
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Windows Usb热插拔是指在Windows操作系统中,当USB设备插入或拔出时,系统能够自动检测并做出相应的响应。在程序中实现USB热插拔的功能,可以通过注册USB设备信息和监测USB设备插拔事件来实现。 在程序初始化时,可以通过注册USB设备信息来响应USB热插拔事件。这可以通过调用RegisterDeviceNotification函数来实现。在注册USB设备信息时,需要指定设备类型为DBT_DEVTYP_DEVICEINTERFACE,并传入相应的设备接口信息。具体的注册过程可以参考引用\[1\]中的代码示例。 另外,还可以使用第三方库来实现USB转串口设备的热插拔检测。例如,可以使用CH343PT库中的接口CH343PT_SetDevNotify来实现USB转串口设备的热插拔检测。具体的使用方法可以参考引用\[3\]中的介绍。 总结起来,Windows Usb热插拔可以通过注册USB设备信息和监测USB设备插拔事件来实现。具体的实现方法可以根据需求选择使用系统提供的函数或第三方库来完成。 #### 引用[.reference_title] - *1* [windows下USB检测插拔状态](https://blog.csdn.net/qq_22642239/article/details/110451792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [CH343PT库使用一>USB转串口设备的热插拔检测](https://blog.csdn.net/WCH_TechGroup/article/details/127514913)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值