Is there a way to easily capture subplots in an app?

3 views (last 30 days)
We have a large library of existing plotting functions, most of which generate multiple subplots, and want to use some of these from within an app. The issue we've run into is that many of the plotting routines will automatically create a new figure window, rather than plotting within the app window. Even when the current axis is explicitly set, the plot commands will default to a non-existing axis and therefore open a new figure window. This can be fixed by explicitly passing the axis handle to the plotting functions, but implementing this for our entire library of plotting routines isn't a feasible solution.
A simplified version of our issue is the following, where our app includes a panel container to hold the various plots
%plot panel definition
app.PlotPanel = uipanel( app.UIFigure );
app.PlotPanel.Title = 'Plot Panel';
app.PlotPanel.Position = [1 1 640 353];
app.PlotPanel.AutoResizeChildren = 'off';
% Case 1 / This will plot into the uipannel as needed
ax = subplot(5,1,1,'Parent', app.PlotPanel);
plot(ax, rand(100,1));
% Case 2 / This will NOT plot into the uipannel
ax = subplot(5,1,2,'Parent', app.PlotPanel);
plot(rand(100,1));
% Case 3 / This will NOT plot into the uipannel either
% Plot is not picking up that the current figure and current
% axes is the UIfigure and uipanel - How can i set this ??
ax = subplot(5,1,3,'Parent', app.PlotPanel);
axes(ax);
plot(rand(100,1));
Cases 2 and 3 would be ideal for us, as they would require no code changes, whereas case 1 needs considerable rewrites. Is there any solution or workaround for this issue?

Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2022
When you use plotting commands without passing in the parent container, they use gca() or gcf() as appropriate. gca() uses gcf() and accesses the CurrentAxes property of what is returned, and if that is empty it uses axes() to create a new axes inside the figure. In turn, gcf() looks at the groot CurrentFigure property and if it is empty then uses figure() to create a new traditional figure.
A challenge in this is that a uifigure() such as is used by App Designer can never be CurrentFigure and so CurrentAxes can never refer to a uiaxes()
uifigure() and uiaxes() have no notion of "current".
Unfortunately there is not a lot you can do other than search and replace matlab function names to similar names that are interfaces to routines that pull out some "current" uiaxes and pass it to the matlab plotting routine.

More Answers (0)

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!