make a popup four function calculator in matlab

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.

We will create a function popup_calculator that will create a popup window with the calculator. The calculator will have buttons for addition, subtraction, multiplication, and division, as well as buttons for inputting numbers and displaying the result.

main.m
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
3024 chars
46 lines

The function creates a pop-up window with a uitextarea where the user can input numbers and see the result of the calculations. The function also creates a series of uibuttons that allow the user to input numbers and mathematical symbols. The ButtonPushedFcn callback functions for these buttons allow us to define what happens when the user clicks on each button. For example, when the user clicks on the multiplication button (button_mul), the mul_button function is called, which adds a * character to the text in the uitextarea.

To test the function, simply call popup_calculator in the MATLAB console. This will create the popup window with the calculator.

gistlibby LogSnag