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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 25 de Mzo. de 2011
Comentada: Ben Oeveren
el 26 de Sept. de 2018
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.
Respuesta aceptada
MathWorks Support Team
el 25 de Mzo. de 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 comentario
Ben Oeveren
el 26 de Sept. de 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);
Más respuestas (0)
Ver también
Categorías
Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!