在MATLAB中调整Uitable的rowname列宽

时间:2013-07-15 10:10:18

标签: matlab matlab-uitable

我在MATLAB中使用了rownames。带有rownames的列太宽了,我想让它“紧凑” - 如何调整uitable的rownames列的大小?

1 个答案:

答案 0 :(得分:3)

以下代码示例包含执行此操作的方法。干杯。

FontSize = 18;
fh = figure;
data =  {1,1,1,1; 2,2,1,1; 3,6,1,1; 4,4,4,1; 6,5,4,1};
cnames = {'Speed (kph)','Curvature (m)','Banking (deg)','Elevation (m)'};
rnames = {'Braking','Entry','Mid Corner', 'Mid Throttle', 'Exit'};
t = uitable('Units','normalized','Position',[0 0 1 
1],'Data',data,'ColumnName',cnames,'RowName',rnames,'FontSize',FontSize);

hs = '<html><font size="+2">'; %html start
he = '</font></html>'; %html end
cnh = cellfun(@(x)[hs x he],cnames,'uni',false); %with html
rnh = cellfun(@(x)[hs x he],rnames,'uni',false); %with html
set(t,'ColumnName',cnh,'RowName',rnh) %apply

%get the row header
jscroll=findjobj(t);
rowHeaderViewport=jscroll.getComponent(4);
rowHeader=rowHeaderViewport.getComponent(0);
height=rowHeader.getSize;
rowHeader.setSize(80,360)

%resize the row header
newWidth=150; %100 pixels.
rowHeaderViewport.setPreferredSize(java.awt.Dimension(newWidth,0));
height=rowHeader.getHeight;
rowHeader.setPreferredSize(java.awt.Dimension(newWidth,height));
rowHeader.setSize(newWidth,height);

figPos = get(fh,'Position');
tableExtent = get(t,'Extent');
set(fh,'Position',[figPos(1:2), figPos(3:4).*tableExtent(3:4)]);