在 App 设计工具中右对齐 UITable 中的值

Right-align values in UITable in App Designer

我已经在 Matlab Answers 上询问过这个问题,但没有得到回复,所以我在这里试试。

我有一些数字要显示在 UITable 中。由于我需要值的特定格式(逗号后的三位数字,没有科学记数法),我将值转换为字符串。问题是这些字符串默认是左对齐的,看起来不太好。

在 GUIDE 中使用 UITable 时,我能够用前导 space 填充字符串以使它们右对齐,如下所示

data = {'1532.000'; '   5.543'; '  26.457'};

使用单声道 space 字体,值在 table 中显示如下:

1532.000
   5.543
  26.457

目前我正在考虑切换到 App Designer。我使用相同的 space 填充字符串,但这里 uitable 似乎将它们剥离。结果如下所示:

1532.000
5.543
26.457

有没有办法让 App 设计工具中的 uitable 像在 GUIDE 中一样保留 space? 当然,不需要填充而直接右对齐字符串会更好,但据我所知这是不可能的。

以防万一:我使用的是 Matlab R2016b。

编辑:

生成和填充 UITable 的最小示例。这是一个简单的 AppDesigner GUI,我只添加了一个 table 和一个按钮(没有修改任何属性)。按钮的点击回调用于将数据添加到table.

classdef test < matlab.apps.AppBase

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

    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            data = {'1532.000'; '   5.543'; '  26.457'};
            app.UITable.Data = data;
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';
            setAutoResize(app, app.UIFigure, true)

            % Create UITable
            app.UITable = uitable(app.UIFigure);
            app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
            app.UITable.RowName = {};
            app.UITable.Position = [127 180 302 185];

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [220 104 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = test()

            % Create and configure 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

按下按钮后 table 看起来如下:

请注意,AppDesigner 只允许我修改 ButtonPushed 函数内的代码。

investigating 一段时间后,我发现 "special" 行为的原因是 uitable 是因为它实际上是一个 Java 对象! (我不知道他们是如何设法做到这一点的,也不知道为什么)。在任何情况下,右对齐数据都可以通过在脚本中插入一行来完成:

data = {'1532.000'; '   5.543'; '  26.457'};        
app.UITable.Data = data;
setColumnFormat(java(app.UITable),{'numeric'}); % < Add this

如果有不止一列您想以这种方式右对齐,只需根据需要多次复制 {'numeric'}

setColumnFormat(java(app.UITable), repmat( {'numeric'}, 1, size(data,2) ) );

上面的样式命令也可以使用更短的符号(感谢 指出这一点):

app.UITable.ColumnFormat = {'numeric'};
app.UITable.ColumnFormat = repmat( {'numeric'}, 1, size(data,2) );

结果:

如果我对 Yair 的博客理解正确,您可以通过查找 "JIDE Table" 或 SortableTable.

找到有关这些 table 对象自定义的更多信息