繁体   English   中英

如何在 Matlab App 中显示 matlab 表格?

[英]How to display a matlab table in a Matlab App?

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

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

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

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

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

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

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

您可以使用表格组件。

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

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

     properties (Access = private) T % Table end
  • 您可以在startupFcn初始化表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
  • 在按钮推送回调中,您可以像以下示例一样更新表:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM