Calling a plotting function inside a loop.

1 view (last 30 days)
B K
B K on 14 Nov 2011
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.

Answers (1)

Walter Roberson
Walter Roberson on 14 Nov 2011
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 Comments
B K
B K on 15 Nov 2011
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 on 15 Nov 2011
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.

Sign in to comment.

Categories

Find more on Surfaces, Volumes, and Polygons 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!