1

I already asked about this on Matlab Answers but did not get a response there, so I try it here.

I have some numbers I want to show in a UITable. Since I need a specific formatting of the values (three digits after comma, no scientific notation), I converted the values to strings. The problem is that these strings are by default left-aligned which looks not very nice.

When using UITable in GUIDE, I was able to pad strings with leading spaces to get them right aligned like in the following

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

Using a mono space font, the values are shown like this in the table:

1532.000
   5.543
  26.457

Currently I am considering switching to App Designer. I am using the same space-padded strings but here the uitable seems to strip them off. That is the result looks like the following:

1532.000
5.543
26.457

Is there a way to make uitable in App Designer keep the spaces like it did in GUIDE? Of course it would be even better to directly right-align the strings without the need of padding, but as far as I know this is not possible.

In case it matters: I am using Matlab R2016b.

Edit:

Minimal example for generating and filling a UITable. This is a simple AppDesigner GUI where I added only a table and a button (without modifying any attributes). The click callback of the button is used to add the data to the 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

After pushing the button the table looks as follows:

enter image description here

Note that AppDesigner only allows me to modify the code inside the ButtonPushed function.

luator
  • 4,769
  • 3
  • 30
  • 51
  • Did you try to adapt [this solution](http://stackoverflow.com/questions/38933254/how-to-customize-app-designer-figures-in-more-ways-than-officially-documented) to your needs? What you're asking is almost identical, with the only difference being that you ask about a table and the linked question deals with a list... In any case please add some minimal code that generates the `uifigure` in question. – Dev-iL Mar 08 '17 at 15:59
  • @Dev-iL Thanks, that looks really promising. I'm not sure if I'm able to spend time on this this week but I will do once I have the time. I report when I tried it. – luator Mar 08 '17 at 17:39
  • @Dev-iL I found time earlier than expected. A minimal example is added to the question. I also tried your attempt but unfortunately the table seems to be handled differently as is not there when I open it in a browser (all other elements are there). – luator Mar 09 '17 at 11:27

1 Answers1

3

After investigating this for a while, I found that the reason for the "special" behavior when it comes to a uitable is because it's actually a Java object! (I have no idea how they managed to pull this off or why). In any case, right-aligning the data can be done by inserting one line to your script:

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

if there's more than one column that you would like to right-align this way, simply replicate {'numeric'} as many times as needed:

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

There's also a slightly shorter notation possible for the styling commands above (thanks @luator for pointing this out):

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

The result:

Demonstration

If I understood Yair's blog correctly, you can find more information about the customization of these table objects by looking for "JIDE Table", or SortableTable.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • This indeed right-aligns the data while preserving the number formatting of the string (i.e. three decimal positions, no scientific notation). Right now your code only affects the first column (in my real application there are multiple columns), but I guess it should be easy to extend it (again I have no time right now but I will let you know if I find a solution). – luator Mar 10 '17 at 09:15
  • @luator - I have extended the solution to affect multiple columns. – Dev-iL Mar 12 '17 at 09:12
  • 1
    Thanks a lot for you effort! I was able to simplify your code a bit: `app.UITable.ColumnFormat = repmat(...);` has the same effect. – luator Mar 17 '17 at 08:33
  • So you... removed the call to `java()`? Ok... :) .. and you're welcome! – Dev-iL Mar 17 '17 at 08:36
  • Yes, apparently it is not needed. – luator Mar 17 '17 at 08:39