Update plot data when HOLD ON is active
8 次查看(过去 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.
0 个评论
回答(2 个)
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.
0 个评论
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 个评论
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!