MATLAB课程设计报告
《MATLAB课程设计》报告
设计题目: MATLAB课程设计(简单计算器) 学生姓名:
学生学号: 201004444300 专业班级: 光信息
答辩时间: 2013-12
指导教师: 冯明库
广东技术师范学院
电子与信息学院
一、设计目的及意义
1、运用MATLAB实现MATLAB的GUI程序设计。
2、熟悉和掌握MATLAB 程序设计方法。
3、掌握MATLAB GUI 程序设计。
二、设计任务及指标
要求利用MATLAB GUI设计实现一个图形用户界面的计算器程序,要求实现:
A. 具有友好的用户图形界面。实现十进制数的加、减、乘、除、乘方、取模等简单计算。
B. 科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等运行。
C. 能够保存上次历史计算的答案,先是答案存储器中得内容。
D. 有清除键,能清除操作,并对不正确的表达式能指出其错误原因。
E. 独立存储器功能,使之可以直接输入存储器,可与存储器中的数值相加减。能够清除独立存储器中的内容。
三、设计过程
1.启动MATLAB
2.观察MATLAB窗口的各个组成部分
1)了解菜单栏各菜单功能,用鼠标打开MATLAB各个菜单,在状态栏里显示当前鼠标所指的菜单项的含义。
2)用鼠标指向常用工具栏的每个工具按钮,了解其含义。
3、题目分析,了解各显示框的分布和功能,然后在通过各个按钮的回调函数,实现简单的计算功能。
4、总体设计,首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及每个命令按钮,调整好各控件大小、颜色,整体布局。
5、具体设计,实现各功能界面设计。
四、结论及分析[2]
4.1 各功能界面设计
4.2 各功能模块实现
算法设计:
A. 数字键设计:0—9以及小数点函数都一样,只是参数不同: global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0) set(handles.text1,'String','1')
else
textString=strcat(textString,'1');
set(handles.text1,'String',textString)
end jj=0;
B. 四则运算函数:
textString=get(handles.text1,'String');
textString=strcat(textString,'+');
set(handles.text1,'String',textString)
C. 科学计算函数:
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a=strread(textString, '%f');
a=sin(a);
set(handles.text1,'String',a)
end
D. 退格键:通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:
global jj
textString=get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','0.')
else
ss=char(textString);
l=length(textString);
textString=ss(1:l-1);
set(handles.text1,'String',textString)
end jj=0;
E. 清屏键函数:
set(handles.text1,'String','0.')
4.3 各模块实现结果
A. 数字键:
B. 四则运算函数:
经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。 问题和解决方法:
a. 小数点可以连续输入。解决方法是:用strfind函数查看文本框里有几个小数点,如果已经有一个了,再按小数点就保持不变。
b. 按过运算符号后一个数不等于一个数,比如:输入1,按等号,会出来一个3,经过长时间分析得知,这是由于在按运算符号时,系统记录了文本框里的数但没有清空,才会出现这种问题。解决方法是再申请一个不同于加减乘除的另一个符号,并将按过运算符后记录的数值置0。
五、设计体会
通过本次的MATLAB课程设计,让我对MATLAB尤其是其GUI设计的功能有了进一步的了解,认识到了它功能的强大。在MATLAB简单计算器的设计中,了解了关于MATLAB图形用户界面的部分控件的使用方法;利用MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。
参考文献:
[1]刘卫国编著.MATLAB程序设计与应用(第二版).2006.7
[2]王洪元主编.MATLAB语言及其在电子信息工程中的应用 清华大学出版社 程序清单:
定义函数
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
gui_Singleton',
gui_Singleton, ...
gui_OpeningFcn', @calc_OpeningFcn, ...
gui_OutputFcn', @calc_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 calc is made visible.
function calc_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to calc (see VARARGIN)
% Choose default command line output for calc handles.output = hObject; % Update handles structure guidata(hObject, handles);
% UIWAIT makes calc wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.text1,'String','0.');
jj=0;
% --- Outputs from this function are returned to the command line. function varargout = calc_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure
% eventdata reserved - to be defined in pushbutton20 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;
按键0响应(按键0-9响应程序一样)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
%eventdata reserved - to be defined in pushbutton20 future version of MATLAB% handles structure with handles and user data(see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','0')
else
textString =strcat(textString,'0');
set(handles.text1,'String',textString)
end jj=0;
%按键‘/’响应
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved-to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
textString =strcat(textString,'/');
set(handles.text1,'String',textString)
%按键‘(’响应
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved-to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','(')
else
textString =strcat(textString,'(');
set(handles.text1,'String',textString)
end
%按键‘*’响应
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton15 (see GCBO)
% eventdata reserved- to be defined in pushbutton20 future version of MATLAB% handles structure with handles and user data (see GUIDATA) %end textString = get(handles.text1,'String'); textString =strcat(textString,'*'); set(handles.text1,'String',textString)
%按键‘)’响应
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved- to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)
set(handles.text1,'String',')')
else
textString =strcat(textString,')');
set(handles.text1,'String',textString)
end
%按键‘-’响应
% --- Executes on button press in pushbutton17.
% case '-'
% ans=pushbutton20-c;
% case '+'
% ans=pushbutton20+c;
%
% end ans =eval(textString);
set(handles.text1,'String',ans)
end
%按键‘+’响应
% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in pushbutton20
future version of MATLAB
% handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,'String');
textString =strcat(textString,'+');
set(handles.text1,'String',textString)
end
%按键‘sin’响应 % --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton21 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=sin(a); set(handles.text1,'String',a)
end
%按键‘cos’响应
% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton22 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else a = strread(textString, '%f');
a=cos(a); set(handles.text1,'String',a)
end
%按键‘tan’响应
% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton22 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=tan(a);
set(handles.text1,'String',a)
end
%按键‘log10’响应
% --- Executes on button press in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton23 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA) textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=log10(a); set(handles.text1,'String',a)
end
%按键‘sqrt’响应
% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton24 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=sqrt(a);
set(handles.text1,'String',a)
end
%按键‘+/-’响应
% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton34 (see GCBO)
% eventdata reserved-to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=0-a;
set(handles.text1,'String',a)
end
% 按键backspace响应
% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton18 (see GCBO)
% eventdata reserved-to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
global jj
textString = get(handles.text1,'String');
if(strcmp(textString,'0.')==1)&(jj==0)
set(handles.text1,'String','0.')
else
ss=char(textString);
l=length(textString);
textString=ss(1:l-1);
set(handles.text1,'String',textString)
end jj=0;
%按键‘X^2’响应
% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in pushbutton20 future version of MATLAB % handles structure with handles and user data (see GUIDATA)
textString = get(handles.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=a*a;
set(handles.text1,'String',a)
end
%按键‘C’响应
% --- Executes on button press in pushbutton28.
function pushbutton28_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton28 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
set(handles.text1,'String','0.')
%按键‘cot’响应
% --- Executes on button press in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton30 (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.text1,'String');
%strcmp(textString,'0.')
if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=cot(a);
set(handles.text1,'String',a)
end
%按键‘log2’响应
% --- Executes on button press in pushbutton48.
function pushbutton48_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton48 (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.text1,'String'); %strcmp(textString,'0.') if(strcmp(textString,'0.')==1)
set(handles.text1,'String','0.')
else
a = strread(textString, '%f');
a=log2(a);
set(handles.text1,'String',a)
end