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 (최근 30일)
이전 댓글 표시
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.
채택된 답변
MathWorks Support Team
2011년 3월 25일
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
Ben Oeveren
2018년 9월 26일
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!