看B站up主freexyn的freexyn编程实例视频教程系列36Matlab GUI的学习笔记
文章目录
- 初步认识+普通按钮
- 静态文本和可编辑文本
- 设计一个简易计算机
初步认识+普通按钮
任务要求:点击一次“100”按钮,按钮上的文字值就递增1;点击“close”按钮,关闭所有窗口
-
命令行打开GUI:命令行guide,或者appdesigner
-
如果左边方块没有文字说明,则点击文件-预设-在组件选项板中显示名称-确定
-
调整设计界面大小,点击普通按钮,拉到设计界面,调整普通按钮大小
-
双击这个普通按钮,或者右键点击属性,修改String、FontSize等内容。修改完毕后点击×号退出
-
重复以上操作,得到以下
-
点击保存,命名为test1.fig,同时自动生成test1.m文件
-
对于test1.m文件:
**function varargout = test1(varargin)** % TEST1 MATLAB code for test1.fig % TEST1, by itself, creates a new TEST1 or raises the existing % singleton*. % % H = TEST1 returns the handle to a new TEST1 or the handle to % the existing singleton*. % % TEST1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in TEST1.M with the given input arguments. % % TEST1('Property','Value',...) creates a new TEST1 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before test1_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to test1_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 test1% Last Modified by GUIDE v2.5 18-Feb-2024 15:21:53% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @test1_OpeningFcn, ...'gui_OutputFcn', @test1_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []); if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1}); endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT% --- Executes just before test1 is made visible. function test1_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 test1 (see VARARGIN)% Choose default command line output for test1 handles.output = hObject;% Update handles structure guidata(hObject, handles);% UIWAIT makes test1 wait for user response (see UIRESUME) % uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = test1_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)% --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
-
function varargout = test1(varargin):函数名称即文件名称,包含了方才设计的图形文件的一些初始化信息,不要修改
-
function test1_OpeningFcn(hObject, eventdata, handles, varargin):回调函数,当打开一个图形文件的时候,所有的控件数据都设置好了之后,把图形界面呈现在电脑屏幕之前,该函数运行,对界面进行一些初始化处理
-
function varargout = test1_OutputFcn(hObject, eventdata, handles) :输出函数,有输出参数,在打开函数OpeningFcn()返回控制之后,把控制权返回给命令行窗口之前,执行该函数,执行结果会输出到命令行窗口中
-
pushbutton1_Callback、pushbutton2_Callback:两个组件的回调函数,在图形界面点击按钮会执行对应回调函数,其中的pushbutton1、pushbutton2对应组件的Tag
function pushbutton1_Callback(hObject, eventdata, handles) %hObject是点击对象的句柄,即点击“100”按钮时,会返回这个按钮的句柄, %以此来区分点击对象是“100”,而不是“close”%eventdata是一个保留字段,在不同的控件中有不同用法,在按钮中其实是没什么意义的%handles是结构数组,包含整个界面控件信息以及数据信息
-
-
现在回到设计界面
-
给close按钮添加回调:对“close”按钮点击右键,查看回调,单击callback,会定位到.m文件中的对应位置,进行代码编辑,保存
% --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clear all %清除所有变量 close all %关闭所有窗口
点击.fig页面中的绿色三角形,运行,点击运行界面的“close”,图形窗口就关闭了
-
给100按钮添加回调:要求实现点击一次“100”按钮,按钮上的文字值就递增1。步骤同上
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) %hObject是点击对象的句柄,即点击“100”按钮时,会返回这个按钮的句柄, %以此来区分点击对象是“100”,而不是“close” %eventdata是一个保留字段,在不同的控件中有不同用法,在按钮中其实是没什么意义的 %handles是结构数组,包含整个界面控件信息以及数据信息%get(h,属性)获得图形对象h上的属性值,是字符串格式 s=get(hObject,'string');%set(h,Name,Value) 使用一个或多个名称-值参数设置指定图形对象h的属性 set(hObject,'string',num2str(str2num(s)+1));
静态文本和可编辑文本
任务要求实现:在可编辑文本中输入文字,回车,立即显示在静态文本框中
-
可编辑文本:可以输入内容
-
静态文本:仅用于显示字符
-
界面设计如下:属性不做修改,保存为test2.fig
-
双击.fig页面中的可编辑文本,点击查看回调,点击callback
-
自动定位到.m文件中的edit1_Callback,根据任务要求进行代码编辑
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 doubles=get(hObject,'string'); %handles包含整个界面控件信息以及数据信息, %**handles.text2即引用了另一个控件的数据,text2是静态文本框的tag值** set(handles.text2,'string',s);
handles可帮忙引用其他控件的数据
-
保存.m,在.fig界面运行即可
设计一个简易计算机
普通按钮、静态文本和可编辑文本的综合运用
-
先用普通按钮设计,属性只修改string
-
增加静态文本:属性修改backgroungcolor,防止和页面背景融为一体,同时把string删除。该框用于显示计算过程和结果
-
编写各个普通按钮的回调函数:任务要求把计算过程和结果在静态文本框中显示出来
-
0~9和+、-、/的按钮
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % 如果只是 % s=get(hObject,'string'); % set(handles.text2,'string',s); % 则会导致无法输入11,只能输入1 s0=get(handles.text2,'string'); s=get(hObject,'string'); set(handles.text2,'string',[s0,s]);
-
×的按钮:×不能运算,*才可以运算
% --- Executes on button press in pushbutton16. function pushbutton16_Callback(hObject, eventdata, handles) % hObject handle to pushbutton16 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) s0=get(handles.text2,'string'); s=get(hObject,'string'); set(handles.text2,'string',[s0,'*']);
-
=和C的按钮
% --- Executes on button press in pushbutton18. function pushbutton18_Callback(hObject, eventdata, handles) % hObject handle to pushbutton18 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) s0=get(handles.text2,'string'); %eval(expression)返回 expression 的输出,数值型 set(handles.text2,'string',num2str(eval(s0)));% --- Executes on button press in pushbutton19. function pushbutton19_Callback(hObject, eventdata, handles) % hObject handle to pushbutton19 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text2,'string',[]);
-