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)?

6 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Mar 2011
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);
  1 Comment
Ben Oeveren
Ben Oeveren on 26 Sep 2018
To make it dynamically, you can use:
set(utable,'units','pixel');
tablew = utable.Position(3); %get with of the uitable
f = tablew/sum(maxLen); % Normalize it to the with of the table
maxLen = max(cellfun(@length,C),[],1); % Calculate the with of the data in cell C.
cellMaxLen = num2cell(maxLen*f);
set(utable, 'ColumnWidth', cellMaxLen);

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!