如何在 Matlab App 中显示 matlab table?

How to display a matlab table in a Matlab App?

我正在构建一个应用程序,用户可以在其中 select 一些参数并按下按钮 "update",这会触发创建 table。假设一个名为 A.

的 table

现在我想在我的应用程序上以 excel 形式显示此 table,例如 window,以便用户可以看到数据更新的结果。我找不到哪个元素以及如何设置它,以便我的 table A 在我的应用程序中显示在 excel 中,例如 window,用户可以在其中上下、左右滚动对。

这有可能吗?如果可以,怎么做?

您可以使用 table 组件。

我的示例基于以下 example, which displays MATLAB table in a uitable(用户界面 table 组件)。

  • 您可以先在 App Designer 设计视图中向应用程序主图添加一个 table。
    您还可以在设计视图中添加更新按钮。
  • 在应用程序class中添加一个私有属性用于存储table数据(我将其命名为T):

    properties (Access = private)
        T % Table
    end
    
  • 您可以在 startupFcn 中初始化 table T,如下例所示:

    % Code that executes after component creation
    function startupFcn(app)
        LastName = {'Smith'; 'Johnson'; 'Williams'; 'Jones'; 'Brown'};
        Age = [38; 43; 38; 40; 49];
        Height = [71; 69; 64; 67; 64];
        Weight = [176; 163; 131; 133; 119];
        app.T = table(Age, Height, Weight, 'RowNames', LastName);
    end
    
  • 在按下按钮的回调中,您可以更新 table,如下例所示:

    % Button pushed function: UpdateButton
    function UpdateButtonPushed(app, event)
        app.UITable.Data = app.T{:,:};
        app.UITable.ColumnName = app.T.Properties.VariableNames;
        app.UITable.RowName = app.T.Properties.RowNames;
    end
    

这是用户界面的样子(按下更新按钮后):


完整代码如下(包括自动生成的代码):

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure      matlab.ui.Figure
        UITable       matlab.ui.control.Table
        UpdateButton  matlab.ui.control.Button
    end


    properties (Access = public)
        children = app1.empty % Description
    end

    properties (Access = private)
        T % Table
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            LastName = {'Smith'; 'Johnson'; 'Williams'; 'Jones'; 'Brown'};
            Age = [38; 43; 38; 40; 49];
            Height = [71; 69; 64; 67; 64];
            Weight = [176; 163; 131; 133; 119];
            app.T = table(Age, Height, Weight, 'RowNames', LastName);
        end

        % Button pushed function: UpdateButton
        function UpdateButtonPushed(app, event)
            app.UITable.Data = app.T{:,:};
            app.UITable.ColumnName = app.T.Properties.VariableNames;
            app.UITable.RowName = app.T.Properties.RowNames;
        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 322 233];
            app.UIFigure.Name = 'UI Figure';

            % Create UITable
            app.UITable = uitable(app.UIFigure);
            app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
            app.UITable.RowName = {};
            app.UITable.Position = [36 57 251 163];

            % Create UpdateButton
            app.UpdateButton = uibutton(app.UIFigure, 'push');
            app.UpdateButton.ButtonPushedFcn = createCallbackFcn(app, @UpdateButtonPushed, true);
            app.UpdateButton.Position = [36 14 100 22];
            app.UpdateButton.Text = 'Update';

            % 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

您可以将代码复制并粘贴到 app1.m 文件中,看看它是如何工作的(不使用 App Designer)。

其实我已经找到了一个满意的答案,它建立在上面Rotem的答案之上:

在按钮按下的回调中,只需添加:

% Button pushed function: UpdateButton
function UpdateButtonPushed(app, event)
    app.UITable.Data = app.T;
    app.UITable.ColumnName = app.T.Properties.VariableNames;
end

这适用于多种数据类型。 (我实际上没有显示 rowName 属性,因为在这种情况下我没有)。