MATLAB App Designer 计算器的设计

作者初涉App设计,看到有前人写了guide版的计算器,所以萌生想法,想用App Designer来设计一个计算器。

有纰漏请指出,转载请说明。

学习交流请发邮件 [email protected]

一、效果展示

开启时显示当前时间

点击“历史”按钮时可显示历史输入值

MATLAB App Designer 计算器的设计_第1张图片

MATLAB App Designer 计算器的设计_第2张图片

MATLAB App Designer 计算器的设计_第3张图片

二、核心代码

创建全局变量

properties (Access = private)
        str % the string of the expression
        ans % the answer
        his=''
end

启动时显示当前时间

function startupFcn(app)
            formatOut = 'HH:MM:SS';
            app.monitor.Text=datestr(datetime("now"),formatOut);
end

 把按键相应的字符添加到str中,+-*/ 同理

function btn1Pushed(app, event)
            app.str=strcat(app.str,'1');
            app.monitor.Text=app.str;
end

 “=”按钮,将str添加到history中,判断str是否为空,不为空,则进行计算

function btnEquButtonPushed(app, event)
            app.his = append(app.his,' ',app.str)
            if length(app.str) ~= 0
                app.ans=eval(app.str);
                app.ans=num2str(app.ans)
                app.monitor.Text=app.ans;
                app.str=app.ans;
            end           
end

清空按钮

function clearButtonPushed(app, event)
            app.str='';
            app.his='';
            app.monitor.Text=app.str;
end

 退出

function exitButtonPushed(app, event)
            delete(app.UIFigure);
end

历史输入

function historyButtonPushed(app, event)
            app.monitor.Text=app.his;
end

