Update plot data when HOLD ON is active

조회 수: 8 (최근 30일)
Matthew Saul
Matthew Saul 2014년 12월 18일
답변: Image Analyst 2014년 12월 30일
I need to update 1 set of data on a GUI figure as a slider variable changes. I have HOLD ON active as I have other data on the plot I need to keep. I get a new curve when i move the slider, but I want the old curves associated with the previous slider positions to be cleared from the plot. Any help would be greatly appreciated.

답변 (2개)

Image Analyst
Image Analyst 2014년 12월 30일
The problem is that when the slider is moved, and you plot the data and get the handle, the next time you move the slider, that handle is lost/forgotten. So you need to retain the handle to the curve from one call to the next. So you can either make the handle global or persistent so that it will be there next time. So your slider callback might look something like this:
global plotHandle;
axes(handles.axesPlot); % Switch current axes to this particular one.
hold off;
if exist('plotHandle', 'var')
% If this handle already exists
% Delete the handle to the old curve which will erase the curve.
delete(plotHandle);
end
% Now plot the new data.
hold on; % Don't blow away other curves.
plotHandle = plot(x, y); % Plot your new, updated curve.

Geoff Hayes
Geoff Hayes 2014년 12월 18일
Matthew - if you keep track of the handles to the graphics that you plot on the GUI axes, you can then delete them at a later time. For example,
figure;
% we are going to plot the sine and cosine curves
x = linspace(-2*pi,2*pi,1000);
y1 = sin(x);
y2 = cos(x);
% plot the data and save the handles for each
hPlot1 = plot(x,y1);
hold on;
hPlot2 = plot(x,y2);
Now the figure should display both curves. If you want to delete the second one, just try
delete hPlot2;
Try adapting the above for your code and see what happens!
  댓글 수: 2
Matthew Saul
Matthew Saul 2014년 12월 19일
Hi Geoff, Thanks for your answer. For the scenario you mention it works fine, however it does not work in my GUI implementation unfortunately. Referencing your example, what I would want to do would be to keep hPlot1 on the figure, and then as the variable y2 updates with the GUI slider movement, then I want hPlot2 to update on the figure (while removing previous plots of hPlot2 for previous slider positions). Any ideas? Thanks,Matt
Geoff Hayes
Geoff Hayes 2014년 12월 19일
Matthew - without seeing how you have implemented your code, it is somewhat difficult to suggest ideas. Are you using plot to draw your curves? If so, and you just want to replace the previous data with something else, then you could do something like
% plot the sine curve
hPlot2 = plot(x,y1);
% replace with the cosine curve
set(hPlot2,'XData',x, 'YData',y2);
If this method doesn't work, then please provide some details as to why it won't.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by