create a table next to a graph on figure

Hey
I want to create a table next to a graph
Is it possible?
Example code:
clc, clear
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)
%I want to see a picture next to the table in fig
figure(1)
subplot(121);
u=uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',[0, 0, 1, 1]);
% It does not work :(
subplot(122);
x = 0:pi/100:2*pi;
y = sin(x);
plot(y,x)
% I want like this but With a table
figure(2)
subplot(121);
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
subplot(122);
x = 0:pi/100:2*pi;
y = sin(x);
plot(y,x)
TNX :)

 Respuesta aceptada

dpb
dpb el 11 de Mzo. de 2021
A table object can't be put in a figure and, AFAIK, it's not possible to redirect command window output to a figure other than by preparing an image independently of the content desired.
But, you can put the uitable on subplot; you used wrong coordinate system -- I reoriented it to a 2x1 format instead of 1x2, but if your data fit more better the other way, you can easily revert.
hAx=subplot(2,1,1); % set subplot area
hUI=uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx.Position);% set table on top
delete(hAx) % no need for the axes
hAx=subplot(2,1,2); % make the other axes
plot(x,y) % and plot into them
results in
Your table doesn't have enough data to take up the whole area of the axes, but you can see the outline of the area the top axes laid out.
I didn't try with nexttile; I presume same thing will happen.

6 comentarios

Shahar ben ezra
Shahar ben ezra el 12 de Mzo. de 2021
Thank you very much!
you helped me alot
Do you know how to create a table title?
To graph I succeed
dpb
dpb el 12 de Mzo. de 2021
Editada: dpb el 13 de Mzo. de 2021
Since it (the uitable) isn't a part of handle graphics it has the figure as a parent, not the axes -- and title is associated with an axes, not a figure. You could not delete the first axes but make it invisible by
hAx(1)=subplot(2,1,1); % set subplot area, save handle
hTitle=title('A table on same area as underlying axes');
hAx(1).Visible='off'; hTitle.Visible='on';
hUI=uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position',hAx(1).Position);
hAx(2)=subplot(2,1,2); % make the other axes
plot(x,y) % and plot into them
Alternatively, you create a container of the same size as the axes and place the table in it along with a text label.
Shahar ben ezra
Shahar ben ezra el 25 de Abr. de 2021
Could it be that in versions 2018 it will not work this way?
tnx agian
dpb
dpb el 25 de Abr. de 2021
What "not work" in what way?
I used R2019b but there's been no change in this area in ages that I'm aware of.
Show actual code and what didn't work as expected.
When I use the 2018 version I get the next error
Unrecognized method, property, or field 'Position' for class 'matlab.Gra​phics.axis.axes
clc, clear
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
Table_freq_sortrows = table(Age,Height,Weight,'RowNames',LastName)
x = 0:pi/100:2*pi;
y = sin(x);
hAx=subplot(2,1,1);
hUI=uitable('Data',Table_freq_sortrows{:,:},'ColumnName',Table_freq_sortrows.Properties.VariableNames,...
'RowName',Table_freq_sortrows.Properties.RowNames,'Units', 'Normalized', 'Position',hAx.Position);
delete(hAx)
hAx=subplot(2,1,2);
plot(x,y)
dpb
dpb el 26 de Abr. de 2021
You didn't keep the two axes handles but wiped out the first...look at my code more carefully.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 11 de Mzo. de 2021

Comentada:

dpb
el 26 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by