如何在 matlab 中居中对齐合适的列?

How to center-justify uitable columns in matlab?

如何在 uitable 中居中对齐列(字符串和数字)?这是为了演示,所以 "fake it" 可以用空格填充它。 (使用 R2018a。)

% construct simple table as example %
t = array2table(zeros(4,1));
t.Properties.VariableNames{'Var1'} = 'id';
t.id(:) = 1;
t.fieldA = {'a'; 'b'; 'c'; 'd'};
t.GroupCount = [22; 1; 19; 0];

% plotting
f = figure;
uit = uitable(f, 'Data', table2cell(t));
uit.ColumnName={t.Properties.VariableNames{:}};
uit.RowName=[];

% test justification on the first column:
uit.ColumnFormat = {'numeric'}; % this right justifies
uit.ColumnFormat = {'char'}; % this left justifies
% how can I get it center justified?

类似于uit.ColumnFormat = {'%5s'};但是它不能传递这个输入(下面的错误);有什么办法可以骗过它吗?

Error setting property 'ColumnFormat' of class 'Table':
ColumnFormat definitions must be either 'numeric', 'logical', 'char', 'short', 'long', 'short e', 'long e', 'short g', 'long g', 'short eng', 'long eng',
'bank', '+', 'rat' or be a popupmenu definition

这可以使用 undocumented 功能完成,如下所示:

for k=1:numel(uit.Data)
    uit.Data{k} =['<html><tr><td width=9999 align=center>',num2str(uit.Data{k})];
end

结果: