【问题标题】:How to display a matlab table in a Matlab App?如何在 Matlab App 中显示 matlab 表格?
【发布时间】:2020-04-30 09:34:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: matlab matlab-table matlab-app-designer


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      您可以使用table 组件。

      我的示例基于以下example,它在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 设计工具)。

      【讨论】:

      • 感谢Rotem。这本来可以正常工作,但我的表不仅包含双精度类型,还包含单元格、日期时间和逻辑数据。因此,我收到以下错误:使用 Screening_Tool/UpdateButton_3Pushed 时出错(第 52 行)无法连接表变量“AS_OF_DATE_CONSTITUENTDATA”和“TICKER”,因为它们的类型是日期时间和单元格。使用 matlab.ui.control.internal.controller.ComponentController/executeUserCallback 时出错(第 335 行)评估 Button PrivateButtonPushedFcn 时出错。
      • 没有自动解决方案,你必须通过代码来解决它!如果表格组件不适合,您可以使用网格布局(并将表格/标签/文本区域/列表框...放置在网格的每个单元格内)。
      • 我找到了一种方法,我将其发布为答案。
      • 您所要做的就是将app.UITable.Data = app.T{:,:} 替换为app.UITable.Data = app.T。不错...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多