MATLAB 使用App Designer创建应用程序

在MATLAB中使用App Designer创建应用程序

MATLAB是一种强大且高级的编程语言,广泛用于工程和科学领域的系统设计。关于MATLAB最重要的事实是它提供了一个易于使用的图形用户界面(GUI)。MATLAB内置了一个名为App Designer的工具,允许用户开发各种应用程序,将他们的概念无缝地转化为用户友好的GUI。总体而言,MATLAB是以简化和可访问的方式传达复杂信息的无价之宝。

在本教程中,我们将探讨如何使用MATLAB的App Designer创建GUI应用程序。

如何使用MATLAB App Designer创建应用程序

下面解释了使用MATLAB的App Designer创建应用程序的逐步过程:

第1步 - 打开MATLAB并启动App Designer。有两种方法可以做到这一点:

  • 在MATLAB命令窗口中写入’appdesigner’命令,然后按enter键。

  • 点击’APPS’选项卡,选择’App Designer’选项。

第2步 - 创建一个空白的应用程序。

第3步 - 使用按钮、文本字段、绘图等组件设计用户界面。

第4步 - 使用右侧显示的属性编辑器工具自定义每个组件的属性。

第5步 - 通过编写MATLAB代码向应用程序的组件添加功能。

第6步 - 通过点击App Designer选项卡上的’Run’按钮运行应用程序,并检查其工作情况。

第7步 - 如果应用程序的用户界面或功能存在任何错误,请使用MATLAB提供的调试工具对应用程序进行调试。

第8步 - 通过点击App Designer窗口上的’Save’按钮保存应用程序。在MATLAB中,我们可以将应用程序保存为’.mlapp’文件或独立的应用程序,可在不安装MATLAB的情况下运行。

这就是在MATLAB中设计GUI应用程序的步骤。

现在,为了更好地了解MATLAB App Designer中的应用程序设计,让我们设计一个简单的计算器应用程序,可以计算电路中的电阻、电功率和电能。

示例:在MATLAB中使用App Designer创建应用程序

设计一个计算器应用程序,用于计算电路中的电阻、电功率和电能,需要六个数值编辑字段,其中三个可编辑用于接受电压(V)、电流(I)和时间(t),而其他三个不可编辑,用于保存电阻(R)、电功率(P)和电能(E)的值。

它还将具有一个用于计算和显示结果的按钮。我们还可以为应用程序添加标签以增强外观。

因此,让我们开始开发这个应用程序。

第1步 - 打开MATLAB App Designer并创建一个黑色应用程序。

在MATLAB中使用App Designer创建应用程序

步骤(2) - 从左侧的组件库中拖放六个数值字段、六个文本编辑字段、一个标签和一个按钮到应用画布上。按照您希望在应用程序中显示的方式排列这些组件。

在MATLAB中使用App Designer创建应用程序

步骤(3) - 当点击“计算”按钮时,编写MATLAB代码来执行电阻、电力、电能的计算。

在MATLAB中使用App Designer创建应用程序

一旦您点击了突出显示的选项,您将获得一个空间来粘贴下面的代码。

% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
    % Get values from the editable fields
    V = app.VoltageEditField.Value;
    I = app.CurrentEditField.Value;
    t = app.TimeEditField.Value;

    % Calculate resistance, power, and energy
    R = V / I;
    P = V * I;
    E = P * t;

    % Update non-editable fields with calculated values
    app.ResistanceEditField.Value = R;
    app.ElectricPowerEditField.Value = P;
    app.ElectricalEnergyEditField.Value = E;
end

步骤(4) - 运行并测试应用功能。

在MATLAB中使用App Designer创建应用程序

应用的MATLAB代码

