increase or decrease "RowNames" column - table
12 views (last 30 days)
Show older comments
How do I increase or decrease "RowNames" column?
Similar to the command:
'ColumnWidth', {100,100,100}
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Height,Weight,'RowNames',LastName)
x = 0:pi/100:2*pi;
y = sin(x);
hAx=subplot(2,1,1);
hUI=uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx.Position , 'ColumnWidth' , {100,100,100});
delete(hAx)
hAx=subplot(2,1,2);
plot(x,y)
TNX
0 Comments
Accepted Answer
Adam Danz
on 31 May 2021
Edited: Adam Danz
on 2 Jun 2021
To set row-name column width to auto-fit the row-label-width:
If your goal is to fit the row name column to the width of the labels, create the uitable within a uifigure. The row-name column is auto-fitted in uifigures but not in regular figures. Also, use tiledlayout instead of subplot -or- turn off the AutoResizeChildren property of the uifigure to avoid an error produced by subplot.
tiledlayout & uifigure demo
Also note the use of axis and figure handles.
% Set up data
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Height,Weight,'RowNames',LastName);
x = 0:pi/100:2*pi;
y = sin(x);
% Plot uitable
fig = uifigure();
tlo = tiledlayout(fig,2,1);
hAx(1) = nexttile(tlo);
hUI = uitable(fig,'Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx(1).Position ,...
'ColumnWidth' , {100,100,100});
hAx(2)=nexttile(tlo);
plot(hAx(2),x,y)
delete(hAx(1)) % delete *after* 2nd axes are created
subplot & uifigure demo
Also note the use of axis and figure handles.
% Create figure, turn off AutoResizeChildren
fig = uifigure('AutoResizeChildren','off');
hAx(1) = subplot(2,1,1,'Parent',fig);
hUI=uitable(fig,'Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx.Position , ...
'ColumnWidth' , {100,100,100});
hAx(2) = subplot(2,1,2,'Parent',fig);
plot(hAx(2),x,y)
delete(hAx(1)) % delete *after* 2nd axes are created
To control width of row-name column:
I am not aware of a method that directly sets the column width of the row name. A workaround is to move the row names into column 1 of the data itself and then format that colum to make it look similar to the row-name column using uistyle & addStyle (requires Matlab >=r2019b). This requires you to use uifigure. Remember to index the table data by offsetting the column numbers by 1 to account for the row names.
The upper table is the default table with a row-name column. The lower table is without a row-name column but with formats column 1 to make it look like a row-name column.
fig = uifigure();
tlo = tiledlayout(fig,2,1);
hAx(1)=nexttile(tlo);
hUI=uitable(fig,'Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx(1).Position , 'ColumnWidth' , {100,100,100});
hAx(2)=nexttile(tlo);
C = [T.Properties.RowNames, table2cell(T)];
hUI2=uitable(fig,'Data',C,'ColumnName',[{''},T.Properties.VariableNames],...
'RowName',[],'Units', 'Normalized', 'Position',hAx(2).Position , 'ColumnWidth' , {65,100,100,100});
colStyle = uistyle('BackgroundColor',hUI2.BackgroundColor(2,:),'FontWeight','bold');
addStyle(hUI2,colStyle,'column',1)
delete(hAx)
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!