用MATLAB做一个计算器_包括加减乘除_平方_清空等功能
MATLAB使用教程
MATLAB使用教程MATLAB是一种高级的计算机语言和环境,广泛应用于科学、工程和其他领域的数值计算和数据可视化。
它具有简单易学的语法,强大的计算功能和丰富的工具箱,成为许多研究人员和工程师的首选工具。
这篇文章将提供一个简要的MATLAB使用教程,帮助初学者快速上手使用MATLAB进行数值计算和数据可视化。
在MATLAB中,您可以进行基本的数值计算,例如加减乘除和幂运算。
例如,要计算1加2的结果,您可以在命令窗口中输入“1+2”,然后按回车键执行命令。
MATLAB将显示计算结果3此外,MATLAB还提供了许多内置的数学函数,例如sin、cos、exp等。
您可以通过输入函数名和参数来使用这些函数,并按回车键执行命令。
例如,要计算sin(2.5)的结果,您可以在命令窗口中输入“sin(2.5)”,然后按回车键执行命令。
MATLAB将显示计算结果。
A=[1,2,3;4,5,6;7,8,9];B=[2,0,1;3,1,2;4,5,6];C=A*B;上述代码将创建两个3x3的矩阵A和B,并将其相乘,结果存储在矩阵C中。
除了数值计算,MATLAB还可以进行数据可视化,使数据更容易理解和分析。
您可以使用MATLAB提供的绘图函数创建各种类型的图形,例如折线图、散点图和柱状图。
以下是一个例子:x = 0:0.1:2*pi;y = sin(x);plot(x, y);上述代码将创建一个x轴范围从0到2π的向量,并将其作为横坐标。
然后,计算sin(x)的值,并将其作为纵坐标。
最后,在坐标系中绘制这些数据点并连接它们以形成一条曲线。
此外,MATLAB还提供了许多其他的功能和工具箱,例如图像处理、信号处理和统计分析。
您可以通过学习官方文档和参考书籍来深入了解这些功能,并在实践中掌握它们的使用。
总之,MATLAB是一个强大的数值计算和数据可视化工具,适用于各种科学、工程和其他领域。
本教程提供了一个简要概述,帮助初学者快速上手使用MATLAB进行数值计算和数据可视化。
matlab gui计算器设计总结
matlab gui计算器设计总结设计MATLAB GUI计算器是一项有趣且具有挑战性的任务。
在这个过程中,你需要考虑到用户的需求和体验,以及如何实现这些需求的技术细节。
以下是对此任务的设计总结:1. 需求分析:在设计计算器之前,首先要明确用户的需求。
一个基本的计算器应该能够进行基本的算术运算(加、减、乘、除)以及求平方和平方根等操作。
此外,设计一个友好的用户界面也非常重要,以方便用户理解和使用计算器。
2. 创建GUI界面:MATLAB的图形用户界面(GUI)工具箱可以帮助你创建计算器的用户界面。
你可以使用各种小部件(如按钮、文本框和标签)来创建界面,并通过回调函数来定义小部件的行为。
3. 实现计算器功能:在MATLAB中,你可以使用脚本或函数来实现计算器的功能。
当用户点击按钮时,回调函数将被触发,执行相应的计算,并将结果显示在界面上。
4. 优化用户体验:良好的用户体验是GUI设计的重要方面。
你可以通过调整颜色、字体和布局来改善界面外观,并通过添加错误处理和提示信息来提高程序的易用性。
5. 测试和调试:完成设计后,进行彻底的测试是非常重要的。
检查所有功能是否正常工作,是否有任何错误或异常行为。
此外,通过用户反馈来进一步优化计算器也是一个好主意。
6. 代码组织和可维护性:为了使代码易于理解和维护,应该保持良好的代码组织和注释。
此外,考虑将计算逻辑和GUI代码分离,以方便未来的修改和扩展。
7. 扩展功能:除了基本功能外,还可以考虑添加一些高级功能,如科学计算、三角函数计算等。
这将为用户提供更多选择,并增加计算器的实用价值。
8. 跨平台兼容性:虽然MATLAB主要用于学术和工程领域,但确保GUI计算器在各种操作系统上都能正常工作仍然是一个好习惯。
这可能需要一些额外的测试和调整。
9. 文档和帮助系统:提供详细的文档和帮助信息可以帮助用户更好地理解和使用计算器。
你可以创建一个帮助文件或使用MATLAB的帮助系统来提供这些信息。
基于MATLAB的计算器设计
2021.07科学技术创新大数据时代,时时刻刻都在和数据打交道。
为了解决人们在学习、生活以及工作中遇到的一些常见的数据计算问题,利用MATLAB 设计出一款实用的、简单的计算器。
该计算器的操作过程很简单,只需使用者按下相应按钮,便能轻松解决以下问题:四则运算、对数运算、指数运算、开平方运算等。
1MATLAB GUI 简介MATLAB GUI 是指采用图形方式显示的计算机操作用户界面,是MATLAB 用户可视化交互式的工具,运用GUI 生成的操作界面,避免了用户浏览繁冗的代码进行操作。
实现了基于MATLAB GUI 的计算器功能的设计。
2计算器的设计过程及功能验证2.1计算器设计流程设计计算器,首先用MATLAB GUI 设计一个计算器界面,这个界面要包括加减乘除、三角函数、对数等按钮,然后编辑程序实现相应的功能,最后进行功能验证。
流程如图1所示。
图1计算器设计流程图2.2计算器界面设计在MATLAB 中新建一个GUI 界面,在GUI 界面上绘制一个面板,面板上绘制一个静态文本框用来显示输入和输出,再绘制30个按钮,每个按钮代表一种功能,双击按钮调出按钮的属性检查器,将按钮代表的功能标注在按钮上,最后调整按钮的大小和颜色、面板的颜色以及整体的布局使计算器看起来更美观,界面如图2所示:图2计算器界面图3输入数据基于MATLAB 的计算器设计Calculator Design Based on MATLAB赵海君赵青云(山西师范大学物理与信息工程学院,山西临汾041004)摘要:论文设计基于MATLAB 的多功能计算器,运用MATLAB 的GUI 设计计算器界面,通过编写程序实现四则运算、指数运算、对数运算、正余弦运算等功能。
关键词:MATLAB ;计算器;GUI Abstract :This paper designs a multi-function calculator based on MATLAB,uses Matlab GUI to design the calculator interface,and realizes four arithmetic operations,exponential operation,logarithm operation,sine cosine operation and other functions by programming.Key words :MATLAB;Calculator;GUI中图分类号:TP311.1文献标识码:A 文章编号:2096-4390(2021)07-0089-02教改项目:2018年山西师范大学校级教改项目“学生工程素质的培养在《传感器技术》课程教改中的实践”(2018JGXM-43),赵青云。
电信12-1班 1206110109matlab简易计算器设计步骤与回调函数设计
简易算术计算器的设计一、算术计算器的功能:能进行简单的加、减、乘、除法,sin,cos,tan,x^2,squrt运算,输入文本框显示为常量多项式,输出文本框显示为精确到小数点后十位的数值。
计算器包含十个数字键0~9、小数点键“.”、小括号键“(”“)”、运算符号、清屏键、退格键、退出键等。
二、设计思路:每按下一个数字键或者符号键的时候,利用get(handles.edit1,'string')获取当前输入文本框edit1的string属性,并通过字符串合并函数strcat()将当前输入文本框edit1的字符型string属性与按键的字符型string属性合并成字符串,然后利用属性设置函数set(); 将合并后的字符串返回到输入文本框edit1的string属性。
当输入结束,用户按下等号的时候,采用x=get(handles.edit1,'string')获取当前输入文本框edit1的string属性,然后利用字符串执行命令y1=eval(x)计算输入文本框的表达式,得到的结果是字符型常量;然后利用字符串输出格式控制函数y=sprintf('%.10f',y1)使输出结果精确到小数点后十位;最后利用属性设置函数set(handles.edit2,'string',y)将转换后的字符串返回到输出文本框edit2的string属性。
三、设计步骤:1、运行matlab2014a,进入主界面。
2、输入guide,进入GUI开发环境。
3、创建控件:按钮包括数字0~9、运算符“+ - * /”、功能键等26个,输入和动态静态文本框,显示文字的静态文本框四个等。
4、对控件单击右键弹出菜单,选择属性检查器,设置控件属性。
5、选择工具栏上的对齐对象按键,调整控件布局。
6、选择工具栏上的Tab 顺序编辑器按键,通过和设置各控件的Tab顺序。
7、设计菜单:在GUI开发环境下,选择工具栏上的进入菜单编辑器,分别选择和创建主菜单和子菜单。
Matlab实现的简易计算器程序代码
function varargout = jisuanqi(varargin)% JISUANQI M-file 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 20-Jul-2011 09:45:20% Begin initialization code - DO NOT EDITgui_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});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_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) handles.current_str='';%´ËΪ´æ´¢µ±Ç°µÄ×Ö·û´®handles.L1_str='';handles.L2_str='';%´ËΪ´æ´¢ÉÏÒ»¸öÊý×ÖµÄ×Ö·û´®% 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 jisuanqihandles.output = hObject;% Update handles structureguidata(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 structurevarargout{1} = handles.output;% --- Executes on button press in Number_0.function Number_0_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ0£¬Èç¹û°´ÁË0¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'0');handles.L1_str=strcat(handles.L1_str,'0');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_0 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in xiaoshudian.function xiaoshudian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'.');guidata(hObject, handles);%СÊýµã% hObject handle to xiaoshudian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jia_jian.function jia_jian_Callback(hObject, eventdata, handles)% hObject handle to jia_jian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_1.function Number_1_Callback(hObject, eventdata, handles)% hObject handle to Number_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ1£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'1');handles.L1_str=strcat(handles.L1_str,'1');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% --- Executes on button press in Number_2.function Number_2_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ2£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'2');handles.L1_str=strcat(handles.L1_str,'2');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_3.function Number_3_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ3£¬Èç¹û°´ÁË3¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'3');handles.L1_str=strcat(handles.L1_str,'3');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_4.function Number_4_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ4£¬Èç¹û°´ÁË4¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'4');handles.L1_str=strcat(handles.L1_str,'4');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_5.function Number_5_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ5£¬Èç¹û°´ÁË5¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'5');handles.L1_str=strcat(handles.L1_str,'5');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_6.function Number_6_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ6£¬Èç¹û°´ÁË6¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'6');handles.L1_str=strcat(handles.L1_str,'6');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_7.function Number_7_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ7£¬Èç¹û°´ÁË7¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'7');handles.L1_str=strcat(handles.L1_str,'7');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_8.function Number_8_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ8£¬Èç¹û°´ÁË8¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'8');handles.L1_str=strcat(handles.L1_str,'8');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_9.function Number_9_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ9£¬Èç¹û°´ÁË9¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'9');handles.L1_str=strcat(handles.L1_str,'9');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jiahao.function jiahao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'+');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jiahao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in denghao.function denghao_Callback(hObject, eventdata, handles)st = get(handles.edit1,'String');%¼ÆËã½á¹û£¬²¢´æ·Åµ½µÚ¶þ¸ö±à¼-¿òÖС£val = eval(st);s = num2str(val);set(handles.edit2,'String',s);guidata(hObject, handles);% hObject handle to denghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jianhao.function jianhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'-');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jianhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_daoshu.function X_daoshu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'\1');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_daoshu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chenghao.function chenghao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'*');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chenghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in baifenhao.function baifenhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/100');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);handles.L2_str=handles.L1_str;handles.L1_str = '';% hObject handle to baifenhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chuhao.function chuhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chuhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Sqrt.function Sqrt_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sqrt');handles.L1_str=strcat(handles.L1_str,'sqrt');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Sqrt (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_lifang.function X_lifang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^3');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_lifang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in x_pingfang.function x_pingfang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^2');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to x_pingfang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengqie.function zhengqie_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'tan');handles.L1_str=strcat(handles.L1_str,'tan');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengqie (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengxian.function zhengxian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sin');handles.L1_str=strcat(handles.L1_str,'sin');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in CE.function CE_Callback(hObject, eventdata, handles)% hObject handle to CE (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in C.function C_Callback(hObject, eventdata, handles)% hObject handle to C (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.current_str = '';set(handles.edit1,'String','0.');set(handles.edit2,'String','0.');guidata(hObject, handles);% --- Executes on button press in Backspace.function Backspace_Callback(hObject, eventdata, handles)% hObject handle to Backspace (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)s1 = handles.current_str;handles.current_str = s1(1:length(s1)-1);set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);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');endif ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;end% --- Executes on button press in shujujiazai.function shujujiazai_Callback(hObject, eventdata, handles)% hObject handle to shujujiazai (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global YSData;[filename,pathname,FILTERINDEX]=uigetfile({'*.xls';'*.dat';'*.*'},'Ñ¡ÔñÊý¾ÝÎļþ');if(FILTERINDEX==0)return;endstr_filename=[pathname,filename];fid=fopen(str_filename,'rt');if(fid==-1)errordlg('Open file error!','Open error');return;endYSData=xlsread(str_filename);set(handles.lujingxianshi,'String',str_filename);set(handles.tuxingxianshi,'Enable','on');function lujingxianshi_Callback(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.lujingxianshi,'String',pathname);% Hints: get(hObject,'String') returns contents of lujingxianshi as text % str2double(get(hObject,'String')) returns contents of lujingxianshi as a double% --- Executes during object creation, after setting all properties. function lujingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to lujingxianshi (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 zhuxingtu.function zhuxingtu_Callback(hObject, eventdata, handles)% hObject handle to zhuxingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',1);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhuxingtu% --- Executes on button press in zhexiantu.function zhexiantu_Callback(hObject, eventdata, handles)% hObject handle to zhexiantu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',1);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhexiantu% --- Executes on button press in yuanbingtu.function yuanbingtu_Callback(hObject, eventdata, handles)% hObject handle to yuanbingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',1);% Hint: get(hObject,'Value') returns toggle state of yuanbingtu% --- Executes on button press in tuxingxianshi.function tuxingxianshi_Callback(hObject, eventdata, handles)global YSData;%Åжϡ®Í¼ÏñÀàÐÍ¡¯ÏÂ×éºÏ¿òÖеĸ÷µ¥Ñ¡°´Å¥µÄÑ¡ÖÐÇé¿ö£¬²¢´æ´¢ÔÚIndex_rad iobuttonÖÐIndex_radiobutton=get([handles.zhuxingtu,handles.zhexiantu,handles.yuan bingtu],'Value');%½øÐÐͼÐÎÉú³Éif(Index_radiobutton{1}==1)bar(YSData);title('¸÷Êý¾ÝÖ±·½Í¼');endif(Index_radiobutton{2}==1)plot(YSData);title('¸÷Êý¾ÝÕÛÏßͼ');endif(Index_radiobutton{3}==1)pie(YSData);title('¸÷Êý¾ÝÔ²±ýͼ');end% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% global YSData;% --- Executes during object creation, after setting all properties. function tuxingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called 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 Quit.function Quit_Callback(hObject, eventdata, handles)% hObject handle to Quit (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 during object deletion, before destroying properties. function zhengxian_DeleteFcn(hObject, eventdata, handles)% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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。
用MATLAB做的 一个计算器
一、布局GUI。
1.打开Matlab,输入Guide 回车或者在工具栏上点击图标出现Guide 窗口:2.然后双击“Blank GUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。
最终设置的静态文本框为白色,其他按钮均为分红色。
5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。
以下是相应按钮的功能函数。
(1)数字按键编写。
在function pushbutton1_Callback(hObject, eventdata, handles)下输入:textString = get(handles.text1,'String');textString =strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。
同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。
(2).符号键:function pushbutton12_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString 后添加“+”进行运算。
如何在MATLAB中进行数值计算
如何在MATLAB中进行数值计算1.基本数学操作:-加法、减法、乘法、除法:使用+、-、*、/操作符进行基本算术运算。
-幂运算:使用^或.^(点乘)操作符进行幂运算。
- 开平方/立方:可以使用sqrt(或power(函数进行开平方和立方运算。
2.矩阵操作:- 创建矩阵:可以使用矩阵构造函数如zeros(、ones(、rand(等创建矩阵。
- 矩阵运算:使用*操作符进行矩阵相乘,使用transpose(函数进行矩阵转置。
- 矩阵求逆和求解线性方程组:使用inv(函数求矩阵的逆,使用\操作符求解线性方程组。
3.数值积分和微分:- 数值积分:使用integral(函数进行数值积分。
可以指定积分函数、积分上下限和积分方法。
- 数值微分:使用diff(函数进行数值微分。
可以指定微分函数和微分变量。
4.解方程:- 一元方程:使用solve(函数可以解一元方程。
该函数会尝试找到方程的精确解。
- 非线性方程组:使用fsolve(函数可以求解非线性方程组。
需要提供初始值来开始求解过程。
-数值方法:可以使用牛顿法、二分法等数学方法来求解方程。
可以自定义函数来实现这些方法。
5.统计分析:- 统计函数:MATLAB提供了丰富的统计分析函数,如mean(、std(、var(等用于计算均值、标准差、方差等统计量。
- 直方图和密度估计:使用histogram(函数可以绘制直方图,并使用ksdensity(函数进行核密度估计。
- 假设检验:使用ttest(或anova(函数可以进行假设检验,用于比较多组数据之间的差异。
6.数值优化:- 非线性最小化:使用fminunc(函数可以进行非线性最小化。
需要提供目标函数和初始点。
- 线性规划:使用linprog(函数可以进行线性规划。
需要提供目标函数和限制条件。
- 整数规划:使用intlinprog(函数可以进行整数规划。
需要提供目标函数和整数约束。
7.拟合曲线:- 线性拟合:使用polyfit(函数进行线性拟合。
matlab加减乘除运算
matlab加减乘除运算
Matlab是一种流行的科学计算软件,它可以进行各种数学运算,包括加减乘除运算。
在Matlab中,加法和减法运算可以使用'+'和'-'符号完成,乘法和除法运算可以使用'*'和'/'符号完成。
下面是一些示例代码,演示如何在Matlab中进行加减乘除运算:
加法运算:
a = 5;
b = 10;
c = a + b;
disp(c);
这个代码将输出15,因为a和b的值相加得到15,然后将结果赋给变量c。
减法运算:
a = 20;
b = 5;
c = a - b;
disp(c);
这个代码将输出15,因为a和b的值相减得到15,然后将结果赋给变量c。
乘法运算:
a = 3;
b = 4;
c = a * b;
disp(c);
这个代码将输出12,因为a和b的值相乘得到12,然后将结果赋给变量c。
除法运算:
a = 10;
b = 2;
c = a / b;
disp(c);
这个代码将输出5,因为a除以b得到5,然后将结果赋给变量c。
需要注意的是,在进行除法运算时,如果除数为0,则会出现错误,需要避免这种情况的发生。
基于MATLAB的多功能计算器设计与实现
基于MATLAB的多功能计算器设计与实现摘要随看信息技术的发展,数据变得越来越重要,针对数据的处理也越来越复杂,计算器在我们日常生活学习中扮演越来越重要角色。
本文基于MATLAB设计的GUI 多功能计算器,包括三部分:简易计算器、科学计算器和程序员计算器。
其中,简易计算器包括了常用的加减乘除;科学计算器在加减乘除上,添加了正余弦、阶乘和对数等常用的数学计算;程序员计算器包括了二进制、八进制、十六进制的相互转换和二进制的与、或和异或等逻辑运算。
此多功能计算器改进了以前功能单一的计算器,具有较强的实用性。
关键字:MATLAB; GUI ;多功能计算器AbstractWith the development of information technology, the data become more andmore complicated, according to data operations have become increasingly frequent, thecalculator is playing a more and more important role in leamning in our daily life. In this paper,MATLAB design of GUI based multi functional calculator, consists of three parts: a simplecalculator, scientific calculator and programmers calculator. The simple calculator, includingcommon add, subtract, multiply and divide; scientific calculator in add, subtract, multiply anddivide, add the commonly used mathematical cosine, factorial and logarithm calculation;programmers calculator includes the conversion of binary, octal, hexadecimal and binarysixteen and, or and XOR logic operation. The multi-function calculator before improvedsingle function has a strong practical.Keyword: MATLAB; GUI; Multi Function Calculator目录第1章绪论 (1)1.1.基于MATLAB的多功能计算器设计的目的和意义 (1)1.2国内外现状分析 (1)1.2.1 国内外计算器的发展 (1)1.2.2计算器的类型 (2)第2章数字信号处理原理概念 (4)2.1数字信号处理 (4)2.2数字信号处理的概述 (4)2.3用MATLAB实现信号处理 (4)2.3.1信号的取样 (4)2.3.2信号的重构 (5)第3章计算器总体设计 (6)3.1计算器整体设计思路 (6)3.2计算器的功能实现 (7)3.2.1数字0-9设计与实现 (7)3.2.2四则运算设计 (7)3.2.3简单科学计算设计 (7)3.2.4功能按钮设计 (7)第4章 MATLAB GUI界面设计 (8)4.1MATLAB GUI介绍 (8)4.2GUI界面创建的基本知识 (8)4.3计算器界面设计 (9)第5章 MATLAB程序设计 (10)5.1 M文件 (10)5.1.1局部变量与全局变量 (10)5.1.2M文件的编辑与运行 (11)5.1.3脚本文件 (13)5.1.4函数文件 (13)5.1.5函数调用 (14)5.2MATLAB的程序控制结构 (16)5.3 数据的输入与输出 (17)5.3.1 键盘输入语句(input) (17)5.3.2屏幕输出语句(disp) (17)5.3.3 M数据文件的存储/加载(save/load) (17)5.3.4二进制数据文件的存储/读取 (18)5.4.面向对象程序设计的基本方法 (19)5.4.1.创建类目录 (19)5.4.2.建立类的数据结构 (19)5.4.3创建类的基本方法 (19)5.4.4重载运算 (19)5.4.5面向对象的函数 (19)5.5MATLAB程序优化 (19)结语 (20)附录 (21)参考文献 (28)致谢...............................................................................................................第1章绪论1.1.基于MATLAB的多功能计算器设计的目的和意义当今时代是一个信息化的时代,信息化的时代到处充满了数据,生活在这个信息化时代的人们,无论人们在做什么行业,都需要时时刻刻跟数据打交道。
matlab制作的简易计算器
Matlab大作业——简易计算器一.实现的功能简易计算器能够计算简单的加减乘除,能够将计算过程和结果显示出来,并且能够清空和删除数字。
二.实现方式通过Matlab中的GUI接口设计这个简易计算器,通过GUI中的pushbottom设计输入数字的键和加减乘除等于等键,然后通过edit text设计显示屏。
更改回调函数实现相关的计算功能。
三.程序分析0~9数字键的代码设置%全局变量locaval用于存储用户输入的多位数值%全局变量gloval2用于存储待处理的第二位数值function pushbutton1_Callback(hObject, eventdata, handles)global locaval;a = get(handles.pushbutton1,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);加减乘除键的代码设置%全局变量flagnum存储运算符标志%全局变量global1用于储存第一个待处理数值function pushbutton10_Callback(hObject, eventdata, handles)a = get(handles.pushbutton10,'String');b = get(handles.text1,'String');set(handles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' ';flagnum=1;gloval1=b;guidata(hObject, handles);等号键功能的实现%根据flagnum运算标志用switch决策语句实现相应计算%需注意相应数据类型的转化function pushbutton17_Callback(hObject, eventdata, handles) global flagnumglobal gloval1global gloval2global locavallocaval=' ';gloval1=str2num(gloval1);gloval2=str2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=gloval1-gloval2;case4=gloval1+gloval2;case1=num2str(case1);case2=num2str(case2);case3=num2str(case3);case4=num2str(case4);switch flagnum;case 1set(handles.text1,'String',case1);case 2set(handles.text1,'String',case2);case 3set(handles.text1,'String',case3);case 4set(handles.text1,'String',case4);endguidata(hObject,handles)Backspace键的代码设置%算法实现:MA TLAB是用矩阵存储数据的,相应的可以取文本框的前N-1实现其功能function pushbutton19_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)endguidata(hObject,handles)清除按钮函数function pushbutton20_Callback(hObject, eventdata, handles)global locavallocaval=' ';set(handles.text1,'String','0.');guidata(hObject,handles)四.程序展示function varargout = jisuanji(varargin)% JISUANJI MATLAB code for jisuanji.fig% JISUANJI, by itself, creates a new JISUANJI or raises the existing% singleton*.%% H = JISUANJI returns the handle to a new JISUANJI or the handle to% the existing singleton*.%% JISUANJI('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in JISUANJI.M with the given input arguments.%% JISUANJI('Property','Value',...) creates a new JISUANJI or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before jisuanji_OpeningFcn gets called. An % unrecognized property name or invalid value makes propertyapplication% stop. All inputs are passed to jisuanji_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 edit1 to modify the response to help jisuanji% Last Modified by GUIDE v2.5 04-Jun-2013 16:55:11% Begin initialization code - DO NOT EDITglobal gloval1;global gloval2;global flagnum;global locaval;gui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanji_OpeningFcn, ...'gui_OutputFcn', @jisuanji_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before jisuanji is made visible.function jisuanji_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 jisuanji (see VARARGIN)% Choose default command line output for jisuanjihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanji wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = jisuanji_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 structurevarargout{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 edit1 % str2double(get(hObject,'String')) returns contents of edit1 as a doubleglobal locavallocaval=str2num(locaval);locaval=1/locaval;set(handles.edit1,'String',locaval);locaval=' ';guidata(hObject,handles)% --- 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 CreateFcnscalled% 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 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) global locaval;a = get(handles.pushbutton3,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton4,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton5,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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)a = get(handles.pushbutton6,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global flagnumglobal gloval1global locavallocaval=' ';flagnum=1;gloval1=b;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) global locaval;a = get(handles.pushbutton7,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton8,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton9,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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)a = get(handles.pushbutton10,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global flagnumglobal gloval1global locavallocaval=' ';flagnum=2;gloval1=b;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) global locaval;a = get(handles.pushbutton11,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton12,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global locaval;a = get(handles.pushbutton13,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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)a = get(handles.pushbutton14,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global flagnumglobal gloval1global locavallocaval=' ';flagnum=3;gloval1=b;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)a = get(handles.pushbutton16,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global flagnumglobal gloval1global locavallocaval=' ';flagnum=4;gloval1=b;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) global locaval;a = get(handles.pushbutton17,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;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) global flagnumglobal gloval1global gloval2global locavallocaval=' ';gloval1=str2num(gloval1);gloval2=str2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=gloval1-gloval2;case4=gloval1+gloval2;case1=num2str(case1);case2=num2str(case2);case3=num2str(case3);case4=num2str(case4);switch flagnum;case 1set(handles.edit1,'String',case1);case 2set(handles.edit1,'String',case2);case 3set(handles.edit1,'String',case3);case 4set(handles.edit1,'String',case4);endguidata(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) edit1String = get(handles.edit1,'String');if(strcmp(edit1String,'0.')==1)set(handles.edit1,'String','0.') ;elsess=char(edit1String);l=length(edit1String);edit1String=ss(1:l-1);set(handles.edit1,'String',edit1String)endguidata(hObject,handles)% --- 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) global locavallocaval=' ';set(handles.edit1,'String','0');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五.结果展示。
MATLAB做简易计算器
MATLAB做简易计算器1、数字键的编写:textstring=get(handles.text1,'string');textstring=strcat(textstring,'0');set(handles.text1,'string',textstring);这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString')在text1中输出。
同理,1~9也是这样实现。
2、符号键:textstring=get(handles.text1,'string');textstring=strcat(textstring,'+');set(handles.text1,'string',textstring);strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString后添加“+”进行运算。
然后执行set(handles.text1,'String',textString)。
符号键‘-’、‘*’、‘/’与‘+’的运算函数类似。
3、等于键:textstring=get(handles.text1,'string');s=eval(textstring);set(handles.text1,'string',s);“eval”的作用是将符号表达式转换成数值表达式。
再由set(handles.text1,'String',s)输出4、清除键:set(handles.text1,'string',' ');计算器使用:‘+’‘=’后:源程序: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 propertyapplication% 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 04-May-2015 16:44:30% Begin initialization code - DO NOT EDITgui_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});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_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 jisuanqihandles.output = hObject;% Update handles structureguidata(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 structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'1');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'2');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'3');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'+');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)textstring=get(handles.text1,'string');textstring=strcat(textstring,'4');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'5');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'6');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'-');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'7');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton14.function pushbutton14_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'8');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'9');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'*');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles);set(handles.text1,'string',' ');% 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)% --- Executes on button press in pushbutton18.function pushbutton18_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'.');set(handles.text1,'string',textstring);% 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)% --- Executes on button press in pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');s=eval(textstring);set(handles.text1,'string',s);% 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)% --- Executes on button press in pushbutton20.function pushbutton20_Callback(hObject, eventdata, handles) textstring=get(handles.text1,'string');textstring=strcat(textstring,'/');set(handles.text1,'string',textstring);% 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)。
用MATLAB做一个计算器包括加减乘除平方清空等功能
广西科技大学MATLAB课程设计说明书课题:设计简易计算器开发环境:MATLAB GUIDE作者:系别:专业:时间:实验目的:1、熟悉MATLAB的主要控件使用方法。
2、熟悉MATLAB的GUI设计流程。
实验环境:编程软件:MATLAB7.0实验内容与结果:使用MATLAB的GUI接口设计一个简单的计算器。
效果图:一、布局GUI。
1.打开Matlab,输入Guide 回车或者在工具栏上点击图标出现Guide 窗口:2.然后双击“Blank GUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。
最终设置的静态文本框为白色,其他按钮均为分红色。
5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。
以下是相应按钮的功能函数。
(1)数字按键编写。
在function pushbutton1_Callback(hObject, eventdata, handles)下输入:textString = get(handles.text1,'String');textString =strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。
同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。
基于Matlab的简易计算器
工程设计报告设计题目:基于Matlab的简易计算器学院:专业:班级:学号:姓名:电子:日期:2015年12 月成绩:指导教师:. 西 安 电 子 科 技 大 学 电 子 工 程 学 院 工 程 设 计 任 务 书 学生 指导教师 职称 学生学号 专业 题目 基于Matlab 的简易计算器 任务与要求 任务如下: 利用MATLAB GUI 设计实现一个图形用户界面的计算器程序,实现: A.实现十进制数的加、减、乘、除、简单计算。
B. 科学计算函数,包括正弦、余弦、正切、余切、开方、指数等函数运行。
C. 有清除键,能清除操作。
要求如下: A .熟练掌握Matlab GUI 界面的设计与应用 B .最终计算器能够实现预期的相关功能开始日期 2015年 11月 日 完成日期 2016年1月 日课程设计所在单位本表格由电子工程学院网络信息中心 编辑录入 . …………………………装………………………………订………………………………线………………………………………………………………摘要基于Matlab GUI计算器设计时利用GUI的创建图像用户界面进行计算器设计。
设计计算器时,主要是考虑到计算器的易用性、功能的常用程度进行计算器界面与功能的设计。
通过调整控件和文本的布局及颜色,使界面简单大方、布局合理,达到界面友好的效果。
计算器设计时主要利用到get和set两个函数进行各个控件属性值的传递和设置。
计算器实现的功能有:数字0~9和小数点的输入显示,平方开方和对数的输入显示。
进行四则运算、正弦函数、余弦函数、正切函数以及反正弦函数、反余弦函数、反正切函数的计算等等。
最后运行调试,实现基于Matlab GUI的计算器的设计。
关键词:Matlab GUI 计算器AbstractsBased on Matlab GUI calculator design using the user interface to create images of GUI calculator design.Design calculator, mainly considering the ease of use, function calculators calculator interface and function of the common level of design.By adjusting the control and the layout of the text and color, make the interface simple and easy, rational layout, to achieve the effect of friendly interface.Calculator design used to get and set two main function for each attribute value transfer and control Settings.Calculator the functions are: 0 ~ 9, according to input and decimal square root and logarithm of input.Arithmetic, sine function and cosine function, tangent function and the arcsine function,arccosine function,the calculation of the arctangent function and so on.Finally running debugging, implementation design based on Matlab GUI calculator.Keywords: Matlab GUI calculator目录摘要 (4)Abstracts (4)目录 (6)第一章绪论 (8)1.MATLAB简介: (8)2.本文安排: (8)第二章GUI设计界面 (10)2.1打开GUI (11)2.2.添加按钮 (11)2.3.根据按钮的作用及视觉效果做一定的修改 (11)2.4.保存、添加功能函数 (12)2.4.1 数字键编写 (12)2.4.2 符号键的编写 (13)2.4.3 运算符“=”的编写 (13)2.4.4 按键“←back”的编写 (13)2.4.5 按键“清空”的编写 (14)2.4.6 按键“退出”的编写 (14)第三章计算器的使用和抽样检验 (15)3.1 除法运算(÷) (15)3.2 平方运算(^2) (15)3.3 函数cos (∏/3)的计算 (17)3.4 函数arcsin(∏/3)的计算 (17)3.5 以2为底的对数(log 2)的计算 (18)3.6 总结: (18)第四章特殊函数的源代码及总结 (19)函数代码及思路: (19)总结:问题及其优化 (21)第五章心得体会 (22)参考文献 (23)第一章绪论1.MATLAB简介:MATLAB是matrix和laboratory两个词的组合,意为矩阵工厂(矩阵实验室)。
学习使用MATLAB进行科学计算
学习使用MATLAB进行科学计算在科学领域的研究和实践中,计算机在数据处理、分析和建模方面的作用越来越重要。
而MATLAB作为一款强大且易于使用的科学计算工具,被广泛应用于工程、物理、数学和计算机科学等领域。
本文将介绍如何学习使用MATLAB进行科学计算,以及一些常用的实例和技巧。
一、MATLAB简介MATLAB是由MathWorks公司开发的一款交互式的科学计算软件。
它具有强大的数学计算和数据可视化功能,支持各种数值计算、矩阵运算、符号计算、数据分析等任务。
MATLAB还提供了丰富的工具箱,涵盖了信号处理、图像处理、控制系统等多个领域。
二、安装与入门1. 下载与安装在MathWorks官方网站上下载适合自己操作系统的MATLAB安装程序,并按照提示进行安装。
2. 运行与界面打开MATLAB程序后,会弹出一个交互式的命令行窗口,同时也会有一个图形用户界面(GUI)。
我们可以在命令行窗口中直接输入MATLAB代码进行计算,或者使用GUI进行更加直观的操作。
三、基本语法与数据结构1. MATLAB语法MATLAB采用类似于C语言的语法,支持函数、运算符和控制结构等。
下面是一些基本的语法规则:- 语句以分号结尾表示不输出结果;- 使用等号赋值,例如:x = 10;- 变量名区分大小写;- 使用圆括号表示数组或矩阵,例如:A = [1 2 3; 4 5 6]。
2. 常用数据结构MATLAB支持多种数据结构,常见的包括:- 数组:一维或多维的数值集合,可以进行矩阵运算;- 结构体:包含不同类型数据的集合,类似于字典;- 单位数组:可以表示时刻、时间段等概念的数据类型。
四、基本计算与函数1. 基本运算MATLAB支持各种数学运算,包括加减乘除、幂运算、三角函数等。
例如,可以使用"+"进行加法运算,"*"进行乘法运算。
2. 内置函数MATLAB提供了丰富的内置函数,可以执行各种常见的数值计算和数据处理操作。
基于matlabe简易计算器的设计论文
visual c++简单计算器课程设计专业:通信工程班级:通信一班学号:2404070132姓名:王龙龙指导教师:吴向东实验时间:2011年3月18日一、实验目的与要求1、实验目的:通过学习掌握面向对象编程的分析设计方法,以及与面向对象技术相关的一些软件开发技术,同时熟悉Visual C++6.0的开发环境,并掌握在 VisualC++6环境下进行可视化程序设计技术。
通过该课程设计的实践,为进一步开展相关领域的学习和科研打下良好的基础。
2、实验要求:在Visual C++6.0环境下设计一个具有如下功能的简单计算器:具有加、减、乘、除、倒数、阶乘、平方和显示功能二、实验过程:1、建立工程:2、添加按键并设置属性:3、程序设计及编辑:1)、数字“1”键程序(0~9键类似):void CCaculator_loloView::OnButton1(){if(c==0)a=1+a*10;else{a=a+1*pow(0.1,i);i++;}m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }2)“.”键程序:void CCaculator_loloView::OnButton11(){i=1;c=1;UpdateData(true);//DO: Add your control notification handler code here}3)“=”键程序:void CCaculator_loloView::OnButton12(){switch (d){case 1:a=b+a;break;case 2:a=b-a;break;case 3:a=b*a;break;case 4:a=b/a;break;//case 5:a=1/a;break;}m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }4)“+”键程序:void CCaculator_loloView::OnButton13(){b=a;a=0;c=0;d=1;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }5)“-”键程序:void CCaculator_loloView::OnButton14(){b=a;a=0;c=0;d=2;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }6)“*”键程序:void CCaculator_loloView::OnButton15(){b=a;a=0;c=0;d=3;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }7)“/”键程序:void CCaculator_loloView::OnButton16(){b=a;a=0;c=0;d=4;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }8)清楚“C”键程序:void CCaculator_loloView::OnButton17(){a=0;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }9)倒数“1/X”键程序:void CCaculator_loloView::OnButton18(){a=1/a;m_out=a;UpdateData(false);// TODO: Add your control notification handler code here }10)阶乘“X!”键程序:void CCaculator_loloView::OnButton19(){for(m=1;a>=1;a--)m*=a;m_out=m;UpdateData(false); // TODO: Add your control notification handler code here }11)平方“X^2”键程序:void CCaculator_loloView::OnButton20(){n=a*a;m_out=n;UpdateData(false); // TODO: Add your control notification handler code herea=0;}4、调试程序并修改,计算器最终结果如下图:四、结论在做这个课程设计之前对Visual C++的设计还很陌生,但通过课堂学习和课程设计的摸索,掌握了很多Visual C++设计的技术。
matlabGUI设计(简易科学计算器)
MATLAB大作业班级:姓名:学号:计算器➢题目本题目通过MATLAB的gui程序设计较为简单,在gui设计中主要用到三种控件,文本编辑框(edit text),静态文本框(Static text),命令按钮(push button)。
然后在通过各个按钮的回调函数,实现简单的计算功能。
➢1、功能介绍(1)具有友好的用户图形界面。
实现十进制数的加、减、乘、除、乘方、开方等简单计算。
(2)具有科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。
(注:三角函数计算的是弧度而不是角度)。
(3)有清除键,能清除操作。
➢2、功能实现程序由两个部分组成:MATLAB代码(.m文件)和GUI图形(.fig)。
程序使用的流程:直接利用图形界面中的按键键入所需数值、运算符等即可得出结果。
备注:软件版本:MATLAB 2011b首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:(附录中有相关属性修改介绍)然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。
2.1 各功能界面设计GUI设计界面:注:底部边框用(Panel)工具添加,有两种设计顺序。
(1、先加底部边框,再在底部边框上画功能键。
2、先画功能键,布好局,画底框,全选功能键拖动到底框上。
)2.2 各功能模块实现(可根据需要增减功能键)算法设计:1. 数字键设计:0—9以及小数点函数都一样,只是参数不同:例如:按键‘1’响应:global jjtextString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','1') ;elsetextString =strcat(textString,'1');set(handles.text1,'String',textString)endjj=0;2. 四则运算函数:‘+’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)‘-’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'-');set(handles.text1,'String',textString)‘×’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'*');set(handles.text1,'String',textString)‘÷’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'/');set(handles.text1,'String',textString)3. 科学计算函数:例如:‘sin’功能响应:textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end4. 退格键(DEL):通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:global jjtextString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)endjj=0;5. 清屏键函数(AC):set(handles.text1,'String','0.') ;2.3 各模块程序添加方法选中一个需添加程序的功能键,右击,View Callbacks,Callback,出现如下图所示界面。
matlab数组做统一加减乘除变换
标题: MATLAB中数组的统一加减乘除变换简介:在MATLAB中,对数组进行统一的加减乘除变换是非常常见的操作。
这些变换能够高效地处理大量数据,为数据分析和数值计算提供了强大的支持。
本文将详细介绍如何在MATLAB中对数组进行统一的加减乘除变换,包括基本的算术运算、向量化操作以及广播机制等。
正文:在MATLAB中,数组是一种重要的数据结构,它可以存储多个相同类型的元素。
对数组进行统一的加减乘除变换,意味着对数组中的每个元素执行相同的运算操作。
1. 基本算术运算MATLAB支持基本的算术运算符,如加号+、减号-、乘号*和除号/。
这些运算符可以直接应用于数组,实现对数组中每个元素的统一变换。
例如,假设有两个数组A和B,我们对它们进行加法运算:matlabA = [1, 2, 3];B = [4, 5, 6];C = A + B; % 结果 C = [5, 7, 9]同样地,减法、乘法和除法运算也可以这样进行:matlabD = A - B; % 结果 D = [-3, -3, -3]E = A * B; % 结果 E = [4, 10, 18] (对应元素相乘)F = A ./ B; % 结果 F = [0.25, 0.4, 0.5] (对应元素相除)2. 向量化操作在MATLAB中,向量化操作是一种高效的数据处理方式。
它允许你使用简单的算术运算符对整个数组进行操作,而不需要使用循环或迭代。
例如,如果你想给数组A中的每个元素都加上一个常数k,可以直接这样做:matlabA = [1, 2, 3];k = 10;A = A + k; % 结果 A = [11, 12, 13]同样地,你可以对整个数组进行乘法、除法等操作:matlabA = [1, 2, 3];k = 2;A = A * k; % 结果 A = [2, 4, 6]A = A / k; % 结果 A = [0.5, 1, 1.5]3. 广播机制在MATLAB中,广播机制允许你对不同大小的数组进行算术运算。
基于matlab的计算器编程附代码
1.需求分析本次的实验要求是设计一个计算器,主要功能如下:(1)实现基本数学运算(加减乘除等),而且要能进行混合运算(2)实现部分函数功能,如求平方根、求倒数等(3)能实现小数运算界面与标准计算器界面类似根据要求以及以前的学习情况,决定使用matlab进行编程。
Matlab强大的计算功能以及便捷的GUI设计,可以较为简便的实现所要求的功能。
按照要求,数据输入和输出支持小数点,支持四则混合运算,决定使用如下几个数据进行分析:(1+3)*5Sqrt(4)1/2Sin4用以检验是否可以进行加减乘除四则运算、平方根、倒数、正弦的运算。
2.程序设计M atlab的程序设计较为简便,用GUI设计出一个计算器的模型,然后系统会自动生成一个框架,在框架中,写入每一个按键对应的程序就可以实现功能。
3.调式分析编程的过程中遇到的问题不是很多,基本就是找要实现各个功能的子程序,通过上网和去图书馆,加上自己的编写,终于实现了实验要求的功能。
但是有一点很重要,matlab不支持中文,所以从路径到文件名必须是全英文的,不然就无法识别。
此外,给每个按键命名也是很重要的,不然在生成的程序框架里面,就无法识别各个按键的作用,编写程序的时候也就无法做到一一对应。
4.使用说明程序的使用比较简单,由于是可视化界面,直接打开matlab,然后建立一个GUI 工程,再打开生成的fig文件,就是一个计算器的界面,直接按照市面上卖的计算器的方法,按键使用即可。
5.测试结果计算结果为204sqrt=2Sin4结果为1/2=0.5经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。
6.心得体会本次试验由于不限制语言,于是计算功能强大,操作简便的matlab变成了首选,matlab的GUI设计,操作是较为简单的,首先建立一个GUI工程,然后用可视化界面,设计出计算器的大致外观,为每一个按键设置预计的功能,然后就是将对应的函数写到框架生成的程序中,然后每个按键就可以实现对应的功能。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
广西科技大学MATLAB课程设计说明书课题:设计简易计算器开发环境:MATLAB GUIDE作者:系别:专业:时间:实验目的:1、熟悉MATLAB的主要控件使用方法。
2、熟悉MATLAB的GUI设计流程。
实验环境:编程软件:MATLAB7.0实验内容与结果:使用MATLAB的GUI接口设计一个简单的计算器。
效果图:一、布局GUI。
1.打开Matlab,输入Guide 回车或者在工具栏上点击图标出现Guide 窗口:2.然后双击“Blank GUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。
最终设置的静态文本框为白色,其他按钮均为分红色。
5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。
以下是相应按钮的功能函数。
(1)数字按键编写。
在function pushbutton1_Callback(hObject, eventdata, handles)下输入:textString = get(handles.text1,'String');textString =strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。
同理,分别在function pushbutton2~10_Callback(hObject, eventdata, handles)下给1~9数字按键下编写此类程序。
(2).符号键:function pushbutton12_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString 后添加“+”进行运算。
然后执行set(handles.text1,'String',textString)。
符号键‘-’、‘*’、‘/’与‘+’的运算函数类似。
“平方运算”,主要是由“^2”功能实现。
(3)运算符“=”的编程:textString = get(handles.text1,'String');ans =eval(textString);“eval”的作用是将符号表达式转换成数值表达式。
再由set(handles.text1,'String',ans)输出。
(4)按键“back”编程:即显示一个空字符:set(handles.text1,'String','') ;按键“back”编程:textString = get(handles.text1,'String');set(handles.text1,'String','') ;ss=char(textString);n=length(textString);textString=ss(1:w-1);%将w个字符串转成w-1个,即删除了末位字符set(handles.text1,'String',textString)(5)按键“清空”:把静态文本框的字符清空,返回一个空格set(handles.text1,'String','')(6)按键“退出”:关闭计算器close(gcf);二、计算器的使用加法运算(+):按“=”后显示平方运算:按“=”后显示附各按键的程序源代码:function pushbutton1_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'0')set(handles.text1,'String',textString)function pushbutton2_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'1')set(handles.text1,'String',textString)function pushbutton3_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'2')set(handles.text1,'String',textString)function pushbutton4_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'3')set(handles.text1,'String',textString)function pushbutton5_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'4')set(handles.text1,'String',textString)function pushbutton6_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'5')set(handles.text1,'String',textString)function pushbutton7_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'6')set(handles.text1,'String',textString)function pushbutton8_Callback(hObject, eventdata, handles)textString=get(handles.text1,'String')textString=strcat(textString,'7')set(handles.text1,'String',textString)function pushbutton9_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'8')set(handles.text1,'String',textString)function pushbutton10_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'9')set(handles.text1,'String',textString)function pushbutton11_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'+')set(handles.text1,'String',textString)function pushbutton12_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'-')set(handles.text1,'String',textString)function pushbutton13_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'*')set(handles.text1,'String',textString)function pushbutton14_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'/')set(handles.text1,'String',textString)function pushbutton15_Callback(hObject, eventdata, handles)textString=get(handles.text1,'String')textString=strcat(textString,'.')set(handles.text1,'String',textString)function pushbutton16_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')s=eval(textString)set(handles.text1,'String',s)function pushbutton17_Callback(hObject, eventdata, handles) textString=get(handles.text1,'String')textString=strcat(textString,'^2')set(handles.text1,'String',textString)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.text1,'String')w=length(textString)t=char(textString)textString=t(1:w-1)set(handles.text1,'String',textString)function pushbutton19_Callback(hObject, eventdata, handles)set(handles.text1,'String','')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) close(gcf);三、设计心得及体会:略.。