MATLAB GUI实现计算器(设计)「建议收藏」

MATLAB GUI实现计算器(设计)「建议收藏」1.先打开matlab新建GUI文件2.选择路径(左边是默认的不用改)然后点击ok3.此时界面会弹出一个小框4.建立计算器界面(贴上我设计的界面,不许嘲笑我的设计)5.细致讲解一下,这里的按键和显示框的是怎么实现的A.显示框:选择edittext在右边屏幕拉取即可如图所示,新建两个即可,左边作为输入屏,右边作为输入结果的显示屏双击该框,…

大家好,又见面了,我是你们的朋友全栈君。

1.先打开matlab新建GUI文件

新建GUI文件

2.选择路径(左边是默认的不用改)

选择路径

然后点击ok

3.此时界面会弹出一个小框

弹出框

4.建立计算器界面(贴上我设计的界面,不许嘲笑我的设计)

界面设计

5.细致讲解一下,这里的按键和显示框的是怎么实现的

A.显示框: 选择edit text
显示框
在右边屏幕拉取即可
显示框
如图所示,新建两个即可,左边作为输入屏,右边作为输入结果的显示屏
双击该框,弹出一个窗口,窗口下拉,里面有该显示框的属性
显示框的属性
string值为显示框显示的值
Tag为显示框的名字
(现在这个计算器需要显示框和输出框,如果没有新建其他的edit框,默认新建的第一个edit框的名字为edit1,第二个为edit2)

B.数字按钮和字符按钮
点击push button 新建按钮,需要几个建几个
按钮
双击之后在string那一栏改成你所需要的按钮
按钮值
回车之后,如图所示
改好的按钮值

6.把界面雏形做出之后,我们需要去实现计算器的操作

A.首先改0~9的数字键(需要点击后在edit1上显示该数字)
右键点击你需要改的button之后会出现一个窗口选择view callbacks->callback,然后会打开一个函数,蓝色字体所在的位置是该button所在的位置的代码
代码所在
按照这个方式将自己的代码改成自己所需要的数字
handles.后面加的是自己所需要将显示的文本框的名字

textString = get(handles.edit1,‘String’);
textString = strcat(textString,‘6’);
set(handles.edit1,‘String’,textString);
guidata(hObject, handles);

B.对于计算符号
计算符号

textString = get(handles.edit1,‘String’);
textString = strcat(textString,’+’);
set(handles.edit1,‘String’,textString);
guidata(hObject, handles);

加减乘除只需用换一下就好
C.对于AE操作
AE

textString = get(handles.edit1,‘String’);
textString = get(handles.edit2,‘String’);
set(handles.edit1,‘String’,’’);
set(handles.edit2,‘String’,’’);
guidata(hObject, handles);

D.对于back操作
back

textString=get(handles.edit1,‘String’);
as=char(textString);
n=length(textString);
textString=as(1:n-1);
set(handles.edit1,‘String’,textString)
guidata(hObject, handles);

E.退出操作
退出

close(gcf);

7.初始化操作

A.需要对edit1和edit2进行初始化,使代码运行时,文本框里为空白(需要在opening里改函数)
初始化

set(handles.edit1,‘string’,’’);
set(handles.edit2,‘string’,’’);

B.需要将edit1的结果在edit2中显示出来(需要改计算那个button的函数值)
计算

textString = get(handles.edit1,‘String’);
aa = eval(textString);
set(handles.edit2,‘String’,aa);
guidata(hObject, handles);

使其显示在edit2上
点击运行即可实现计算器。
计算器

function varargout = text2(varargin)
% TEXT2 MATLAB code for text2.fig
%      TEXT2, by itself, creates a new TEXT2 or raises the existing
%      singleton*.
%
%      H = TEXT2 returns the handle to a new TEXT2 or the handle to
%      the existing singleton*.
%
%      TEXT2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEXT2.M with the given input arguments.
%
%      TEXT2('Property','Value',...) creates a new TEXT2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before text2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to text2_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 text2
% Last Modified by GUIDE v2.5 28-May-2018 19:45:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @text2_OpeningFcn, ...
'gui_OutputFcn',  @text2_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 text2 is made visible.
function text2_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 text2 (see VARARGIN)
% Choose default command line output for text2
set(handles.edit1,'string','');
set(handles.edit2,'string','');
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes text2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = text2_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 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
% --- 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)
textString = get(handles.edit1,'String');
aa = eval(textString);
set(handles.edit2,'String',aa);
guidata(hObject, handles);
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
% --- 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)
textString = get(handles.edit1,'String');
textString = strcat(textString,'7');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- 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)
textString = get(handles.edit1,'String');
textString = strcat(textString,'8');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- 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)
textString = get(handles.edit1,'String');
textString = strcat(textString,'9');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'4');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'5');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'6');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'1');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'2');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'3');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'0');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'+');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = get(handles.edit2,'String');
set(handles.edit1,'String','');
set(handles.edit2,'String','');
guidata(hObject, handles);
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'-');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'*');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- 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)
textString = get(handles.edit1,'String');
textString = strcat(textString,'/');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton17 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'4');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- 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)
textString = get(handles.edit1,'String');
textString = strcat(textString,'7');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
% --- 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)
close(gcf);
% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton20 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString=get(handles.edit1,'String');
as=char(textString);
n=length(textString);
textString=as(1:n-1);
set(handles.edit1,'String',textString)
guidata(hObject, handles);
% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
textString = get(handles.edit1,'String');
textString = strcat(textString,'.');
set(handles.edit1,'String',textString);
guidata(hObject, handles);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134529.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号