首页 > 编程语言 >Matlab GUI_guide模式编程快速入门教程

Matlab GUI_guide模式编程快速入门教程

时间:2022-09-05 19:46:40浏览次数:88  
标签:String see get GUI 入门教程 hObject handles Matlab eventdata

摘要:GUI设计是交互设计,关联界面和软件本体之间的联系,然后一般设计包括

实现计算和绘图等等,在软件著作中需要要求是计算严谨,绘图吸引,功能丰富以及具体的实际用途


目录

1.界面介绍

2.标签修改

3.相关命令介绍

4.合并.fig和.m使得其在任意平台运行

5.测试

6.附录源码

 


1.界面介绍

     (1) 在matlab命令窗口输入guide 回车

         

     (2)工具栏介绍

                       

2.标签修改

      如下图所示,编号很重要,对于之后编程很重要

               

3.相关命令介绍

%%

1).把文本框中的值输入到程序中
a11 = str2double(get(handles.edit1,'String'));  

%%

2).将程序中的数据转移到静态文本框中

set(handles.text20,'String',x1(1));  

3).按钮回调函数

               

根据按钮序列编写相应功能的代码

               

(4)清零函数

                 set(handles.edit1,'String',0);

(5)退出函数

                   set(gcf,'visible','off');

4.合并.fig和.m使得其在任意平台运行

    在命令窗口输入deploytool,回车

      电机Matlab compiler

      出现如下界面

     

     

     

5.测试

        ===。启动软件

     

     ===。启动等待界面

   

===。主界面

===该程序主要实现功能,实现线性方程组的求解(解析解和数值解),并绘图。

6.附录源码

 

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

