How to draw an uncertain number of sub-images in one coordinate in app designer?
Show older comments
I can use matlab script to draw VMD decomposition diagram, but I don't know how to draw such a diagram in one coordinate in app designer? Below is the code in my matlab script and the picture I need to draw. Thank you very much for your time.
[m,n]=size(u);
figure
subplot(m+1,1,1);
plot(DATA.data,'k');grid on;
title('Original data');
for i = 1:m
subplot(m+1,1,i+1);
plot(u(i,:),'k');
title(['Signal',num2str(i-1),':']);
end

1 Comment
Benjamin Kraus
on 4 Jun 2024
Can you clarify what you mean by "one coordinate" in the sentence "I don't know how to draw such a diagram in one coordinate in app designer"?
Answers (2)
Benjamin Kraus
on 4 Jun 2024
Edited: Benjamin Kraus
on 4 Jun 2024
I'm not sure what you mean by "one coordinate", but the way I would create the diagram above in App Designer would be:
- When designing your app, drag and drop a panel into the location in your app in which you want to draw the diagram.
- Within your code (either your startup function or any other callback of your app), create a tiledlayout with a vertical layout.
- Add as many axes as you want to the layout using the nexttile command and plot into those axes.
For example, assuming you have a handle to your panel named app.UIPanel:
% This code is written automatically when you drag and drop a panel into your app.
app.UIPanel = uipanel;
% This code needs to be added to a callback function of your app.
tcl = tiledlayout(app.UIPanel, 'vertical');
% This code creates a number of different axes and plots into them.
m = 5;
for i = 1:m
ax = nexttile(tcl);
plot(ax, 1:10, sin(1:10),'k');
title(ax, ['Signal ',num2str(i-1)]);
end
5 Comments
Xiangfeng
on 4 Jun 2024
Benjamin Kraus
on 4 Jun 2024
Edited: Benjamin Kraus
on 4 Jun 2024
@Xiangfeng: I think the difference between your code and my code is that it looks like subplot is not making enough room for the titles, so you aren't seeing any titles. tiledlayout is making room for the titles, but perhaps too much room, which is why the axes are so short.
One option is to make your panel bigger, but I'll assume that isn't an option due to space constraints in your app.
There are a few things you can do to compensate, as demonstrated by the code below, which shows a few options. You can do all the options, or just some of them.
% This code is written automatically when you drag and drop a panel into your app.
app.UIPanel = uipanel;
% This code needs to be added to a callback function of your app.
tcl = tiledlayout(app.UIPanel, 'vertical');
% Option 1: Decrease the space between axes (the TileSpacing) or the space
% around the layout (the Padding).
tcl.TileSpacing = 'tight';
tcl.Padding = 'tight';
m = 5;
for i = 1:m
ax = nexttile(tcl);
% Option 2: Decrease the font size on the axes.
ax.FontSize = 8;
plot(ax, 1:10, sin(1:10),'k');
% Option 3: Remove x-tick labels from all but the last axes.
if i < m
xticklabels(ax,[])
end
% Option 4: Decrease the font size on the title.
title(ax, ['Signal ',num2str(i-1)],'FontSize',9);
end
Xiangfeng
on 5 Jun 2024
Benjamin Kraus
on 5 Jun 2024
@Xiangfeng: As the error states, that indicates that your axes (stored in the variable ax) has been deleted.
Based on the code I can see, I suspect the issue is that your calls to subplot are deleting axes.
The subplot command will delete any axes that overlap with the axes you are trying to create. You are calling subplot in a loop, and appending new values to a vector of axes handles stored in the variable ax. You are also calling subplot twice each loop. My guess is that a later call to subplot is deleting an axes created in an earlier call to subplot, so that when you call xticklabels one of the elements in the vector has been deleted.
I suspect the first fix you want to apply is to index into ax when you call xticklabels:
xticklabels(ax(i+1), [])
Xiangfeng
on 5 Jun 2024
help stackedplot
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






