Table embedded in uitable ignores ColumnFormat Property

4 views (last 30 days)
Any idea why a table embedded into a uitable control ignores the ColumnFormat property? In the example below, if I feed a cell array (data1 variable) to my uitable, I get the second column right-aligned, as desired. If I try the same thing with feeding a table to uitable as data (data2 variable), it obeys all of the other property commands except for ColumnFormat. Thoughts?
f = uifigure;
A = {'Test 1'; 'Test 2'};
B = {'1'; '2'};
data1 = [A, B];
data2 = table(A, B);
t = uitable(f);
% t.Data = data1;
t.Data = data2;
t.ColumnName = {'Coefficient'; 'Value'};
t.ColumnWidth = {99 99};
t.RowName = {};
t.RowStriping = 'off';
t.ColumnEditable = [false true];
t.ColumnFormat = {[] ['numeric']};
  2 Comments
Adam Danz
Adam Danz on 14 Jul 2019
Not only the alignment of numeric values differs but also the fontsize, row height, and header alignment. The image on the left is the cell input, the image on the right is table input.
190714 161139-.jpg
Jeremy
Jeremy on 14 Jul 2019
Indeed. The other stuff didn't bother me so much, mostly the alignment. Actually, the reason why I want to feed a table to uitable instead of an array is precisely because the row height is larger and more visually appealing to me.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 14 Jul 2019
Edited: Adam Danz on 15 Jul 2019
In your table "data2", the second column are not numeric values. They are of class char.
class(data2.B{1})
ans =
'char'
For whatever reason, even when you specify the column format as numeric, they are still treated as 'char' even through they are treated as numeric if you input a cell array (data1).
To fix the issue, use numeric values in your table.
A = {'Test 1'; 'Test 2'};
B = {1; 2};
data2 = table(A, B);
Or convert the values you already have
data2 = table(A,str2double(B));
  3 Comments
Jeremy
Jeremy on 14 Jul 2019
Edited: Jeremy on 14 Jul 2019
Here's something that's weird. If you change the exponent on 1.1254441e-05 to 1.1254441e+05, it displays fine.
Adam Danz
Adam Danz on 14 Jul 2019
Edited: Adam Danz on 17 Jul 2019
If your input is a cell array (data1 in your original example), you can set the ColumnFormat so that the numers are in sci notation or any other format listed here.
f = uifigure;
A = {'ROC'; 'k'; 'a2'; 'a4'};
B = {12.8302; 0.2904; 0; 1.1254441e-05};
data1 = [A, B];
t = uitable(f,'data',data1,'ColumnFormat',{[],'short g'});
t.ColumnName = {'Coefficient'; 'Value'};
t.ColumnWidth = {99 99};
t.RowName = {};
t.RowStriping = 'off';
t.ColumnEditable = [false true];
But for whatever reason, the ColumnFormat specification is ignored with table inputs. So unless there's an undocumented way to get around this, I'm not sure if you'll be able to have your cake (the Table input) and eat it, too (the sci notation).
As you discovered previously, you can always convert your numbers to string format and just use strings.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!