How to detect if a plot has already existed?
10 次查看(过去 30 天)
显示 更早的评论
What I want to do is to plot my sampling stations on a map. The map is pre-plotted . I want to delete the previous sampling stations (h1 handle) if they already exist and then plot the new sampling stations.
Below is my code:
if isexist(h1)
delete(h1);
end
hold(app.mapPlot, 'on');
h1 = plot(app.mapPlot, lonlat(:,1), lonlat(:,2), 'o', 'color', 'b','markersize', 5);
Unfortunately, I got the below error:
Unrecognized function or variable 'h1'
Any idea on how to make this work?
0 个评论
采纳的回答
Joe Vinciguerra
2019-10-23
Try this... (note I'm using "exist" instead of "isexist")
clear % I'm starting with no 'h1'. This checks the "False" condition
if exist('h1')
delete(h1); % if 'h1' is a thing, delete it
end
h1=plot([5 8],[12 11]); % now there is an 'h1' either way
hold on; % I insertered this to match your application
pause(3); % wait a few seconds to verify it visually
% Let's do it again to check the "True" condition
if exist('h1')
delete(h1); % if 'h1' is a thing, delete it
end
h1 = plot([1 2],[2 4]); % still works
3 个评论
Joe Vinciguerra
2019-10-23
I'm not as familiar with the app designer, but I'll try to help.
Are you sure it's really not deleting? Or is it deleting and replotting the same data quick enough that you don't notice?
Try another command instead of "delete". Perhaps "close"?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!