Creating subplots in a for cycle with App Designer

27 views (last 30 days)
Hi all,
I’m trying to create subplots within a Panel with a for cycle in App Designer.
I was able to implement my first 3 subplots in a single panel in the following way:
app.subplot1(3,1,1,'Parent',app.Plotpanel);
plot(app.subplot1,t,a(:,1),’g’]);
hold(app.subplot1);
app.subplot2(3,1,2,'Parent',app.Plotpanel);
plot(app.subplot2,t,a(:,2),’b’]);
hold(app.subplot2);
app.subplot3(3,1,3,'Parent',app.Plotpanel);
plot(app.subplot3,t,a(:,3),’r’]);
hold(app.subplot3);
I have defined my subplots at the beginning of the app in the following way:
properties (Access = public)
subplot1 matlab.ui.control.UIAxes % Description
subplot2 matlab.ui.control.UIAxes % Description
subplot3 matlab.ui.control.UIAxes % Description
Supposing that my matrix a to plot is actually is bigger than 3 columns and I want to plot, at any cycle, columns 1,2,3 and then 4,5,6 and then 7,8,9 etc etc..
How can I implement that in App Designer? If I write this:
figure(n3) %n3 is the index that moves the columns of a in a for cycle
app.subplot1(3,1,1,'Parent',app.Plotpanel);
plot(app.subplot1,t,a(:,1),’g’]);
hold(app.subplot1);
I get this message: Unable to use a value of type matlab.ui.container.Panel as an index.
I would like that every set of 3 columns of the data are plotted as subplots in the same panel, maybe adding a sort of scrolling tab, is this possibile?
I hold Matlab R2022a. Thanks to everyone who will be able to help me.

Accepted Answer

Kevin Holly
Kevin Holly on 26 Apr 2022
I created an app that generates tabs and subplots based on the dimensions of matrix a. Please see the app attached. I did not use subplots but used axes() instead.
  3 Comments
Iacopo
Iacopo on 2 May 2022
Hi Kevin,
Do you also know how I can refresh the tabs content after changing the number of rows/columns? Because for example if I plot less number of columns after the first plot, the previous plots will still be displayed. Thank you
Kevin Holly
Kevin Holly on 2 May 2022
You can add the lines:
delete(app.TabGroup.Children(2:end))
delete(app.TabGroup.Children(1).Children)
within the push button callback as shown below
function PlotMatrixButtonPushed(app, event)
a = rand(app.RowsEditField.Value,app.ColumnsEditField.Value);
delete(app.TabGroup.Children(2:end))
delete(app.TabGroup.Children(1).Children)
for i = 4:3:size(a,2)
if ceil(i/3) > size(app.Panel.Children.Children)
uitab(app.TabGroup,'Title',['Set' num2str(ceil(i/3))])
end
end
for i = 1:3:size(a,2)
ax1 = axes(app.Panel.Children.Children(ceil(i/3)),"Position",[0.1300 0.7093 0.7750 0.2157]);
plot(ax1,a(:,i))
if i+1 <=size(a,2)
ax2 = axes(app.Panel.Children.Children(ceil(i/3)),"Position",[0.1300 0.4096 0.7750 0.2157]);
plot(ax2,a(:,i+1))
end
if i+2 <=size(a,2)
ax3 =axes(app.Panel.Children.Children(ceil(i/3)),"Position",[0.1300 0.1100 0.7750 0.2157]);
plot(ax3,a(:,i+2))
end
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!