Clearing plot in GUI axes
조회 수: 5 (최근 30일)
이전 댓글 표시
I have 10 variables with data which I want to plot against time. I generated checkboxes with the corresponding name of the variable and axis in a figure. when the user checks a checkbox, the data corresponding to the variable it to be plotted with time series. I am using plot command in the checkbox callback function and was able to plot when it was checked. But when the checkbox is unchecked the plot still remains in the figure. I used cla command to clear the plot. The problem is when multiple checkboxes are selected and if one checkbox is unchecked all the plots are being cleared. I need to clear the plot only for which the checkbox is unchecked. kindly help... Thanks in advance
댓글 수: 3
Image Analyst
2022년 5월 30일
편집: Image Analyst
2022년 5월 30일
@Reza Lashani in the callback for each individual checkbox, clear the one axes that corresponds to that checkbox. For example in the callback for chkClearAxes1, have
if handles.chkClearAxes1.Value
cla(handles.axes1, 'reset');
end
Do it similarly for all the other checkboxes.
Reza Lashani
2022년 5월 30일
yes but this command clears all plotted diagrams in axes. there is already other diagrams plotted in the same axes, I just want to delete one of them after the checkbox is unchecked and plot it when it is checked.
I have tried such code: (The checkbox is unchecked at the begining)
function CheckBoxDiagram_Callback(hObject, eventdata, handles)
x = 0: 0.02: 1;
y = sin(x);
if get(handles.CheckBoxDiagram, 'value')
Diagram = plot(handles.axes1, x, y, 'r--');
else
delete(Diagram)
end
But I get this error:
Undefined function or variable "Diagram".
답변 (1개)
Brendan Hamm
2015년 8월 27일
You need only get the plot objects (or handles prior to 2014b) to delete that individual plot.
f = figure;
a = axes('Parent',f);
x = linspace(0,2*pi);
hold on
for k = 1:3
p(k) = plot(a,x,sin(x/k));
end
We can now just delete the individual plot object:
delete(p(1))
It seems you may have a check box for each possible plot, so you may want to just initialize a vector of plot objects:
p = gobjects(numOfPlots,1);
so that you can always assign the plot from the first check box to p(1) and from the nth checkbox to p(n).
댓글 수: 4
Brendan Hamm
2015년 9월 21일
Presumably you have replaced the line:
p=gobjects(length(txt),1);
with
p = zeros(length(txt),1);
as you mention you are using 2007b.
In this case if you do not assign to one of the elements of p, the value of this element remains 0. When you call:
delete(0)
you will get an error as 0 is used for the root (that is information from your graphics card pertaining to your display). You could place another conditional to ensure that p(index) ~= 0 before deleting in this case.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!