Using a loop to print subplots

조회 수: 3 (최근 30일)
TastyPastry
TastyPastry 2015년 10월 12일
댓글: Walter Roberson 2015년 10월 14일
I want to print an 2x2 grid of plots on an axes in a GUI. I'd like for there to be multiple "pages" of these plots so a user can plot more than 4 and be able to flip through the axes using the 'visible' attribute. My code looks like this:
for i=1:totalPlotNum
%code to calculate which subplot and axes to plot the data on
axesNum = ceil(i/4);
subLoc = 4*(i/4 - floor(i/4));
if subLoc == 0
subLoc = 4;
end
%switch focus to the correct axes and subplot
axes(printPlot.plotAxes(axesNum)
subplot(2,2,subLoc);
%plot
end
printPlot.plotAxes is an array of axes objects which are the "pages" the user can flip through.
My issue is that when Matlab runs axes() and subplot() after the first iteration, the axes object at printPlot.plotAxes(axesNum) gets deleted. Then, on the second iteration, Matlab crashes to desktop because axes() is trying to focus on a deleted axes handle. How do I get Matlab to stop deleting the axes, or do I need to adjust my code such that after running subplot() once, axes() is not called with a repeated axes?

채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 12일
Call subplot once for each axes to be created and store the results in printPlot.plotAxes . After that do not call subplot inside the loop: use the axes handle you stored.
  댓글 수: 4
TastyPastry
TastyPastry 2015년 10월 14일
So if I wanted to flip through pages of 2x2 grids, would I use the figure 'visible' option? Is there a way to make all the figures appear at the same spot, i.e. if I move one figure window and change pages, the next figure appears in the same spot the previous one disappeared in?
Walter Roberson
Walter Roberson 2015년 10월 14일
You can pass Units and Position parameters to the figure() call

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by