Matlab设计简易计算器

article/2025/10/19 16:20:56

效果如如下:

整个工程还是挺简单的,之前一直都是用matlab做信号处理,由于要做课程设计,就学了一下matlab的GUI。下面总结几个关键的地方。

(1)控件拉到自己喜欢的位置,并将控件的Text和Tag改好(不改也行,看个人习惯)

(2)设置一个全局变量global TextString,用来记录用户按了什么按钮,把字符串拼接起来

(3)做好每个按钮的回调函数的工作

(4)eval()函数能够把字符串转换成matlab命令并返回结果(用在‘=’的回调函数)

function varargout = untitled2(varargin)
% UNTITLED2 MATLAB code for untitled2.fig
%      UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing
%      singleton*.
%
%      H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to
%      the existing singleton*.
%
%      UNTITLED2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED2.M with the given input arguments.
%
%      UNTITLED2('Property','Value',...) creates a new UNTITLED2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled2_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 untitled2% Last Modified by GUIDE v2.5 09-Jul-2021 22:30:29% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @untitled2_OpeningFcn, ...'gui_OutputFcn',  @untitled2_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 untitled2 is made visible.
function untitled2_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 untitled2 (see VARARGIN)% Choose default command line output for untitled2
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes untitled2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = untitled2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% Get default command line output from handles structure
varargout{1} = handles.output;function window_Callback(hObject, eventdata, handles)
% hObject    handle to window (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 window as text
%        str2double(get(hObject,'String')) returns contents of window as a double% --- Executes during object creation, after setting all properties.
function window_CreateFcn(hObject, eventdata, handles)
% hObject    handle to window (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
global TextString% --- Executes on button press in one.
function one_Callback(hObject, eventdata, handles)
% hObject    handle to one (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString =  get(handles.one,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)
% --- Executes on button press in two.
function two_Callback(hObject, eventdata, handles)
% hObject    handle to two (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.two,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in three.
function three_Callback(hObject, eventdata, handles)
% hObject    handle to three (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.three,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in four.
function four_Callback(hObject, eventdata, handles)
% hObject    handle to four (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.four,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in five.
function five_Callback(hObject, eventdata, handles)
% hObject    handle to five (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.five,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in six.
function six_Callback(hObject, eventdata, handles)
% hObject    handle to six (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.six,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in seven.
function seven_Callback(hObject, eventdata, handles)
% hObject    handle to seven (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.seven,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in eight.
function eight_Callback(hObject, eventdata, handles)
% hObject    handle to eight (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.eight,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in nine.
function nine_Callback(hObject, eventdata, handles)
% hObject    handle to nine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.nine,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in addition.
function addition_Callback(hObject, eventdata, handles)
% hObject    handle to addition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.addition,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in subtraction.
function subtraction_Callback(hObject, eventdata, handles)
% hObject    handle to subtraction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.subtraction,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in multiplication.
function multiplication_Callback(hObject, eventdata, handles)
% hObject    handle to multiplication (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.multiplication,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject    handle to division (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.division,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in sqrt.
function sqrt_Callback(hObject, eventdata, 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)
global TextString
textString = get(handles.sqrt,'String')
TextString = strcat(TextString , textString) 
set(handles.window,'string',textString)% --- Executes on button press in sin.
function sin_Callback(hObject, eventdata, handles)
% hObject    handle to sin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.sin,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in cos.
function cos_Callback(hObject, eventdata, handles)
% hObject    handle to cos (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cos,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in tan.
function tan_Callback(hObject, eventdata, handles)
% hObject    handle to tan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.tan,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in square.
function square_Callback(hObject, eventdata, handles)
% hObject    handle to square (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.square,'String')
TextString = strcat(TextString ,'^2') 
set(handles.window,'string',textString)% --- Executes on button press in cot.
function cot_Callback(hObject, eventdata, handles)
% hObject    handle to cot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.cot,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
% hObject    handle to equal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
ans = eval(TextString)
a = strcat(TextString,'=')
aa = strcat(a,num2str(ans))
set(handles.window,'string',aa)
TextString = []% --- Executes on button press in right.
function right_Callback(hObject, eventdata, handles)
% hObject    handle to right (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.right,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in left.
function left_Callback(hObject, eventdata, handles)
% hObject    handle to left (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
textString = get(handles.left,'String')
TextString = strcat(TextString ,textString) 
set(handles.window,'string',textString)% --- Executes on button press in qingkong.
function qingkong_Callback(hObject, eventdata, handles)
% hObject    handle to qingkong (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global TextString
TextString = []

 


http://chatgpt.dhexx.cn/article/rwhkiFg9.shtml

相关文章

简易计算器(有界面)

(没有括号和优先级,简易计算器)界面: package javaprogram;import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import j…

Andriod设计简易计算器

1.设计任务及要求 (1)设计一款基于Android系统下的计算器,实现加减乘除算法,以及清零、撤销操作。界面设计应该就尽量简洁而美观,具有良好的交互性,程序应具有较好的稳健性; &a…

java实现简易计算器

Java简易计算器 用java语言写的一个简易计算器,实现了最基本的、-、*、/ 运算。 先来看下效果: 界面简述: 整个面板的由一个JTextFiled组件16个JButton组件构成,外加一个JPanel组件存放16个按钮,其布局为4x4的网格…

做一个简易计算器(VB版)

今天小编来带大家用VB做一个简易计算器 废话不多说,下面就是具体步骤了 1、创建控件组的方法首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行…

简易计算器的设计_C#课程设计

以下内容可且仅可供参考,如有错误欢迎指正。 部分内容借鉴自百度 侵删致歉 位切换键盘的实现用了复杂的拖64给label的方法,此功能可以在自己计算机上的计算器里找到。 目录 一、设计简介 1.设计背景 2.开发工具及环境 二、需求分析 1.设计功能要求 …

Python制作简易计算器(GUI)---Tkinter

Python制作简易计算器(GUI)---Tkinter Tkinter简介Tkinter 与 PyQt5 的比较TkinterPyQt5 项目展示导入模块函数封装1. 运算公式的拼接与展示2. 将显示框的内容删除3. 使用eval()函数对表达式求值 主逻辑1. 布局窗口2. 布局表达式展示区域3. 布局按钮 代码…

自制个性计算器

一、布局文件xml的制作。 如图:是布局之后的一个效果图 完全可以自定义自己喜欢的各种类型的图片。 代码如下: 正所谓每一个鲜艳靓丽app背后都有成堆的代码支撑这句话是没有错的。由于没有长截屏软件只能用短截屏了,大家谅解一下&#…

C++简易计算器的实现

定义: 计算器是近代人发明的可以进行数字运算的机器。 也就是说,计算器不等同于算盘,前者能自行运算,后者只能简便计算过程,在古代,人们发明了许多计算工具,如算筹、算盘、计算尺等,随着社会的发展和科技的进步,计算…

制作简易计算器

简易计算器 效果展示图项目要求内容分析涉及函数 效果展示图 项目要求 制作简易计算器,使其能实现简单的加,减,乘,除运算。 1.在两个文本框中分别输入两个数字 2.输入完成后,再次点击加,减,乘&…

C# 制作简易计算器

前言:环境是vs 2022 1、打开vs2022后,右边导航栏选择创建新项目。 2、选择Windows窗体应用(.net Framework) 3、进入配置新项目界面(项目名称和位置可自行修改)点击创建 4、窗体From1即为我们要要编辑的位…

至简设计系列_简易计算器

–作者:小黑同学 本文为明德扬原创及录用文章,转载请注明出处! 1.1 总体设计 1.1.1 概述 计算器是近代人发明的可以进行数字运算的机器。现代的电子计算器能进行数学运算的手持电子机器,拥有集成电路芯片,但结构比电…

如何制作一个简易的计算器

今天,我们来学习如何完成一个简易计算器的功能吧! 一.布局 我们可以在HTML中使用CSS完成计算器的布局。接下来,我们便先来看看我们所要实现的效吧! 上图的计算器中,我们可以通过以下几个步骤完成对计算器的简单布局…

简易版计算器

这次我们来写一个简单的计算器案例 代码部分 HTML部分 首先设置一个基本样式,把我们需要的“计算”按钮,“-*/”四个计算符合,三个文本框准备好(两个用来输入数字进行计算,一个用来接收计算出来的结果),再为三个文…

java拦截器怎么实现

Java拦截器是一种对象拦截器,它可以拦截任何的类、方法和字段。拦截器还可以用于检查类是否已经加载以及对字段的访问权限是否符合规范。下面就让我们来了解一下 java拦截器怎么实现吧。 在 Java中,可以通过重写方法和代码块来实现拦截功能,但…

Java中的过滤器和拦截器

Java中的过滤器和拦截器 一.应用场景 拦截器应用场景 拦截器本质上是面向切面编程(AOP),符合横切关注点的功能都可以放在拦截器中来实现,主要的应用场景包括: 登录验证,判断用户是否登录。权限验证&…

拦截器(HandlerInterceptor)

目录 1 什么是拦截器 2 HandlerInterceptor和WebMvcConfigurer 2.1 HandlerInterceptor 2.2 WebMvcConfigurer 3 拦截器实现流程 1 什么是拦截器 拦截器是相对于Spring中来说的,它和过滤器不一样,过滤器的范围更广一些是相对于Tomcat容器来说的。拦…

用 Java 实现拦截器 Interceptor 的拦截功能

Java 里的拦截器是动态拦截 action 调用的对象,它提供了一种机制可以使开发者可以定义在一个 action 执行的前后执行的代码,也可以在一个 action 执行前阻止其执行,同时也提供了一种可以提取 action 中可重用部分的方式。在 AOP(A…

java-拦截器

(1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如果是静态资源,会直接到Tomcat的项目部署目录下去直接访问 (4)如果是动态资源,就需要交给项目的后台代码进行处理…

JAVA中的拦截器、过滤器

JAVA变成拦截器、过滤器 一、拦截器1、简介说明2、源码及方法说明3、拦截器自定义应用 二、过滤器1、简介说明2、源码及方法说明3、过滤器的自定义应用 三、Springboot中的WebMvcConfigurer1、简介2、主要方法3、添加拦截器 四、区别1、原理2、触发3、其他 一、拦截器 1、简介…

Java开发学习----拦截器(Interceptor)详细解析

一、拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如果是静态资源,会直接到Tomcat的项目部署目录下去直接访…