用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,则会出现错误,需要避免这种情况的发生。
- 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);三、设计心得及体会:略.。