计算器MRmatlab回调函数
时间: 2023-06-28 10:06:14 浏览: 72
回调函数
在Matlab中使用回调函数可以使得用户与GUI界面之间的交互更加方便。下面是一个简单的计算器GUI界面,以及相关的回调函数代码示例:
```matlab
% 创建计算器GUI界面
f = figure('Name', 'Calculator', 'NumberTitle', 'off', 'Position', [200, 200, 300, 300]);
% 添加文本框用于显示计算结果
result_text = uicontrol('Style', 'text', 'String', '0', 'HorizontalAlignment', 'right', 'Position', [10, 250, 280, 40], 'Fontsize', 20);
% 添加数字按钮和运算符按钮
btn_1 = uicontrol('Style', 'pushbutton', 'String', '1', 'Position', [10, 190, 60, 50], 'Fontsize', 20, 'Callback', @btn_1_Callback);
btn_2 = uicontrol('Style', 'pushbutton', 'String', '2', 'Position', [80, 190, 60, 50], 'Fontsize', 20, 'Callback', @btn_2_Callback);
btn_3 = uicontrol('Style', 'pushbutton', 'String', '3', 'Position', [150, 190, 60, 50], 'Fontsize', 20, 'Callback', @btn_3_Callback);
btn_4 = uicontrol('Style', 'pushbutton', 'String', '4', 'Position', [10, 130, 60, 50], 'Fontsize', 20, 'Callback', @btn_4_Callback);
btn_5 = uicontrol('Style', 'pushbutton', 'String', '5', 'Position', [80, 130, 60, 50], 'Fontsize', 20, 'Callback', @btn_5_Callback);
btn_6 = uicontrol('Style', 'pushbutton', 'String', '6', 'Position', [150, 130, 60, 50], 'Fontsize', 20, 'Callback', @btn_6_Callback);
btn_7 = uicontrol('Style', 'pushbutton', 'String', '7', 'Position', [10, 70, 60, 50], 'Fontsize', 20, 'Callback', @btn_7_Callback);
btn_8 = uicontrol('Style', 'pushbutton', 'String', '8', 'Position', [80, 70, 60, 50], 'Fontsize', 20, 'Callback', @btn_8_Callback);
btn_9 = uicontrol('Style', 'pushbutton', 'String', '9', 'Position', [150, 70, 60, 50], 'Fontsize', 20, 'Callback', @btn_9_Callback);
btn_0 = uicontrol('Style', 'pushbutton', 'String', '0', 'Position', [80, 10, 60, 50], 'Fontsize', 20, 'Callback', @btn_0_Callback);
btn_add = uicontrol('Style', 'pushbutton', 'String', '+', 'Position', [220, 190, 60, 50], 'Fontsize', 20, 'Callback', @btn_add_Callback);
btn_sub = uicontrol('Style', 'pushbutton', 'String', '-', 'Position', [220, 130, 60, 50], 'Fontsize', 20, 'Callback', @btn_sub_Callback);
btn_mul = uicontrol('Style', 'pushbutton', 'String', '*', 'Position', [220, 70, 60, 50], 'Fontsize', 20, 'Callback', @btn_mul_Callback);
btn_div = uicontrol('Style', 'pushbutton', 'String', '/', 'Position', [220, 10, 60, 50], 'Fontsize', 20, 'Callback', @btn_div_Callback);
btn_eq = uicontrol('Style', 'pushbutton', 'String', '=', 'Position', [150, 10, 60, 50], 'Fontsize', 20, 'Callback', @btn_eq_Callback);
btn_clear = uicontrol('Style', 'pushbutton', 'String', 'C', 'Position', [10, 10, 60, 50], 'Fontsize', 20, 'Callback', @btn_clear_Callback);
% 初始化计算器状态
num_str = '0';
num1 = [];
num2 = [];
operator = [];
% 数字按钮的回调函数
function btn_num_Callback(hObject, eventdata)
global num_str;
num_str = strcat(num_str, get(hObject, 'String'));
update_result(num_str);
end
% 数字按钮1-9的回调函数
function btn_1_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_2_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_3_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_4_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_5_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_6_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_7_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_8_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_9_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
function btn_0_Callback(hObject, eventdata)
btn_num_Callback(hObject, eventdata);
end
% 运算符按钮的回调函数
function btn_operator_Callback(hObject, eventdata)
global num_str num1 num2 operator;
num1 = str2double(num_str);
operator = get(hObject, 'String');
num_str = '0';
end
% 加法按钮的回调函数
function btn_add_Callback(hObject, eventdata)
btn_operator_Callback(hObject, eventdata);
end
% 减法按钮的回调函数
function btn_sub_Callback(hObject, eventdata)
btn_operator_Callback(hObject, eventdata);
end
% 乘法按钮的回调函数
function btn_mul_Callback(hObject, eventdata)
btn_operator_Callback(hObject, eventdata);
end
% 除法按钮的回调函数
function btn_div_Callback(hObject, eventdata)
btn_operator_Callback(hObject, eventdata);
end
% 等于号按钮的回调函数
function btn_eq_Callback(hObject, eventdata)
global num_str num1 num2 operator;
num2 = str2double(num_str);
switch operator
case '+'
result = num1 + num2;
case '-'
result = num1 - num2;
case '*'
result = num1 * num2;
case '/'
result = num1 / num2;
end
num_str = num2str(result);
update_result(num_str);
end
% 清除按钮的回调函数
function btn_clear_Callback(hObject, eventdata)
global num_str num1 num2 operator;
num_str = '0';
num1 = [];
num2 = [];
operator = [];
update_result(num_str);
end
% 更新计算结果文本框的函数
function update_result(str)
global result_text;
set(result_text, 'String', str);
end
```
这个计算器GUI界面中包含了数字按钮和运算符按钮,当用户点击这些按钮时,对应的回调函数将会被调用。在回调函数中,我们可以根据用户操作来更新计算器的状态,并且通过更新文本框来显示计算结果。
阅读全文