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

采纳的回答

MathWorks Support Team
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
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 CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by