필터 지우기
필터 지우기

Subplots in a for loop- find the mistake

조회 수: 1 (최근 30일)
Simon
Simon 2014년 7월 1일
댓글: Geoff Hayes 2014년 7월 1일
Dear all,
I want to create a plot with n subplots. The problem here is that all the subplots are the same (see picture), but they shouldn't as the data in the cell 'M' alters. I guess it is some mistake with 'hold off' (but I tried to put it nearly everywhere) or the looping of the cell, but can't find it.
figure
cl={'r','y','g'};
subplot(n+1,1,1),plot(P_est)
for m=2:n+1;
subplot(n+1,1,m);
hold off
for p=1:n;
for q=1:t;
xx=[q-1,q] ;
plot([ xx fliplr(xx) xx(1)],[0 0 1 1 0],cl{M{1,p}(q)+1},'linewidth',12)
hold on
end
end
end
Thanks!

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 7월 1일
I don't think that the hold off is the problem, and it can probably be removed due to the hold on that follows.
The outer loop, for m=2:n+1; updates each of the six (colourful!) subplots that follow the initial subplot. But aside from
subplot(n+1,1,m);
nothing else in that loop relies upon m. So in the block of code
for p=1:n;
for q=1:t;
xx=[q-1,q] ;
plot([ xx fliplr(xx) xx(1)],[0 0 1 1 0],cl{M{1,p}(q)+1},'linewidth',12)
hold on
end
end
there is no dependence upon m, and so each subplot will be drawn identically. It may be that the cell array M should include the m parameter (somehow).
  댓글 수: 2
Simon
Simon 2014년 7월 1일
To say it with Christoph Waltz: That's a BINGO ;)
Thanks! I changed the code to
for m=2:n+1;
subplot(n+1,1,m);
for q=1:t;
xx=[q-1,q] ;
plot([ xx fliplr(xx) xx(1)],[0 0 1 1 0],cl{M{1,m-1}(q)+1},'linewidth',12)
hold on
end
hold off
end
And now it works! By chance, do you know how I change the size of the subplots so the seven bars are smaller than the first plot?
Geoff Hayes
Geoff Hayes 2014년 7월 1일
Awesome!
As for resizing the seven bars to be smaller than the first plot, you could try the following. Grab the position of the subplot (using its handle). The position will be a 4 element vector with [x y width height] components. Reduce the element that corresponds to height, and reset the position:
h = subplot(n+1,1,m); % get the handle to the subplot
plotDims = get(h,'Position'); % get the subplot dimensions
plotDims(4) = plotDims(4)/5; % shrink the height by a factor of 5
set(h,'Position', plotDims); % reset
Try doing this before you do the plotting, but if that doesn't work, then just do it once you've iterated over p.

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

추가 답변 (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