以下是上述应用程序的完整MATLAB代码。

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                        matlab.ui.Figure
        ElectricalCalculatorLabel       matlab.ui.control.Label
        CalculateButton                 matlab.ui.control.Button
        ElectricPowerEditField          matlab.ui.control.NumericEditField
        ElectricPowerEditFieldLabel     matlab.ui.control.Label
        ElectricalEnergyEditField       matlab.ui.control.NumericEditField
        ElectricalEnergyEditFieldLabel  matlab.ui.control.Label
        CurrentEditField                matlab.ui.control.NumericEditField
        CurrentEditFieldLabel           matlab.ui.control.Label
        TimeEditField                   matlab.ui.control.NumericEditField
        TimeEditFieldLabel              matlab.ui.control.Label
        ResistanceEditField             matlab.ui.control.NumericEditField
        ResistanceEditFieldLabel        matlab.ui.control.Label
        VoltageEditField                matlab.ui.control.NumericEditField
        VoltageEditFieldLabel           matlab.ui.control.Label
    end

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

        % Button pushed function: CalculateButton
        function CalculateButtonPushed(app, event)
            % Get values from the editable fields
            V = app.VoltageEditField.Value;
            I = app.CurrentEditField.Value;
            t = app.TimeEditField.Value;

            % Calculate resistance, power, and energy
            R = V / I;
            P = V * I;
            E = P * t;

            % Update non-editable fields with calculated values
            app.ResistanceEditField.Value = R;
            app.ElectricPowerEditField.Value = P;
            app.ElectricalEnergyEditField.Value = E;
        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 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create VoltageEditFieldLabel
            app.VoltageEditFieldLabel = uilabel(app.UIFigure);
            app.VoltageEditFieldLabel.HorizontalAlignment = 'right';
            app.VoltageEditFieldLabel.FontWeight = 'bold';
            app.VoltageEditFieldLabel.Position = [207 378 47 22];
            app.VoltageEditFieldLabel.Text = 'Voltage';

            % Create VoltageEditField
            app.VoltageEditField = uieditfield(app.UIFigure, 'numeric');
            app.VoltageEditField.Position = [269 378 100 22];

            % Create ResistanceEditFieldLabel
            app.ResistanceEditFieldLabel = uilabel(app.UIFigure);
            app.ResistanceEditFieldLabel.HorizontalAlignment = 'right';
            app.ResistanceEditFieldLabel.FontWeight = 'bold';
            app.ResistanceEditFieldLabel.Position = [186 219 68 22];
            app.ResistanceEditFieldLabel.Text = 'Resistance';

            % Create ResistanceEditField
            app.ResistanceEditField = uieditfield(app.UIFigure, 'numeric');
            app.ResistanceEditField.Editable = 'off';
            app.ResistanceEditField.Position = [269 219 100 22];

            % Create TimeEditFieldLabel
            app.TimeEditFieldLabel = uilabel(app.UIFigure);
            app.TimeEditFieldLabel.HorizontalAlignment = 'right';
            app.TimeEditFieldLabel.FontWeight = 'bold';
            app.TimeEditFieldLabel.Position = [221 300 33 22];
            app.TimeEditFieldLabel.Text = 'Time';

            % Create TimeEditField
            app.TimeEditField = uieditfield(app.UIFigure, 'numeric');
            app.TimeEditField.Position = [269 300 100 22];

            % Create CurrentEditFieldLabel
            app.CurrentEditFieldLabel = uilabel(app.UIFigure);
            app.CurrentEditFieldLabel.HorizontalAlignment = 'right';
            app.CurrentEditFieldLabel.FontWeight = 'bold';
            app.CurrentEditFieldLabel.Position = [206 338 48 22];
            app.CurrentEditFieldLabel.Text = 'Current';

            % Create CurrentEditField
            app.CurrentEditField = uieditfield(app.UIFigure, 'numeric');
            app.CurrentEditField.Position = [269 338 100 22];

            % Create ElectricalEnergyEditFieldLabel
            app.ElectricalEnergyEditFieldLabel = uilabel(app.UIFigure);
            app.ElectricalEnergyEditFieldLabel.HorizontalAlignment = 'right';
            app.ElectricalEnergyEditFieldLabel.FontWeight = 'bold';
            app.ElectricalEnergyEditFieldLabel.Position = [152 156 102 22];
            app.ElectricalEnergyEditFieldLabel.Text = 'Electrical Energy';

            % Create ElectricalEnergyEditField
            app.ElectricalEnergyEditField = uieditfield(app.UIFigure, 'numeric');
            app.ElectricalEnergyEditField.Editable = 'off';
            app.ElectricalEnergyEditField.Position = [269 156 100 22];

            % Create ElectricPowerEditFieldLabel
            app.ElectricPowerEditFieldLabel = uilabel(app.UIFigure);
            app.ElectricPowerEditFieldLabel.HorizontalAlignment = 'right';
            app.ElectricPowerEditFieldLabel.FontWeight = 'bold';
            app.ElectricPowerEditFieldLabel.Position = [166 187 88 22];
            app.ElectricPowerEditFieldLabel.Text = 'Electric Power';

            % Create ElectricPowerEditField
            app.ElectricPowerEditField = uieditfield(app.UIFigure, 'numeric');
            app.ElectricPowerEditField.Position = [269 187 100 22];

            % Create CalculateButton
            app.CalculateButton = uibutton(app.UIFigure, 'push');
            app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
            app.CalculateButton.FontWeight = 'bold';
            app.CalculateButton.Position = [269 263 100 22];
            app.CalculateButton.Text = 'Calculate';

            % Create ElectricalCalculatorLabel
            app.ElectricalCalculatorLabel = uilabel(app.UIFigure);
            app.ElectricalCalculatorLabel.HorizontalAlignment = 'center';
            app.ElectricalCalculatorLabel.FontSize = 18;
            app.ElectricalCalculatorLabel.FontWeight = 'bold';
            app.ElectricalCalculatorLabel.Position = [199 416 186 23];
            app.ElectricalCalculatorLabel.Text = 'Electrical Calculator';

            % 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)

            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应用程序设计者创建任何类型的GUI应用程序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程