How not to delete one element of a figure when using cla?

I have a map with coastlines and sampling stations that are added from other scripts using the command plot. When I move on to the next data set, I need to initialize the plot. What I want specifically is to keep the coastline plots (handle 'h' as below), but clear everything else (e.g., the sampling stations plotted on the map).
cla(app.mapPlot2);
h = patch(app.mapPlot2, M2(:,1), M2(:,2), [0.62,0.83,0.50]);
My current program as above would delete everything including coastalines. Is there a way I can delete everything but the 'h' handle?
Many thanks!

 採用された回答

Walter Roberson
Walter Roberson 2020 年 2 月 8 日
編集済み: Walter Roberson 2020 年 2 月 8 日

0 投票

No. cla always deletes the children of the axes.
You could put a copy of the object to keep into an axes in an invisible figure and copyobj() it instead of recreating it. However you mentioned mapping toolbox and one thing that cla does is reset back to a regular axes instead of a mapping axes and trying to copyobj a mapping child into a regular axes could easily be a problem. Like it would be easier to copyobj the entire axes.

5 件のコメント

Leon
Leon 2020 年 2 月 8 日
編集済み: Leon 2020 年 2 月 8 日
Very cool. Many thanks!
One of the reasons I want to keep the coastline is because it is quite a laborous process with a loop to plot them:
for i=1:200;
M2 = [];
for j=1:m;
if M(j,3)==i;
M2=[M2;M(j,1:2)];
end
end
patch(app.mapPlot2, M2(:,1), M2(:,2), [0.62,0.83,0.50]);
end;
In this case, how can I save the loop items into one graphic object?
BTW, is it possible to save the copyobj value into a *.mat file?
Walter Roberson
Walter Roberson 2020 年 2 月 8 日
After the above, you can save app.mapPlot2 into a variable and save() the variable, and you can load() again.
But what I would suggest in your case is creating a second axes that is over top of the first axes, with 'background', 'none' so it is transparent, and do all your temporary drawing in it. You can cla() it without affecting the axes you drew the coastline into.
Leon
Leon 2020 年 2 月 8 日
編集済み: Leon 2020 年 2 月 8 日
Thank you so much!
I have no idea how to let the 2nd axes to be on top of the first axes.
Is my current axes app.mapPlot2? If I create another one called app.mapPlot2b.
How do I overlay them?
Walter Roberson
Walter Roberson 2020 年 2 月 8 日
If you set the Position of the second axes to be the same as the Position of the first axes, then the second one will be on top of the first provided it was drawn later. You can use uistack() to force a particular relative position to be certain.
Leon
Leon 2020 年 2 月 8 日
Got you. Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Stijn Haenen
Stijn Haenen 2020 年 2 月 8 日

0 投票

Maybe you can delete one of the 'Children' of the figure.
type:
h.Children
and check if there is any data that can be deleted.
or
h.Children.Children

1 件のコメント

Leon
Leon 2020 年 2 月 8 日
Thanks for pointing me to that direction.
To clarify, I do not want to delete anything about the h handle. I just want to delete the sampling station plots from other scripts with unknown handles.
Thanks.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で 2-D and 3-D Plots についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2020 年 2 月 8 日

コメント済み:

2020 年 2 月 8 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by