Deleting individual plots from a figure

88 ビュー (過去 30 日間)
Daniel Morgan
Daniel Morgan 2016 年 12 月 7 日
コメント済み: Daniel Morgan 2016 年 12 月 8 日
Hi everyone, i'm fairly new to matlab but I will try to explain my question in as much detail as possible. I'm creating a GUI that allows users to create either a line or a circle(specified by coordinates that they put into an inputdlg), and plot it to a graph. This is done with the use of two push buttons (create line, create circle). When each button is pressed, a function runs that creates the shape based on their input. As this can be used various times, I have not assigned the plot in either case to a variable so I cant just use the delete() function. I was wondering, If anyone knew any way of deleting plots from a figure(also based on user input). Im guessing it requires you to either label plots or access the array containing all plots on a figure? Any help would be greatly appreciated and if more information is needed I will provide, thanks, dan. (I've included an example of my create line function so you can see what im doing).
promt = {'Set your X(1) coordinate','Set your Y(1) coordinate', 'Set your X(2) coordinate', 'Set Y(2) coordinate', 'Name your line'}
title = 'Create Line';
lineno = 1;
hold on;
linedraw = inputdlg(promt, title, lineno);
x1 = str2num(linedraw{1});
y1 = str2num(linedraw{2});
x2 = str2num(linedraw{3});
y2 = str2num(linedraw{4});
name = linedraw{5};
plot([x1 x2] , [y1 y2], );
hold off;

回答 (1 件)

Adam
Adam 2016 年 12 月 7 日
The plot function returns an argument that is a handle to the plotted object. Use this and just call delete on it. i.e.
hPlot = plot([x1 x2] , [y1 y2], );
...
delete( hPlot );
though try to use a more meaningful name for whatever the specific plot is than hPlot!
  3 件のコメント
Adam
Adam 2016 年 12 月 8 日
If you plot multiple lines in a single plot instruction you will get an array of graphics objects handles out as the result - you can delete any single element or multiple elements from this array too.
Or if you make multiple calls to plot you can assign them yourself to an array:
hPlot(1) = plot([x1 x2] , [y1 y2], );
hPlot(2) = plot([x1 x2] , [y1 y2], );
hPlot(3) = plot([x1 x2] , [y1 y2], );
delete( hPlot(2) );
You do need to be careful, depending what you do with those handles afterwards though because you will still have a 3-element vector, you will just have the 2nd element of this being the handle to a deleted object. You can obviously choose to remove this element with
hPlot(2) = [];
if you wish, though obviously this will re-index all subsequent stored plots.
Daniel Morgan
Daniel Morgan 2016 年 12 月 8 日
Brilliant! Thanls for your help :-)

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

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by