当前位置:   article > 正文

Matlab计算器设计

matlab计算器

MATLAB的图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。

1.系统功能介绍

计算器实现的功能有:数字0~9和小数点的输入显示,平分、开平方和对数的输入显示。可进行加减乘除、正弦、余弦、正切、反正弦、反余弦、反正切的计算,可求阶乘,倒数,括号输入,二进制转十进制、十进制转二进制。还可以通过输入变量x和y,结合坐标轴编辑框和曲线颜色编辑框实现函数y=f(x)的曲线绘制。

2.部分设计思路

计算器设计时主要利用到get和set两个函数进行各个控件属性值的传递和设置,利用strcat函数实现把两个字符串连接起来。利用length函数来计算字符串的长度实现后退的功能。利用eval函数将文本框中的字符串转换成数值表达式,利用MATLAB计算出结果返回显示。利用坐标轴axes和ezplot函数进行图形的绘制,利用factorial函数进行求阶乘运算。利用str2num及num2str实现数值与字符之间的转换。
在这里插入图片描述

3.系统设计

程序由MATLAB代码(.m)文件和GUI图形(.fig)文件两部分组成。
(1)布局构建:
首先在设计界面中添加上所需功能按钮、文本框、画图用的坐标轴等。调整好各个控件布局、通过双击各个功能按钮调整其参数(字体大小,标签名称、背景颜色等)。
在这里插入图片描述
(2)设置控件参数:
各个控件调整完毕后,在m文件中找到与相应按钮对应的回调函数并编写回调函数,以实现各个按钮的功能。
在这里插入图片描述
(3)回调函数的编写:
i.数字(以7为例):
在这里插入图片描述
ii.运算符(以+为例):
在这里插入图片描述
iii.三角函数(以sin为例):
在这里插入图片描述
iv.二进制转十进制
在这里插入图片描述
v.绘图:
在这里插入图片描述
vi.删除键:
在这里插入图片描述
vii.清空键:
在这里插入图片描述
(4)功能展示:

注意:画图前应先设置按键最右侧一列的坐标范围。
  • 1

在这里插入图片描述

4.完整代码

运行时必须与搭建的GUI界面放在同一文件夹下,直接运行即可,本例的m文件和gui文件在上传的资源中。

function varargout = jisuanqi(varargin)
% JISUANQI MATLAB code for jisuanqi.fig
%      JISUANQI, by itself, creates a new JISUANQI or raises the existing
%      singleton*.
%
%      H = JISUANQI returns the handle to a new JISUANQI or the handle to
%      the existing singleton*.
%
%      JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in JISUANQI.M with the given input arguments.
%
%      JISUANQI('Property','Value',...) creates a new JISUANQI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before jisuanqi_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to jisuanqi_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help jisuanqi

% Last Modified by GUIDE v2.5 24-Dec-2019 09:58:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...
                   'gui_OutputFcn',  @jisuanqi_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before jisuanqi is made visible.
function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to jisuanqi (see VARARGIN)

% Choose default command line output for jisuanqi
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes jisuanqi wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = jisuanqi_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%平方
textString = get(handles.text2,'String');
textString = strcat(textString,'^2');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
%开平方
textString = get(handles.text2,'String');
textString = strcat(textString,'^(1/2)');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
%阶乘
textString = get(handles.text2,'String');
ans = factorial(str2num(textString));
textString = strcat(textString,'!');
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
%删除键
textString = get(handles.text2,'String');
set(handles.text2,'String',' ');
s = char(textString);
n = length(textString);
textString = s(1:n-1);
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
%清空键
set(handles.text2,'String',' ');
set(handles.text1,'String',' ');   %清空静态文本
axes(handles.axes1);   %清空图像
cla reset


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'7');
set(handles.text2,'String',textString)



% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'8');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'9');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'+');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'-');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'4');
set(handles.text2,'String',textString)



% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'5');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'6');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'*');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'/');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'1');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'2');
set(handles.text2,'String',textString)



% --- Executes on button press in pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'3');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton20.
function pushbutton20_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'log(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'exp(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'pi');
set(handles.text2,'String',textString)



% --- Executes on button press in pushbutton23.
function pushbutton23_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'0');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton24.
function pushbutton24_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'.');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,')');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'sin(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton28.
function pushbutton28_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'cos(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'tan(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton30.
function pushbutton30_Callback(hObject, eventdata, handles)
%2进制转10进制
textString = get(handles.text2,'String');
textString = bin2dec(textString);
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)
%十进制转二进制
textString = get(handles.text2,'String');
textString = dec2bin(str2num(textString));
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton32.
function pushbutton32_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'asin(');
set(handles.text2,'String',textString)



% --- Executes on button press in pushbutton33.
function pushbutton33_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'acos(');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'atan(');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton35.
function pushbutton35_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
textString = strcat(textString,'log10(');
set(handles.text2,'String',textString)

% --- Executes on button press in pushbutton36.
function pushbutton36_Callback(hObject, eventdata, handles)
%等号显示结果
textString = get(handles.text2,'String');
ans = eval(textString);
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)



% --- Executes on button press in pushbutton37.
function pushbutton37_Callback(hObject, eventdata, handles)
%关闭界面
close(gcf);


% --- Executes on button press in pushbutton43.
function pushbutton43_Callback(hObject, eventdata, handles)
%画图
fun = get(handles.text2,'String');%把text2中的字符串赋给gun
set(handles.text1,'String',fun);%显示在text1中
set(handles.text2,'String','');%清空text2
Xmin = get(handles.edit1,'String');
Xmax = get(handles.edit2,'String');
Ymin = get(handles.edit3,'String');
Ymax = get(handles.edit4,'String');
fun = ezplot(fun,[str2num(Xmin),str2num(Xmax),str2num(Ymin),str2num(Ymax)]);
color = get(handles.edit5,'String');  %颜色字符
set(fun,'Color',color);   %换颜色
grid on   %打开坐标轴网络线


function edit1_Callback(hObject, eventdata, handles)

function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit2_Callback(hObject, eventdata, handles)

function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)

function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)

function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit5_Callback(hObject, eventdata, handles)

function edit5_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton44.
function pushbutton44_Callback(hObject, eventdata, handles)
%绘图时所用的自变量x
textString = get(handles.text2,'String');
textString = strcat(textString,'x');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton45.
function pushbutton45_Callback(hObject, eventdata, handles)
%绘图时所用,y=f(x)
textString = get(handles.text2,'String');
textString = strcat(textString,'y');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton46.
function pushbutton46_Callback(hObject, eventdata, handles)
%等号   非计算结果   画图时使用
textString = get(handles.text2,'String');
textString = strcat(textString,'=');
set(handles.text2,'String',textString)


% --- Executes on button press in pushbutton47.
function pushbutton47_Callback(hObject, eventdata, handles)
textString = get(handles.text2,'String');
ans = 1/str2num(textString);
textString = strcat('1/',num2str(textString));
set(handles.text1,'String',textString);
set(handles.text2,'String',ans)


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号