Search code examples
matlabmatlab-figure

Delete object handle and keep variable in MATLAB


Using the delete function, I can delete the object handle but it also removes the object properties from the variable.
Is there a way to delete the object handle without touching to the variable properties?

For example, I have two plots like below and I delete the first one.

figure;
h1 = plot( 1:10, '*' );
hold on
h2 = plot( 2:5, '.' );
delete( h1 );
h1.XData % returns an error, handle has been removed

The h1 object handle has been removed from the figure as expected but all the h1 properties (XData, YData) also have been removed. Is there a way to keep the h1 properties? Do I necessarily need to back up the properties before using delete?


Solution

  • Instead of deleting h1, simply hide it:

    h1.Visible = 'off';
    

    And you can see the data is still there:

    >> h1.XData
    ans =
         1     2     3     4     5     6     7     8     9    10