MATLAB: Is there an option for the UITABLE object which allows the width of the columns to adjust according to their content in MATLAB 7.11 (R2010b)

autofitcolumnwidthcontentdynamicdynamicallyMATLABuitable

In a UITABLE I have many columns of data and would like the width of the columns to be auto-adjusted such that all their content is visible.

Best Answer

The ability to set the column width of UITABLE such that it fully displays content is not available in MATLAB.
To work around this issue, we can set the 'ColumnWidth' property for each column based on the maximum length of data it contains. This can be implemented as follows:
% Assume data is provided
data = {'data1','data2','data3','very large data which does not fit in column';'a','b','c','smallerstringthatfits'};
% Find the size of data
dataSize = size(data);
% Create an array to store the max length of data for each column
maxLen = zeros(1,dataSize(2));
% Find out the max length of data for each column
% Iterate over each column
for i=1:dataSize(2)
% Iterate over each row
for j=1:dataSize(1)
len = length(data{j,i});
% Store in maxLen only if its the data is of max length
if(len > maxLen(1,i))
maxLen(1,i) = len;
end
end
end
% Some calibration needed as ColumnWidth is in pixels
cellMaxLen = num2cell(maxLen*7);
% Create UITABLE with required arguments
hTable=uitable('parent',gcf,'units','pixels','position',[20 20 400 300]);
set(hTable, 'Data', data);
% Set ColumnWidth of UITABLE
set(hTable, 'ColumnWidth', cellMaxLen);