To create a popup four function calculator in MATLAB, we can use the MATLAB GUI (Graphical User Interface) which allows us to create interactive applications with buttons, text boxes, and other interactive elements.
function popup_calculator
% Create figure and UI elements
fig = uifigure('Position', [100 100 250 300]);
num_box = uitextarea(fig, 'Value', '0', 'Position', [25 250 200 30]);
button_1 = uibutton(fig, 'push', 'Position', [25 200 50 50], 'Text', '1', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_2 = uibutton(fig, 'push', 'Position', [100 200 50 50], 'Text', '2', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_3 = uibutton(fig, 'push', 'Position', [175 200 50 50], 'Text', '3', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_4 = uibutton(fig, 'push', 'Position', [25 150 50 50], 'Text', '4', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_5 = uibutton(fig, 'push', 'Position', [100 150 50 50], 'Text', '5', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_6 = uibutton(fig, 'push', 'Position', [175 150 50 50], 'Text', '6', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_7 = uibutton(fig, 'push', 'Position', [25 100 50 50], 'Text', '7', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_8 = uibutton(fig, 'push', 'Position', [100 100 50 50], 'Text', '8', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_9 = uibutton(fig, 'push', 'Position', [175 100 50 50], 'Text', '9', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_0 = uibutton(fig, 'push', 'Position', [100 50 50 50], 'Text', '0', 'ButtonPushedFcn', @(btn,event) num_button(btn, event, num_box));
button_add = uibutton(fig, 'push', 'Position', [25 50 50 50], 'Text', '+', 'ButtonPushedFcn', @(btn,event) add_button(btn, event, num_box));
button_sub = uibutton(fig, 'push', 'Position', [175 50 50 50], 'Text', '-', 'ButtonPushedFcn', @(btn,event) sub_button(btn, event, num_box));
button_mul = uibutton(fig, 'push', 'Position', [25 0 50 50], 'Text', '*', 'ButtonPushedFcn', @(btn,event) mul_button(btn, event, num_box));
button_div = uibutton(fig, 'push', 'Position', [175 0 50 50], 'Text', '/', 'ButtonPushedFcn', @(btn,event) div_button(btn, event, num_box));
button_equal = uibutton(fig, 'push', 'Position', [100 0 50 50], 'Text', '=', 'ButtonPushedFcn', @(btn,event) equal_button(btn, event, num_box));
% Callback functions for buttons
function num_button(~, ~, num_box)
num_box.Value = strcat(num_box.Value, get(gco, 'Text'));
end
function add_button(~, ~, num_box)
num_box.Value = strcat(num_box.Value, '+');
end
function sub_button(~, ~, num_box)
num_box.Value = strcat(num_box.Value, '-');
end
function mul_button(~, ~, num_box)
num_box.Value = strcat(num_box.Value, '*');
end
function div_button(~, ~, num_box)
num_box.Value = strcat(num_box.Value, '/');
end
function equal_button(~, ~, num_box)
num_box.Value = num2str(eval(num_box.Value));
end
end