increase or decrease "RowNames" column - table

조회 수: 14 (최근 30일)
Shahar ben ezra
Shahar ben ezra 2021년 5월 31일
편집: Adam Danz 2021년 6월 2일
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

채택된 답변

Adam Danz
Adam Danz 2021년 5월 31일
편집: Adam Danz 2021년 6월 2일
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
Shahar ben ezra
Shahar ben ezra 2021년 6월 2일
And I thought it would be just one line of code
TNX
Adam Danz
Adam Danz 2021년 6월 2일
편집: Adam Danz 2021년 6월 2일
Most of the code in my answer is setting up the table. Depending which option you choose, there are really only about 1-4 lines of code that you need to implement.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by