首先应设计好计算器的GUI界面 如下图所示,然后分别设置‘数据1’、‘数据2’、‘计算结果’的tag值为num1、num2和shuchu 当然你要是愿意也可以定义加减乘除按钮的tag属性,tag属性值也就是你的控件名字,给它取了名字就可以赋予它使命,要它执行命令了。
接着为加减乘除按钮编写回调函数:
加法按钮回调函数:
% --- Executes on button press in plus.
function plus_Callback(hObject, eventdata, handles)
% hObject handle to plus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
shuchu = handles.num1+handles.num2;
set(handles.shuchu, 'String', shuchu);减法按钮回调函数:
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7