Calling a plotting function inside a loop.

조회 수: 4 (최근 30일)
B K
B K 2011년 11월 14일
Hello all. I'm trying to make a series of figures using a for loop. I need to do a good deal of processing on the points so I'm using my own function to generate the points for each given set of coefficients then convert from spherical to Cartesian coordinates and plot. The relevant code is as follows:
%The matrices dataexist and resultsmatrix are calculated earlier
c = 1;
for d = 1:26
if dataexist(d,c) == 1
figure(d)
coeffs = resultsmatrix(d,:,c);
plotsurface(coeffs);
end
end
function [] = plotsurface(coeffs)
%Cartesian points are calculated based on the coefficients
tri = delaunay(cart(:,1), cart(:,2));
[r,c] = size(tri);
trisurf(tri, cart(:,1), cart(:,2), cart(:,3));
The problem is, this gives me the first surface just fine on Figure 1, but every figure after that is blank. So somehow the function isn't plotting to it's proper figure. I'm wondering if maybe it's all overwriting onto Figure 1 despite my making a new figure before each call to plotsurface but I don't know why it would do that.

답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 14일
Create an axes within the figure, pass the axes handle down to plotsurface(), and parent your trisurf() against that particular axes by using the 'Parent' property:
thisaxes = axes('Parent',d);
coeffs = resultsmatrix(d,:,c);
plotsurface(coeffs, thisaxes);
function [] = plotsurface(coeffs, thisaxes)
[...]
trisurf(tri, cart(:,1), cart(:,2), cart(:,3), 'Parent', thisaxes);
  댓글 수: 4
B K
B K 2011년 11월 15일
Alright, I tried breakpoints which showed me that it still stubbornly overwrites in figure 1 no matter what I do. I even threw together a version without the loop just to see if I could get it to plot on two different figures by going
figure(1)
trisurf(...)
figure(2)
trisurf(...)
That always works for plot and surf and based on the documentation I don't see why trisurf should behave differently.
Walter Roberson
Walter Roberson 2011년 11월 15일
I have verified that with 2008b, trisurf() will first obey any 'Parent' parameter passed in, provided the parent is given as an axis; if no parent is passed in, then it will use gcf to find the current figure, and gca() that to find the current axes on the current figure, and will use that. If for some reason no figure is current, then figure() should be invoked, which will generally return the lowest positive-integer-numbered unused figure.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by