Update axes in Matlab GUI

1 vue (au cours des 30 derniers jours)
David
David le 6 Mai 2013
Hey guys I´ve got a question. I have a m-File called Test-Gui.m In this m file I have a simple loop
global x
x=0;
for k=1:1000
x=x+1;
pause(0.001)
end
With this small loop I want to draw a plot in my GUI (Test_GUI.m) and update this plot everytime with the new value for x.
function axes1_CreateFcn(hObject, eventdata, handles)
global x
plot(x,x^2,'r');
I have tried drawnow() but it doesn´t work at all. Do someone know any possibility to update my figure in the GUI with the changing variable x.

Réponse acceptée

Gijs van Oort
Gijs van Oort le 6 Mai 2013
Maybe you could try a timer. Have a look for 'Timer' in Matlab's documentation. There are many examples and it is not hard at all!
Furthermore,
  • It doesn't make sense to update your graph 1000 times per second (which is what you seem to try above). 30 Times per second is usually enough to get the impression that the animation is fluently.
  • The example above is a little silly, x is a scalar, and plotting a scalar doesn't really make sense (or do you want the plot to act like a moving point? If so, consider using the function comet (it may do exactly what you want).
  • In order to improve performance dramatically, you should do a plot command once and then update the data of the plot. Otherwise, you will never get a nice animation. It could be the case that this point alone solves your drawnow problem. Using this, the above code would be:
global x
global P % Handle to the plot
x=0;
P=plot(0,0,'r'); % initialize plot. The data does not matter
for k=1:1000
x=x+1;
pause(0.001)
end
function axes1_CreateFcn(hObject, eventdata, handles)
global x
global P
set (P,'xdata',x,'ydata',x.^2) % Note the .^ in order to do element-wise power instead of matrixwise power

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by