0

I am trying to plot something in Matlab. First I am ploting some 3d points and then I execute the hold command. Next I plot some other plots. I want just my first plot to be remembered and never cleard. The other plots will always we deleted after plot command executes.

Example:

 plot3(X,Y,Z,'ro');
 hold;

% now I will plot a lot of other plots here in every iteration. % plot number 2

 [x,y] = meshgrid(X,Y);
 z = a.*x + b.*y + c';
 mesh(x,y,z);

% plot number 3 - plot number 2 should be deleted

[x,y] = meshgrid(X,Y);
 z = a.*x + b.*y + c';
 mesh(x,y,z);

% plot number 4 - plot number 3 should be deleted

[x,y] = meshgrid(X,Y);
 z = a.*x + b.*y + c';
 mesh(x,y,z);

... and so on. So, I want just the first plot to be remembered.

I am trying to do it but I cant find a solution up to now.Thnx a lot :).

EDITED:

I will call this function from c#:

function [] = PlotMatlab(a,b,c,X,Y,Z )

    [x,y] = meshgrid(X,Y);
    z = a.*x + b.*y + c;
    mesh(x,y,z);

end

...so whenever I call this function from c# I need the previous plot (mesh) to be deleted

Student
  • 205
  • 2
  • 11

2 Answers2

2

You can store a handle to drawn meshs and delete them using delete before you draw the next mesh

 h = mesh(x,y,z);

 delete(h) % deletes the mesh
H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • ...well it is a good idea, but I need to call this from c#. Please read my edited code. I edited it after I read your answer. Sorry. – Student May 03 '13 at 14:23
  • @Addon so does this not work when matlab is called from C# ? Note that it also works for `plot`, not just for `mesh`. – Dennis Jaheruddin May 03 '13 at 14:27
  • @Addon: No problem. Please see http://stackoverflow.com/questions/11419209/in-matlab-how-does-one-clear-the-last-thing-plotted-to-a-figure for a similar question. One of the un-accepted answers will be helpful in your case. – H.Muster May 03 '13 at 14:28
  • @H.Muster I am trying your suggested link. Thnx a lot :) – Student May 03 '13 at 14:33
  • 2
    also rather than deleting and re-plotting from scratch each time, you could get a handle to the graphic object, then update it each iteration, i.e something like: `h = plot(..); set(h,'XData',...)` – Amro May 03 '13 at 14:46
  • @H.Muster well I tried suggested link but none of them is deleting the previous mesh...and I think that it would be better as you said to update. I found how to update a plot but there is no way to update a mesh I think! – Student May 03 '13 at 15:08
  • and I am sure that there should be a way to get the last plot and delete it or do something with it. – Student May 03 '13 at 15:20
  • @Addon: `mesh` creates a surface object. See [here](http://www.mathworks.com/help/matlab/ref/surfaceplotproperties.html) for list of properties including: `CData`, `XData`, `YData`, `ZData`, etc... – Amro May 03 '13 at 15:51
0

OK it is working. Now this is my function.

function [] = PlotMatlab(a,b,c,X,Y,Z,n)
    if n > 1      
        items = get(gca, 'Children');
        delete(items(1));
    end
    [x,y] = meshgrid(X,Y);
    z = a.*x + b.*y + c;
    mesh(x,y,z);
end

If I send n = 1 it will not delete anything and if I send n > 1 it will delete the last plot. Exactly what I needed. Now whenever I want to delete the last plot I call it with n = 2. Thnx :)

Student
  • 205
  • 2
  • 11