Matlab制作计算器
程序员文章站
2024-02-27 12:45:03
...
写文章之前先说明一下, 本人用的是 MATLAB R2016b 版本
1 首先新建一个GUIDE ,如下图所示:
2 选择默认的空白界面 Blank GUI (Default),点确认。
3 拖动一个按钮控件作为输入按键,双击按钮,对按钮属性进行设置(本处只对一个按钮进行设置,其它按钮设置方法一样),如下图:把 静态文本 作为显示控件 ,其属性设置与输入按钮方法一样介绍了。
4 布置好的界面如下图:
5 最后是代码部分:
% --- Executes on button press in btn1.
function btn1_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '1');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn2.
function btn2_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '2');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn3.
function btn3_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '3');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in add.
function add_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '+');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn4.
function btn4_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '4');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn5.
function btn5_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '5');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn6.
function btn6_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '6');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in substract.
function substract_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '-');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn7.
function btn7_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '7');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn8.
function btn8_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '8');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn9.
function btn9_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '9');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in multiply.
function multiply_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '*');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in btn0.
function btn0_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '0');
set(handles.txtresult, 'string', temp);
% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
set(handles.txtresult, 'string', '');
% --- Executes on button press in equal.
function equal_Callback(hObject, eventdata, handles)
StrResult = get(handles.txtresult, 'string');
Result = eval(StrResult); % eval 函数的作用:eval('2+3') == 5 eval('6-4/2') == 4
set(handles.txtresult, 'string',Result);
% --- Executes on button press in divide.
function divide_Callback(hObject, eventdata, handles)
temp = get(handles.txtresult, 'string');
temp = strcat(temp, '/');
set(handles.txtresult, 'string', temp);
6
上一篇: Hadoop 相关知识点(一)