% Last Modified by GUIDE v2.5 05-Sep-2022 19:09:33

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = chenxicalculation_gui_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 edit100_Callback(hObject, eventdata, handles)
% hObject    handle to edit100 (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 edit100 as text
%        str2double(get(hObject,'String')) returns contents of edit100 as a double


% --- Executes during object creation, after setting all properties.
function edit100_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit100 (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



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 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



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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



function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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



function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (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 edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (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



function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (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 edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (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



function edit11_Callback(hObject, eventdata, handles)
% hObject    handle to edit11 (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 edit11 as text
%        str2double(get(hObject,'String')) returns contents of edit11 as a double


% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (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



function edit12_Callback(hObject, eventdata, handles)
% hObject    handle to edit12 (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 edit12 as text
%        str2double(get(hObject,'String')) returns contents of edit12 as a double


% --- Executes during object creation, after setting all properties.
function edit12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit12 (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 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)

%%1.把文本框中的值输入到程序中
a11 = str2double(get(handles.edit1,'String'));
a12 = str2double(get(handles.edit2,'String'));
a13 = str2double(get(handles.edit3,'String'));
a21 = str2double(get(handles.edit4,'String'));
a22 = str2double(get(handles.edit5,'String'));
a23 = str2double(get(handles.edit6,'String'));
a31 = str2double(get(handles.edit7,'String'));
a32 = str2double(get(handles.edit8,'String'));
a33 = str2double(get(handles.edit9,'String'));
b1 = str2double(get(handles.edit10,'String'));
b2 = str2double(get(handles.edit11,'String'));
b3 = str2double(get(handles.edit12,'String'));
%%2.计算解向量
%%
A=[a11 a12 a13;a21 a22 a23;a31 a32 a33];
b=[b1 b2 b3];
x1=[0 0 0];
n=numel(b); 
 
e_max=1e6;       %%前一次和后一次之差
while e_max>=1e-6 
    e_max=0; 
    for i=1:n 
        s=0;           %%初始化变量
        for j=1:n 
            if j~=i 

                s=s+A(i,j)*x1(j); 
            end 
        end 
        x2(i) = (b(i)-s)/A(i,i); 
        e = abs(x2(i)-x1(i));     
        if e > e_max  
            e_max = e; 
        end 
    end 
    x1=x2      %%不带分号,观察每步迭代结果 
end 
%%3.输出结果
%%
set(handles.text20,'String',x1(1));  %%将程序中的数据转移到静态文本框中
set(handles.text21,'String',x1(2));  %%将程序中的数据转移到静态文本框中
set(handles.text22,'String',x1(3));  %%将程序中的数据转移到静态文本框中

% --- 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)

%%绘图按钮
a11 = str2double(get(handles.edit1,'String'));
a12 = str2double(get(handles.edit2,'String'));
a13 = str2double(get(handles.edit3,'String'));
a21 = str2double(get(handles.edit4,'String'));
a22 = str2double(get(handles.edit5,'String'));
a23 = str2double(get(handles.edit6,'String'));
a31 = str2double(get(handles.edit7,'String'));
a32 = str2double(get(handles.edit8,'String'));
a33 = str2double(get(handles.edit9,'String'));
b1 = str2double(get(handles.edit10,'String'));
b2 = str2double(get(handles.edit11,'String'));
b3 = str2double(get(handles.edit12,'String'));
%%2.计算解向量
%%
A=[a11 a12 a13;a21 a22 a23;a31 a32 a33];
b=[b1 b2 b3];
x1=[0 0 0];
n=numel(b); 
 
e_max=1e6;       %%前一次和后一次之差
while e_max>=1e-6 
    e_max=0; 
    for i=1:n 
        s=0;           %%初始化变量
        for j=1:n 
            if j~=i 

                s=s+A(i,j)*x1(j); 
            end 
        end 
        x2(i) = (b(i)-s)/A(i,i); 
        e = abs(x2(i)-x1(i));     
        if e > e_max  
            e_max = e; 
        end 
    end 
    x1=x2;      %%不带分号,观察每步迭代结果 
    t=0:1:2;
    f1=x1(t+1);
    plot(handles.axes1,t,f1,'gd-');
    title("the first graphic for chenxin");
    xlabel("x-轴");
end 

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (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,'String',0);
set(handles.edit2,'String',0);
set(handles.edit3,'String',0);
set(handles.edit4,'String',0);
set(handles.edit5,'String',0);
set(handles.edit6,'String',0);
set(handles.edit7,'String',0);
set(handles.edit8,'String',0);
set(handles.edit9,'String',0);
set(handles.edit10,'String',0);
set(handles.edit11,'String',0);
set(handles.edit12,'String',0);

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(gcf,'visible','off');

具体原理介绍见:

雅克比迭代法介绍以及matlab代码实现-线性方程组求解

 

 

date: 20220905

 

标签:String,see,get,GUI,入门教程,hObject,handles,Matlab,eventdata
From: https://www.cnblogs.com/sophiaecho/p/16659318.html

相关文章

  • unigui源码路径
    unigui源码路径$(fmsoft)\uniGUI$(fmsoft)\uniGUI\uIndy$(fmsoft)\uniGUI\Source\Core$(fmsoft)\uniGUI\Source\VCL$(fmsoft)\uniGUI\Source\Components$(fmsoft)\u......
  • matlab练习程序(VAR模型)
    VAR模型称为向量自回归模型,可以对多组变量之间的关系进行建模,是AR模型的多维扩展。比如有两个变量X和Y,AR模型建模场景是X只和X过去的状态有关系,VAR模型建模场景则是X同时......
  • matlab数据结构之-table2
    table是一种有行和列类似于表的数据结构,每一个都具有易于记忆的标签。表的创建需要有相同长度,且是列的存储方式。使用table()函数创建,以下假设记录病人的姓名、身高和......
  • matlab中数据结构之-structure
    数据结构是将有逻辑联系的结构中称为域的值组合成一群。结构的优势是域是被命名了的,可以使结构中存储的数据更加清晰。结构变量不是数组,他们没有索引,不能像vector那样......
  • matlab练习程序(ARMA模型)
    ARMA称为自回归移动平均模型(Autoregressive moving averagemodel),由自回归模型(AR)和移动平均模型(MA)混合构成。注意这里的移动平均模型和移动平均值平滑曲线不是一......
  • matlab中数据结构之-cell array
    不同与matlab中的array数据结构中存储的都是一样的数据,cellarray中可以存储不同的数据类型,而且cellarray也可以是向量或矩阵,数组中不同的元素指向不同的数值。原来主......
  • 最大似然估计(MLE)入门教程
     什么是最大似然估计(MLE)最大似然估计(MaximumLikelihoodEstimation)是一种可以生成拟合数据的任何分布的参数的最可能估计的技术。它是一种解决建模和统计中......
  • gui编程
    newframe 第一个界面需要设置显示  Frameframe=newFrame("这是我的第一个窗口");frame.setVisible(true);//设置显示frame.setSize(300,300);//设置大小f......
  • day01-GUI坦克大战01
    JavaGUI-坦克大战1.Java绘图坐标体系坐标体系介绍:下图说明了一个Java坐标体系。坐标原点位于左上角,以像素为单位。在Java坐标体系中,第一个是x坐标,表示当前位置为水平方......
  • MATLAB R2019b安装 及其启动慢的解决方案
       MATLAB是一款商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。可以进行矩阵运算、......