How do I put a uitable in a tiledlayout?

46 Ansichten (letzte 30 Tage)
Ben
Ben am 23 Mai 2024
Kommentiert: Matt J am 31 Mai 2024
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

Akzeptierte Antwort

Catalytic
Catalytic am 28 Mai 2024
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 Kommentare
Ben
Ben am 31 Mai 2024
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 am 31 Mai 2024
@Ben you should Accept-click Catalytic's answer if you managed to get it working for you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 24 Mai 2024
Bearbeitet: Matt J am 24 Mai 2024
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 Kommentare
Ben
Ben am 28 Mai 2024
I don't really like the look of these uifigures. Can I put ui tables in nested layouts, Matt?
Matt J
Matt J am 28 Mai 2024
Bearbeitet: Matt J am 29 Mai 2024
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by