Lagging with updating a Figure

11 views (last 30 days)
Kevin
Kevin on 10 Mar 2015
Answered: Image Analyst on 12 Mar 2015
I am having an issue when it comes to a figure I have in a GUI. I have it so that an filled object updates its position and moves around the plot based on the input from an accelerometer that is hooked up to my computer. I have it moving around in the way I want however after not very much time the figure begins to lag more and more. I havent yet found a way to fix this issue. If you have an ideas or ways to fix this your help would be much appreciated.
  2 Comments
Stephen23
Stephen23 on 10 Mar 2015
Edited: Stephen23 on 10 Mar 2015
How are you plotting this? What code are you using? There a many ways to plot data using MATLAB, and there are also many ways to access and change that plotted data, so how do you expect us to know what you are doing if you do not give us any information at all?
There are lots of talented people here who volunteer their time to help others, but they can't read minds and they can't read your computer screen. Please help them by reading this, and following its advice:
In particular you can upload code using the paperclip button, and please explain exactly how you are using/calling your code, and what you expect it to do.
Kevin
Kevin on 10 Mar 2015
handles.E = 1;
handles.N = 1;
while strcmp(get(handles.Start,'String'),'STOP');
[gx,gy] = readAcc(handles.accelerometer, handles.calco);
rawmag=sqrt(gx^2+gy^2);
if strcmp(get(handles.DifficultyFilter,'String'),'EASY');
GX = handles.alpha*gx;
GY = handles.alpha*gy;
filtmag=(1-handles.alpha)*handles.magfiltdata(end)+handles.alpha*rawmag;
else
GX = handles.alphaHARD*gx;
GY = handles.alphaHARD*gy;
filtmag=(1-handles.alphaHARD)*handles.magfiltdata(end)+handles.alphaHARD*rawmag;
end
axes(handles.Backdrop);
r = .5;
t = linspace(0,2*pi);
handles.E = handles.E+GX/2;
handles.N = handles.N-GY/2;
fill(handles.E + r*cos(t),handles.N + r*sin(t),'y');
drawnow
guidata(hObject, handles);
end
here readAcc is a function that reads and records the accelerometer data. both handles.alpha and handles.alphaHARD are scalars in order to filter the data somewhat.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 11 Mar 2015
Edited: Stephen23 on 11 Mar 2015
Although you don't give any information about the figure and axes properties (e.g. use of hold, the DrawMode or NextPlot properties), based on the information that you gave it may be due to the repeated calling of fill:
fill(handles.E + r*cos(t),handles.N + r*sin(t),'y');
which creates a new patch object on every call. This might mean that the axes are slowly filling up with patch objects, or that MATLAB is somehow trying to keep track of every patch object you have created (i.e. a history). Rather than create a new patch object on every iteration, consider instead creating the patch object before the first iteration, and then using set to update its properties. You will need to use the patch handle for this:
h = fill(...);
and read the patch properties documentation and decide which properties you wish to update on each iteration.
In my experience updating patch/line properties does not result in lagging pictures with the iteration progress, and is a conceptually neater way of programming than creating a new patch object on every iteration.
  4 Comments
Kevin
Kevin on 12 Mar 2015
When I go about trying to get it to work this way. I specified the fill outside of the while loop and then inside the while loop I have the set functions as
set(h,'XData',handles.E,'YData',handles.N);
When I did this my circle disappeared off of the screen and the lag starts to kick in a few seconds later.
Kevin
Kevin on 12 Mar 2015
I ended up getting it to work another way where I have the fill command in the while loop like this
r = .5;
t = linspace(0,2*pi);
handles.E = handles.E+GX/2;
handles.N = handles.N-GY/2;
handles.W = fill(handles.E + r*cos(t),handles.N + r*sin(t),'y');
hold on
............ and at the end of my while loop
pause(.01);
delete('handles.Workplease');
hold off
This eliminated the lag for me so I figured I would let you know. Thank you for your assistance with this. I will try and mess around with the set function in order to understand it better

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 12 Mar 2015
I think Stephen is probably right. You're using up more and more memory and it slows down. I know that, in older versions of MATLAB, when I would repeatedly call imshow() it would store all those images, just covering up the one before, using up more and more memory until it ground to a halt. I could call cla or cla('reset') before I put stuff into the axes and that seemed to fix it with no slowdown anymore. Try clearing out old clutter from your axes by calling cla and see if that helps.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!