1

我正在构建一个应用程序,用户可以在其中选择一些参数并按下“更新”按钮,这会触发要创建的表。假设一个名为 A 的表。

现在我想在我的应用程序的类似 excel 的窗口中显示此表,以便用户可以看到数据更新的结果。我找不到哪个元素以及如何设置它,以便我的表 A 在我的应用程序中显示在一个类似 excel 的窗口中,用户可以在该窗口中向上和向下、向左和向右滚动。

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

4

2 回答 2

1

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

在按钮推送回调中,只需添加:

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

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

于 2020-01-14T14:56:34.003 回答
0

您可以使用表格组件。

我的示例基于以下示例,该示例在uitable(用户界面表格组件) 中显示 MATLAB 表格。

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

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

    % 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
    

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

在此处输入图像描述


这是完整的代码(包括自动生成的代码):

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 设计器)。

于 2020-01-13T18:13:37.107 回答