Update plot data when HOLD ON is active

8 views (last 30 days)
Matthew Saul
Matthew Saul on 18 Dec 2014
Answered: Image Analyst on 30 Dec 2014
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.

Answers (2)

Image Analyst
Image Analyst on 30 Dec 2014
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 on 18 Dec 2014
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 Comments
Matthew Saul
Matthew Saul on 19 Dec 2014
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 on 19 Dec 2014
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.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!