MATLAB guide例子

1、guide排列後

在這裏插入圖片描述
在這裏插入圖片描述
String-文本框顯示
Style-類型
Tag-保存顯示名字
TooltipString-鼠標移動至此顯示提示

2、保存後

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

% Last Modified by GUIDE v2.5 18-Mar-2020 15:59:18

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

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes during object creation, after setting all properties.
function edit_add1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_add1 (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 edit_add2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_add2 (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 edit_add2 as text
%        str2double(get(hObject,'String')) returns contents of edit_add2 as a double


% --- Executes during object creation, after setting all properties.
function edit_add2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_add2 (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 caculate.
function caculate_Callback(hObject, eventdata, handles)
% hObject    handle to caculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% hObject-可計算的柄
% eventdata -保留-將在未來版本的MATLAB中定義
% handles-具有句柄和用戶數據的結構

3、在回調函數% — Executes on button press in caculate.下操作

num1=str2double(get((handles.edit_add1),'String'));
num2=str2double(get((handles.edit_add2),'String'));
r=num1+num2;
r=num2str(r);
set(handles.result,'String',r);
guidata(hObject, handles);

1、str2double-將字符串轉換爲雙精度值
2、get((handles.edit_add2),‘String’)得到edit_add2裏面的String
3、num2str -將數字轉換爲字符表示
4、guidata(hObject, handles); 更新句柄結構

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章