MATLAB入门之窗口程序-简易计算器

  • 新建一个窗口应用程序,选择GUIDE

  • 编辑界面

  • 把按键原先的callback函数全部删除,然后重新编辑tag生成新的callback函数 

 

在数值键0~9中加入代码,以下为按键1的callback函数中添加的代码

    temp = get(handles.textResult,'String');
    temp = strcat(temp,'1');
    set(handles.textResult,'String',temp);

加号callback函数中加入代码

    temp = get(handles.textResult,'String');
    temp = strcat(temp,'+');
    set(handles.textResult,'String',temp);

减号callback函数中加入代码 

    temp = get(handles.textResult,'String');
    temp = strcat(temp,'-');
    set(handles.textResult,'String',temp);

乘号callback函数中加入代码 

    temp = get(handles.textResult,'String');
    temp = strcat(temp,'*');
    set(handles.textResult,'String',temp);

除号callback函数中加入代码 

    temp = get(handles.textResult,'String');
    temp = strcat(temp,'/');
    set(handles.textResult,'String',temp);

清除键callback函数中加入代码 

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

等于号callback函数中加入代码 

    temp = get(handles.textResult,'String');
    result = eval(temp);
    set(handles.textResult,'String',result);

完整代码如下 

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

% Last Modified by GUIDE v2.5 16-Sep-2019 07:48:28

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 btn_1.
function btn_1_Callback(hObject, eventdata, handles)
% hObject    handle to btn_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'1');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_2.
function btn_2_Callback(hObject, eventdata, handles)
% hObject    handle to btn_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'2');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_3.
function btn_3_Callback(hObject, eventdata, handles)
% hObject    handle to btn_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'3');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_add.
function btn_add_Callback(hObject, eventdata, handles)
% hObject    handle to btn_add (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'+');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_4.
function btn_4_Callback(hObject, eventdata, handles)
% hObject    handle to btn_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'4');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_5.
function btn_5_Callback(hObject, eventdata, handles)
% hObject    handle to btn_5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'5');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_6.
function btn_6_Callback(hObject, eventdata, handles)
% hObject    handle to btn_6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'6');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_minus.
function btn_minus_Callback(hObject, eventdata, handles)
% hObject    handle to btn_minus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'-');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_7.
function btn_7_Callback(hObject, eventdata, handles)
% hObject    handle to btn_7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'7');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_8.
function btn_8_Callback(hObject, eventdata, handles)
% hObject    handle to btn_8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'8');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_9.
function btn_9_Callback(hObject, eventdata, handles)
% hObject    handle to btn_9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'9');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_mul.
function btn_mul_Callback(hObject, eventdata, handles)
% hObject    handle to btn_mul (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'*');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_clear.
function btn_clear_Callback(hObject, eventdata, handles)
% hObject    handle to btn_clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    set(handles.textResult,'String','0');

% --- Executes on button press in btn_0.
function btn_0_Callback(hObject, eventdata, handles)
% hObject    handle to btn_0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'0');
    set(handles.textResult,'String',temp);

% --- Executes on button press in btn_equal.
function btn_equal_Callback(hObject, eventdata, handles)
% hObject    handle to btn_equal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    result = eval(temp);
    set(handles.textResult,'String',result);
% --- Executes on button press in btn_div.
function btn_div_Callback(hObject, eventdata, handles)
% hObject    handle to btn_div (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    temp = get(handles.textResult,'String');
    temp = strcat(temp,'/');
    set(handles.textResult,'String',temp);

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值