1

我已经问这对Matlab Answers,但没有得到回应那里,所以我在这里尝试。右对齐值在应用程序设计

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

当指南使用UITable,我能垫串与领先的空间,让他们正确就像下面

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

使用单一空间字体,值显示这样的表对齐:

1532.000 
    5.543 
    26.457 

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

1532.000 
5.543 
26.457 

有没有一种方法,使uitable在应用程序设计保留空间,如它GUIDE做? 当然,它甚至会更好,直接右对齐的字符串,而不需要填充的,但据我所知,这是不可能的。

如果它的事项:我用Matlab R2016b。

编辑:

用于产生和填充UITable最小例子。这是一个简单的AppDesigner GUI,我只添加了一个表格和一个按钮(不修改任何属性)。该按钮的点击回调用于将数据添加到表格中。

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 

按下按钮后,该表如下所示:

enter image description here

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

+0

您是否尝试将[此解决方案]修改为适用于[此解决方案](http://stackoverflow.com/questions/38933254/how-to-customize-app-designer-figures-in-more-ways-than-officially-documented)您的需求?你所要求的几乎是相同的,唯一的区别是你询问一个表,并且链接的问题涉及一个列表......在任何情况下,请添加一些生成所讨论的uifigure的最小代码。 –

+0

@ Dev-iL谢谢,看起来很有希望。我不确定我是否能够在本周花时间在这个星期,但一旦我有时间,我会做。我尝试时报告。 – luator

+0

@ Dev-iL我发现时间比预期的要早。一个最小的例子被添加到问题中。我也尝试过你的尝试,但不幸的是,当我在浏览器中打开它时(所有其他元素都在那里),表似乎处理不同。 – luator

回答

3

investigating这一段时间后,我发现,对于“特殊”行为的原因,当涉及到一个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))); 

还有一个稍短符号可能为上述定型命令(感谢@luator指出了这一点):

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

结果:

Demonstration

如果我的理解亚伊尔的博客正确的,你可以通过寻找“基德表”,或SortableTable找到这些表对象的更多定制信息。

+0

这确实对齐了数据,同时保留了字符串的数字格式(即三位小数,没有科学记数法)。现在你的代码只影响第一列(在我的真实应用程序中有多列),但我想它应该很容易扩展它(我现在没有时间了,但如果找到解决方案,我会通知你) 。 – luator

+0

@luator - 我已经扩展了解决方案以影响多列。 –

+1

非常感谢您的努力!我能够简化你的代码:'app.UITable.ColumnFormat = repmat(...);'具有相同的效果。 – luator