Deleting individual plots from a figure
조회 수: 88 (최근 30일)
이전 댓글 표시
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;
댓글 수: 0
답변 (1개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!