In MATLAB, how does one clear the last thing plotted to a figure?

和自甴很熟 提交于 2019-11-29 13:37:06

If you know before plotting that you want to remove it again later, you can save the handle returned by plot and delete it afterwards.

figure;
h1 = plot([0 1 2], [3 4 5]);
delete(h1);

Try

items = get(gca, 'Children');
delete(items(end));

(or maybe delete(items(1)))

The answer that @groovingandi gives is the best way to generally do it. You can also use FINDALL to find the handle based on the properties of the object:

h = findall(gca, 'type', 'line', 'color', 'k');
delete(h);

This searches the current axes for all line objects (plot produces line objects) that are colored black.

To do this on, say, figure 9, you need to find the axes for figure 9. Figure handles are simply the figure number, so:

ax = findall(9, 'axes');
h = findall(ax, 'type', 'line', 'color', 'k');
delete(h);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!