Use the same diagram for different sub-functions


I am calling a functions recursively and I want them all to draw in the same plot. When i try to create a handler and pass it on with the parameters I get the following error:

??? Error using ==> set Invalid handle object.

Error in ==> triangle at 23
set(h, 'xdata', [x1,x3], 'ydata', [y1,y3]);

Before calling my function I've created a handler and set my preferences:

h = plot([0,1],[0,0]);
set(h, 'erasemode', 'none');

This is my function:

function triangle(x1,y1,x2,y2, deepth , n,h)
%Paints a equilateral triangle for two given Points
if depth > n

    clf reset

    v_12 = [x2-x1;y2-y1];

    %rotate vector
    g_uz = [0.5,-sqrt(3)/2;sqrt(3)/2, 0.5];
    p = g_uz * v_12;
    x3 = p(1) + x1;
    y3 = p(2) + y1;

    axis([-10 10 -10 10]);
    axis off


    set(h, 'xdata', [x1,x3], 'ydata', [y1,y3]);

    set(h, 'xdata', [x2,x3], 'ydata', [y2,y3]);

    v_13 = [x3-x1,y3-y1];
    v_23 = [x3-x2,y3-y2];

    % 1-3 triangle
    triangle(x1+v_13(1)/3,y1 + v_13(1)/3, x1+ 2*v_13(1)/3,y1 + 2*v_13(1)/3, tiefe, n+1 );

Do you know any solutions? How can I Plot in an object form a function i called?

The clf on line 6 clears the figure, removing the line that you want to use as your graphic output.

Remove that line and see if it works.