How do I put a uitable in a tiledlayout?

44 次查看(过去 30 天)
Hello,
I'm trying to graph some data in a particular way. I need to make the same 4 plots over a variable number of iterations of data and put them all into one fixed-size figure. I need one of these 4 plots to be a table displaying some info about the data. I tried using uitable, but it errors when you try to pass it a tiled layout as it's parent. I could put the figure handle as the parent, but that seems difficult to position and size correctly.
How can I accomplish what I'm trying to do? Is there an alternative to uitable I could use?
Here is my example code and output:
%variable number of data files to plot; could be 4,8, or 15
thingsToPlot=8;
%creates square for tiles depending on # of files; 2x for double wide plots
n=2*ceil(sqrt(thingsToPlot));
%Parent Figure; Size must stay the same to allow exporting to ppt to be consistent
fig = figure('Name','Example','NumberTitle','off','units','inches','Position',[4 2 10 6.4]);
%Tiled layout parent container, child of fig
TL=tiledlayout(fig,n,n,'TileSpacing','tight');
for i=1:thingsToPlot
%Child container; Need to plot a 2x2 tile for each file and it in parent container
tl=tiledlayout(TL,2,2,'Padding','tight','TileSpacing','tight');
tl.Title.String=['Iteration ' num2str(i)];
%double wide tiles so they're more visible;
tl.Layout.TileSpan=[2 2];
%Positioning math to make the 2x2 tiles fit and sequence correctly
pos=2*i-1;
tl.Layout.Tile=pos+n*floor(pos/n);
%example data
data=rand(3,5);
nexttile(tl)
plot(data(1,:))
nexttile(tl)
plot(data(2,:))
nexttile(tl)
plot(data(3,:))
nexttile(tl)
tab=table();
tab.A=data(1,:);
tab.B=data(2,:);
tab.C=data(3,:);
%want to put a table here
% uit=uitable(tl,tab);
end

采纳的回答

Catalytic
Catalytic 2024-5-28
Must it be a table that the user can modify interactively? If not, then you could use the attached non-UI version of uitable (courtesy of ChatGPT).
%variable number of data files to plot; could be 4,8, or 15
thingsToPlot=8;
%creates square for tiles depending on # of files; 2x for double wide plots
n=2*ceil(sqrt(thingsToPlot));
%Parent Figure; Size must stay the same to allow exporting to ppt to be consistent
fig = figure('Name','Example','NumberTitle','off','units','inches','Position',[4 2 10 6.4]);
%Tiled layout parent container, child of fig
TL=tiledlayout(fig,n,n,'TileSpacing','tight');
for i=1:thingsToPlot
%Child container; Need to plot a 2x2 tile for each file and it in parent container
tl=tiledlayout(TL,2,2,'Padding','tight','TileSpacing','tight');
tl.Title.String=['Iteration ' num2str(i)];
%double wide tiles so they're more visible;
tl.Layout.TileSpan=[2 2];
%Positioning math to make the 2x2 tiles fit and sequence correctly
pos=2*i-1;
tl.Layout.Tile=pos+n*floor(pos/n);
%example data
data=rand(3,5);
nexttile(tl)
plot(data(1,:))
nexttile(tl)
plot(data(2,:))
nexttile(tl)
plot(data(3,:))
ax=nexttile(tl);
axisTable(ax, data',{'A','B','C'})
end
  4 个评论
Ben
Ben 2024-5-31
I can't change the dimensions of the figure because it needs to be exported to a powerpoint.
I was able to get somewhere by changing the decimal format and messing with the axis positions, so now it's legible.
Matt J
Matt J 2024-5-31
@Ben you should Accept-click Catalytic's answer if you managed to get it working for you.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2024-5-24
编辑:Matt J 2024-5-24
If you are going to be inserting uitables, it would be advisable to use UI graphics elements everywhere else too,
fig=uifigure;
G=uigridlayout(fig,[3,3]);
k=0;
for i=1:8
p=uipanel(G,'Title',"Iteration "+i);
g=uigridlayout(p,[2,2]);
for j=1:3
k=k+1;
ax(k) = uiaxes(g);
end
uitable(g,'Data',rand(3,3));
end
for i=1:numel(ax)
plot(ax(i),rand(1,5));
end
  2 个评论
Ben
Ben 2024-5-28
I don't really like the look of these uifigures. Can I put ui tables in nested layouts, Matt?
Matt J
Matt J 2024-5-28
编辑:Matt J 2024-5-29
No, but you can set the uipanel properties so that the figure looks more like a conventional tildedlayout, e.g.,
p=uipanel(G,'Title',"Iteration "+i,...
'TitlePosition','centertop','FontSize',16,'BorderWidth',0);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by