Insert two plots and a precreated table in the same figure

12 次查看(过去 30 天)
Hello,
I have this code:
subplot(2,2,1)
yplot_balance = table2array(Tablecalc(:,6));
xplot_balance = table2array(Tablecalc(:,8));
plot(xplot_balance,yplot_balance, '.-')
xlabel('Fecha de cierre de la operación')
ylabel('Total equity')
grid on
subplot(2,2,2)
yplot_r = table2array(Tablecalc(:,5));
xplot_r = table2array(Tablecalc(:,8));
plot(xplot_r,yplot_r, '.-')
xlabel('Fecha de cierre de la operación')
ylabel('Resultado en Rs')
grid on
newfig = uifigure('name','Datos');
uit = uitable(newfig,'Data',Tablecalc, 'Position', [20 20 500 150]);
With that, so far I have been able to create plots in the same figure and insert a table to a separate figure.
However I am trying to insert in the plots' figure the table. The table has ints, doubles, strings and datetime values (I tried to convert it to an array or cells and did not work either).
In each method I have tried I get this error: Error using uitable Functionality not supported with figures created with the figure function. For more information, see Graphics Support in App Designer.
Some help please!

采纳的回答

Mike Danziger
Mike Danziger 2021-7-23
编辑:Mike Danziger 2021-7-23
Hi Camilo,
It looks like you are trying to add two plots and a data table to a figure. The problem with your code is that by default your subplots are being placed into a figure, but uitable is only compatible with uifigure. It is possible to place subplots into a uifigure, but it takes a bit more work, and you will have to use a uipanel. Please see the documentation here:
Here is a simplified example that demonstrates what I think you are trying to achieve:
% create a uifigure with desired dimensions
f = uifigure;
f.Position = [1000, 1000, 1200, 800];
% create a uipanel to hold your subplots
panel = uipanel(f);
panel.Position = [0,400,1200,400];
panel.AutoResizeChildren = 'off';
% create subplots parented to the uipanel
ax1 = subplot(1,2,1, 'Parent', panel);
ax2 = subplot(1,2,2, 'Parent', panel);
plot(ax1, 1:10);
plot(ax2, 1:10);
% create a uitable and add it to the uifigure
vars = {'Age','Systolic','Diastolic','Smoker'};
t = t(1:15,vars);
ui_table = uitable(f, 'Data', t);
ui_table.Position = [10, 10, 1180, 380];
Hope this helps!
-Mike
  3 个评论
Camilo Acevedo
Camilo Acevedo 2021-7-24
Got it! Even though the code you posted did not work for me your explantion was very helpful!
I was able to do it:
Thank you Mike!
% create a uifigure with desired dimensions
fig = uifigure;
fig.Position = [80, 80, 900, 600];
% % create a uipanel to hold your subplots
panel = uipanel(fig,'Position',[320 20 570 300]);
panel.AutoResizeChildren = 'off';
% create subplots parented to the uipanel
subplot(1,2,1, 'Parent', panel);
plot(panel, xplot_balance,yplot_balance, '.-')
subplot(1,2,2, 'Parent', panel);
plot(panel, xplot_r,yplot_r, '.-')
% create a uitable and add it to the uifigure
uitable(fig,'Data',Tablecalc, 'Position', [20 400 400 150]);
Mike Danziger
Mike Danziger 2021-7-26
Hi Camilo,
Apologies, a line got lost at the top of the script when I pasted the code. This just loads some sample data for the table:
t = readtable('patients.xls');
Glad it helped, though!
Cheers,
-Mike

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2021-7-27
Camilo, in general you don't need all those table2array calls. Just do this:
plot(Tablecalc.NameOfYour6thVar,Tablecalc.NameOfYour8thVar, '.-')
If you really, really have a need to pull variables out of a table, do this:
yplot_balance = Tablecalc.NameOfYour6thVar
But chances are you don't need to do that.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by