MATLAB的图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
1.系统功能介绍
计算器实现的功能有:数字0~9和小数点的输入显示,平分、开平方和对数的输入显示。可进行加减乘除、正弦、余弦、正切、反正弦、反余弦、反正切的计算,可求阶乘,倒数,括号输入,二进制转十进制、十进制转二进制。还可以通过输入变量x和y,结合坐标轴编辑框和曲线颜色编辑框实现函数y=f(x)的曲线绘制。
2.部分设计思路
计算器设计时主要利用到get和set两个函数进行各个控件属性值的传递和设置,利用strcat函数实现把两个字符串连接起来。利用length函数来计算字符串的长度实现后退的功能。利用eval函数将文本框中的字符串转换成数值表达式,利用MATLAB计算出结果返回显示。利用坐标轴axes和ezplot函数进行图形的绘制,利用factorial函数进行求阶乘运算。利用str2num及num2str实现数值与字符之间的转换。
3.系统设计
程序由MATLAB代码(.m)文件和GUI图形(.fig)文件两部分组成。
(1)布局构建:
首先在设计界面中添加上所需功能按钮、文本框、画图用的坐标轴等。调整好各个控件布局、通过双击各个功能按钮调整其参数(字体大小,标签名称、背景颜色等)。
(2)设置控件参数:
各个控件调整完毕后,在m文件中找到与相应按钮对应的回调函数并编写回调函数,以实现各个按钮的功能。
(3)回调函数的编写:
i.数字(以7为例):
ii.运算符(以+为例):
iii.三角函数(以sin为例):
iv.二进制转十进制:
v.绘图:
vi.删除键:
vii.清空键:
(4)功能展示:
注意:画图前应先设置按键最右侧一列的坐标范围。
4.完整代码
运行时必须与搭建的GUI界面放在同一文件夹下,直接运行即可,本例的m文件和gui文件在上传的资源中。
function varargout = jisuanqi(varargin)
% JISUANQI MATLAB code for jisuanqi.fig
% JISUANQI, by itself, creates a new JISUANQI or raises the existing
% singleton*.
%
% H = JISUANQI returns the handle to a new JISUANQI or the handle to
% the existing singleton*.
%
% JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in JISUANQI.M with the given input arguments.
%
% JISUANQI('Property','Value',...) creates a new JISUANQI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before jisuanqi_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to jisuanqi_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 jisuanqi
% Last Modified by GUIDE v2.5 24-Dec-2019 09:58:26
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...
'gui_OutputFcn', @jisuanqi_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 jisuanqi is made visible.
function jisuanqi_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 jisuanqi (see VARARGIN)
% Choose default command line output for jisuanqi
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes jisuanqi wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = jisuanqi_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)
%平方
textString = get(handles.text2,'String');
textString = strcat(textString,'^2');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
%开平方
textString = get(handles.text2,'String');
textString = strcat(textString,'^(1/2)');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
%阶乘
textString = get(handles.text2,'String');
ans = factorial(str2num(textString));
textString = strcat(textString,'!');
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
%删除键
textString = get(handles.text2,'String');
set(handles.text2,'String',' ');
s = char(textString);
n = length(textString);
textString = s(1:n-1);
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
%清空键
set(handles.text2,'String',' ');
set(handles.text1,'String',' '); %清空静态文本
axes(handles.axes1); %清空图像
cla reset
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'7');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'8');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'9');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'+');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'-');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'4');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'5');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'6');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'*');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'/');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'1');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'2');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'3');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'log(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'exp(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'pi');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'0');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'.');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,')');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'sin(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton28.
function pushbutton28_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'cos(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'tan(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles)
%2进制转10进制
textString = get(handles.text2,'String');
textString = bin2dec(textString);
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)
%十进制转二进制
textString = get(handles.text2,'String');
textString = dec2bin(str2num(textString));
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton32.
function pushbutton32_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'asin(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton33.
function pushbutton33_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'acos(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'atan(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton35.
function pushbutton35_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'log10(');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton36.
function pushbutton36_Callback(hObject, eventdata, handles)
%等号显示结果
textString = get(handles.text2,'String');
ans = eval(textString);
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)
% --- Executes on button press in pushbutton37.
function pushbutton37_Callback(hObject, eventdata, handles)
%关闭界面
close(gcf);
% --- Executes on button press in pushbutton43.
function pushbutton43_Callback(hObject, eventdata, handles)
%画图
fun = get(handles.text2,'String');%把text2中的字符串赋给gun
set(handles.text1,'String',fun);%显示在text1中
set(handles.text2,'String','');%清空text2
Xmin = get(handles.edit1,'String');
Xmax = get(handles.edit2,'String');
Ymin = get(handles.edit3,'String');
Ymax = get(handles.edit4,'String');
fun = ezplot(fun,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
color = get(handles.edit5,'String'); %颜色字符
set(fun,'Color',color); %换颜色
grid on %打开坐标轴网络线
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit3_Callback(hObject, eventdata, handles)
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit4_Callback(hObject, eventdata, handles)
function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit5_Callback(hObject, eventdata, handles)
function edit5_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton44.
function pushbutton44_Callback(hObject, eventdata, handles)
%绘图时所用的自变量x
textString = get(handles.text2,'String');
textString = strcat(textString,'x');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton45.
function pushbutton45_Callback(hObject, eventdata, handles)
%绘图时所用,y=f(x)
textString = get(handles.text2,'String');
textString = strcat(textString,'y');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton46.
function pushbutton46_Callback(hObject, eventdata, handles)
%等号 非计算结果 画图时使用
textString = get(handles.text2,'String');
textString = strcat(textString,'=');
set(handles.text2,'String',textString)
% --- Executes on button press in pushbutton47.
function pushbutton47_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
ans = 1/str2num(textString);
textString = strcat('1/',num2str(textString));
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)