Delete Graphics Objects
How to Delete Graphics Objects
Remove graphics objects with the delete function.
Pass the object handle as an argument to delete.
For example, delete the current axes, and all the objects contained
in the axes, with the statement.
delete(gca)
If you want to delete multiple objects, pass an array of handles
to delete. For example, if h1, h2,
and h3 are handles to graphics objects that you
want to delete, concatenate the handles into a single array.
h = [h1,h2,h3]; delete(h)
Closing a figure deletes all the objects contained in the figure. For example, create a bar graph.
f = figure; y = rand(1,5); bar(y)
The figure now contains axes and bar objects.
ax = f.Children; b = ax.Children;
Close the figure:
close(f)
MATLAB® deletes each object.
f
f =
handle to deleted Figure
ax
ax =
handle to deleted Axesb
b =
handle to deleted BarHandles to Deleted Objects
When you delete a graphics object, MATLAB does not delete the variable that contains the object handle. However, the variable becomes an invalid handle because the object it referred to no longer exists.
You can delete graphics objects explicitly using the delete function
or by closing the figure that contains the graphics objects. For example,
create a bar graph.
f = figure; y = rand(1,5); b = bar(y);
Close the figure containing the bar graph.
close(f)
The handle variables still exist after closing the figure, but the graphics objects no longer exist.
whos
Name Size Bytes Class f 1x1 104 matlab.ui.Figure b 1x1 104 matlab.graphics.chart.primitive.Bar y 1x5 40 double
Use isgraphics to determine
the validity of a graphics object handle.
isgraphics(b)
ans =
0You cannot access properties with the invalid handle variable.
h.FaceColor
Invalid or deleted object.To remove the variable, use the clear function.
clear h