三、完整代码 

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure   matlab.ui.Figure
        Toolbar    matlab.ui.container.Toolbar
        PushTool   matlab.ui.container.toolbar.PushTool
        history    matlab.ui.control.Button
        exit       matlab.ui.control.Button
        clear      matlab.ui.control.Button
        backspace  matlab.ui.control.Button
        btnDiv     matlab.ui.control.Button
        btnMul     matlab.ui.control.Button
        btnSub     matlab.ui.control.Button
        btnPlus    matlab.ui.control.Button
        btnEqu     matlab.ui.control.Button
        point      matlab.ui.control.Button
        btn0       matlab.ui.control.Button
        btn3       matlab.ui.control.Button
        btn2       matlab.ui.control.Button
        btn1       matlab.ui.control.Button
        btn6       matlab.ui.control.Button
        btn5       matlab.ui.control.Button
        btn4       matlab.ui.control.Button
        btn9       matlab.ui.control.Button
        btn8       matlab.ui.control.Button
        btn7       matlab.ui.control.Button
        monitor    matlab.ui.control.Label
    end

    
    properties (Access = private)
        str % the string of the expression
        ans % the answer
        his=''
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            formatOut = 'HH:MM:SS';
            app.monitor.Text=datestr(datetime("now"),formatOut);
        end

        % Callback function: UIFigure, btn1
        function btn1Pushed(app, event)
            app.str=strcat(app.str,'1');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn2
        function btn2ButtonPushed(app, event)
            app.str=strcat(app.str,'2');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btnEqu
        function btnEquButtonPushed(app, event)
            app.his = append(app.his,' ',app.str)
            if length(app.str) ~= 0
                app.ans=eval(app.str);
                app.ans=num2str(app.ans)
                app.monitor.Text=app.ans;
                app.str=app.ans;
            end
            
        end

        % Button pushed function: btnPlus
        function btnPlusButtonPushed(app, event)
            app.str=strcat(app.str,'+');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn3
        function btn3ButtonPushed(app, event)
            app.str=strcat(app.str,'3');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn4
        function btn4ButtonPushed(app, event)
            app.str=strcat(app.str,'4');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn5
        function btn5ButtonPushed(app, event)
            app.str=strcat(app.str,'5');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn6
        function btn6ButtonPushed(app, event)
            app.str=strcat(app.str,'6');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn7
        function btn7ButtonPushed(app, event)
            app.str=strcat(app.str,'7');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn8
        function btn8ButtonPushed(app, event)
            app.str=strcat(app.str,'8');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn9
        function btn9ButtonPushed(app, event)
            app.str=strcat(app.str,'9');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btn0
        function btn0ButtonPushed(app, event)
            app.str=strcat(app.str,'0');
            app.monitor.Text=app.str;
        end

        % Button pushed function: point
        function pointButtonPushed(app, event)
            app.str=strcat(app.str,'.');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btnSub
        function btnSubButtonPushed(app, event)
            app.str=strcat(app.str,'-');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btnMul
        function btnMulButtonPushed(app, event)
            app.str=strcat(app.str,'*');
            app.monitor.Text=app.str;
        end

        % Button pushed function: btnDiv
        function btnDivButtonPushed(app, event)
            app.str=strcat(app.str,'/');
            app.monitor.Text=app.str;
        end

        % Button pushed function: backspace
        function backspaceButtonPushed(app, event)
            L=length(app.str);
            app.str(L)='';
            app.monitor.Text=app.str;
        end

        % Button pushed function: clear
        function clearButtonPushed(app, event)
            app.str='';
            app.his='';
            app.monitor.Text=app.str;
        end

        % Callback function
        function remButtonPushed(app, event)

        end

        % Button pushed function: exit
        function exitButtonPushed(app, event)
            delete(app.UIFigure);
        end

        % Button pushed function: history
        function historyButtonPushed(app, event)
            app.monitor.Text=app.his;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 429 508];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.ButtonDownFcn = createCallbackFcn(app, @btn1Pushed, true);

            % Create Toolbar
            app.Toolbar = uitoolbar(app.UIFigure);

            % Create PushTool
            app.PushTool = uipushtool(app.Toolbar);

            % Create monitor
            app.monitor = uilabel(app.UIFigure);
            app.monitor.Tag = 'monitor';
            app.monitor.BackgroundColor = [0.502 0.502 0.502];
            app.monitor.HorizontalAlignment = 'right';
            app.monitor.WordWrap = 'on';
            app.monitor.FontSize = 35;
            app.monitor.Position = [54 387 290 122];
            app.monitor.Text = '';

            % Create btn7
            app.btn7 = uibutton(app.UIFigure, 'push');
            app.btn7.ButtonPushedFcn = createCallbackFcn(app, @btn7ButtonPushed, true);
            app.btn7.Tag = 'btn7';
            app.btn7.BackgroundColor = [0.902 0.902 0.902];
            app.btn7.FontSize = 25;
            app.btn7.FontWeight = 'bold';
            app.btn7.Position = [55 300 50 50];
            app.btn7.Text = '7';

            % Create btn8
            app.btn8 = uibutton(app.UIFigure, 'push');
            app.btn8.ButtonPushedFcn = createCallbackFcn(app, @btn8ButtonPushed, true);
            app.btn8.Tag = 'btn8';
            app.btn8.BackgroundColor = [0.902 0.902 0.902];
            app.btn8.FontSize = 25;
            app.btn8.FontWeight = 'bold';
            app.btn8.Position = [135 300 50 50];
            app.btn8.Text = '8';

            % Create btn9
            app.btn9 = uibutton(app.UIFigure, 'push');
            app.btn9.ButtonPushedFcn = createCallbackFcn(app, @btn9ButtonPushed, true);
            app.btn9.Tag = 'btn9';
            app.btn9.BackgroundColor = [0.902 0.902 0.902];
            app.btn9.FontSize = 25;
            app.btn9.FontWeight = 'bold';
            app.btn9.Position = [215 300 50 50];
            app.btn9.Text = '9';

            % Create btn4
            app.btn4 = uibutton(app.UIFigure, 'push');
            app.btn4.ButtonPushedFcn = createCallbackFcn(app, @btn4ButtonPushed, true);
            app.btn4.Tag = 'btn4';
            app.btn4.BackgroundColor = [0.902 0.902 0.902];
            app.btn4.FontSize = 25;
            app.btn4.FontWeight = 'bold';
            app.btn4.Position = [55 230 50 50];
            app.btn4.Text = '4';

            % Create btn5
            app.btn5 = uibutton(app.UIFigure, 'push');
            app.btn5.ButtonPushedFcn = createCallbackFcn(app, @btn5ButtonPushed, true);
            app.btn5.Tag = 'btn5';
            app.btn5.BackgroundColor = [0.902 0.902 0.902];
            app.btn5.FontSize = 25;
            app.btn5.FontWeight = 'bold';
            app.btn5.Position = [135 230 50 50];
            app.btn5.Text = '5';

            % Create btn6
            app.btn6 = uibutton(app.UIFigure, 'push');
            app.btn6.ButtonPushedFcn = createCallbackFcn(app, @btn6ButtonPushed, true);
            app.btn6.Tag = 'btn6';
            app.btn6.BackgroundColor = [0.902 0.902 0.902];
            app.btn6.FontSize = 25;
            app.btn6.FontWeight = 'bold';
            app.btn6.Position = [215 230 50 50];
            app.btn6.Text = '6';

            % Create btn1
            app.btn1 = uibutton(app.UIFigure, 'push');
            app.btn1.ButtonPushedFcn = createCallbackFcn(app, @btn1Pushed, true);
            app.btn1.Tag = 'btn1';
            app.btn1.BackgroundColor = [0.902 0.902 0.902];
            app.btn1.FontSize = 25;
            app.btn1.FontWeight = 'bold';
            app.btn1.Position = [55 160 50 50];
            app.btn1.Text = '1';

            % Create btn2
            app.btn2 = uibutton(app.UIFigure, 'push');
            app.btn2.ButtonPushedFcn = createCallbackFcn(app, @btn2ButtonPushed, true);
            app.btn2.Tag = 'btn2';
            app.btn2.BackgroundColor = [0.902 0.902 0.902];
            app.btn2.FontSize = 25;
            app.btn2.FontWeight = 'bold';
            app.btn2.Position = [135 160 50 50];
            app.btn2.Text = '2';

            % Create btn3
            app.btn3 = uibutton(app.UIFigure, 'push');
            app.btn3.ButtonPushedFcn = createCallbackFcn(app, @btn3ButtonPushed, true);
            app.btn3.Tag = 'btn3';
            app.btn3.BackgroundColor = [0.902 0.902 0.902];
            app.btn3.FontSize = 25;
            app.btn3.FontWeight = 'bold';
            app.btn3.Position = [215 160 50 50];
            app.btn3.Text = '3';

            % Create btn0
            app.btn0 = uibutton(app.UIFigure, 'push');
            app.btn0.ButtonPushedFcn = createCallbackFcn(app, @btn0ButtonPushed, true);
            app.btn0.Tag = 'btn0';
            app.btn0.FontSize = 25;
            app.btn0.FontWeight = 'bold';
            app.btn0.Position = [55 90 50 50];
            app.btn0.Text = '0';

            % Create point
            app.point = uibutton(app.UIFigure, 'push');
            app.point.ButtonPushedFcn = createCallbackFcn(app, @pointButtonPushed, true);
            app.point.Tag = 'btnClear';
            app.point.FontSize = 25;
            app.point.FontWeight = 'bold';
            app.point.Position = [135 90 50 50];
            app.point.Text = '.';

            % Create btnEqu
            app.btnEqu = uibutton(app.UIFigure, 'push');
            app.btnEqu.ButtonPushedFcn = createCallbackFcn(app, @btnEquButtonPushed, true);
            app.btnEqu.Tag = 'btnEqu';
            app.btnEqu.FontSize = 25;
            app.btnEqu.FontWeight = 'bold';
            app.btnEqu.Position = [215 90 50 50];
            app.btnEqu.Text = '=';

            % Create btnPlus
            app.btnPlus = uibutton(app.UIFigure, 'push');
            app.btnPlus.ButtonPushedFcn = createCallbackFcn(app, @btnPlusButtonPushed, true);
            app.btnPlus.Tag = 'btnPlus';
            app.btnPlus.FontSize = 25;
            app.btnPlus.FontWeight = 'bold';
            app.btnPlus.Position = [295 300 50 50];
            app.btnPlus.Text = '+';

            % Create btnSub
            app.btnSub = uibutton(app.UIFigure, 'push');
            app.btnSub.ButtonPushedFcn = createCallbackFcn(app, @btnSubButtonPushed, true);
            app.btnSub.Tag = 'btnSub';
            app.btnSub.FontSize = 25;
            app.btnSub.FontWeight = 'bold';
            app.btnSub.Position = [295 230 50 50];
            app.btnSub.Text = '-';

            % Create btnMul
            app.btnMul = uibutton(app.UIFigure, 'push');
            app.btnMul.ButtonPushedFcn = createCallbackFcn(app, @btnMulButtonPushed, true);
            app.btnMul.Tag = 'btnMul';
            app.btnMul.FontSize = 25;
            app.btnMul.FontWeight = 'bold';
            app.btnMul.Position = [295 160 50 50];
            app.btnMul.Text = '*';

            % Create btnDiv
            app.btnDiv = uibutton(app.UIFigure, 'push');
            app.btnDiv.ButtonPushedFcn = createCallbackFcn(app, @btnDivButtonPushed, true);
            app.btnDiv.Tag = 'btnDiv';
            app.btnDiv.FontSize = 25;
            app.btnDiv.FontWeight = 'bold';
            app.btnDiv.Position = [295 90 50 50];
            app.btnDiv.Text = '/';

            % Create backspace
            app.backspace = uibutton(app.UIFigure, 'push');
            app.backspace.ButtonPushedFcn = createCallbackFcn(app, @backspaceButtonPushed, true);
            app.backspace.Tag = 'btn0';
            app.backspace.FontSize = 25;
            app.backspace.FontWeight = 'bold';
            app.backspace.Position = [215 19 51 50];
            app.backspace.Text = '←';

            % Create clear
            app.clear = uibutton(app.UIFigure, 'push');
            app.clear.ButtonPushedFcn = createCallbackFcn(app, @clearButtonPushed, true);
            app.clear.Tag = 'btnClear';
            app.clear.FontSize = 25;
            app.clear.FontWeight = 'bold';
            app.clear.Position = [295 19 50 50];
            app.clear.Text = 'C';

            % Create exit
            app.exit = uibutton(app.UIFigure, 'push');
            app.exit.ButtonPushedFcn = createCallbackFcn(app, @exitButtonPushed, true);
            app.exit.Tag = 'btnDiv';
            app.exit.BackgroundColor = [0.502 0.502 0.502];
            app.exit.FontSize = 20;
            app.exit.FontWeight = 'bold';
            app.exit.Position = [56 19 49 50];
            app.exit.Text = 'exit';

            % Create history
            app.history = uibutton(app.UIFigure, 'push');
            app.history.ButtonPushedFcn = createCallbackFcn(app, @historyButtonPushed, true);
            app.history.Tag = 'btnClear';
            app.history.FontSize = 20;
            app.history.FontWeight = 'bold';
            app.history.Position = [135 19 50 50];
            app.history.Text = '历史';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = app1

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

 

 点个赞,加个关注再走呗!

蟹蟹各位观众姥爷!

你可能感兴趣的:(MATLAB,matlab)