How to update axes in a GUI properly in a while loop

15 views (last 30 days)
Hello there,
Im currently trying to animate some data in a GUI. Since i interrupt the animation by hand i created an endless while loop updating all 5 axes like this
while true
plotting_index = plotting_index+1;
axes(handles.axes1)
scatter3(scatter data);
axis([xmin xmax ymin ymax zmin zmax])
view(azimuth,elevation);
axes(handles.axes2)
scatter3(scatter data);
axis([xmin xmax ymin ymax zmin zmax])
view(azimuth2,elevation2);
.
.
.
interrupting condition
end;
Since Im updating 5 Axes this way, the plotting gets pretty slow. Matlab recommended initializing the axes ouside the loop, but then I dont know how to assign the Data1(view,axes_lim) for axes1 and Data2 for axes2...etc. Coueld someone help me on that?
Thanks in advance ! :)

Accepted Answer

Jan
Jan on 3 Apr 2016
Remove the "axes(handles.axes1)" lines and use:
scatter3(..., 'Parent', handles.axes1);
Or even better: Create the scatter plot once only and adjust the data afterwards:
scatterH1 = [];
while true
...
if isempty(scatterH1)
scatterH1 = scatter3(scatter data);
else
set(scatterH1, 'XData', ..., 'YData', ..., 'ZData', ...);
end
...
end
  1 Comment
Mr Anderson
Mr Anderson on 3 Apr 2016
Thank you so much! Setting the scatter3 once and just updating the data the following runs through the loop is really much faster.
You really helped me a lot! Thanks again :)

Sign in to comment.

More Answers (2)

Mr Anderson
Mr Anderson on 3 Apr 2016
My final code for anyone who comes across a similar problem. I used Jan Simons approach and manipulated it for my uses, especially for the view and axis settings.
scatterA1 = [];
scatterA2 = [];
...
while true
...
if isempty(scatterA1)
scatterA1 = scatter3(scatter data...'Parent',handles.axes1);
set(handles.axes1,'view',[azimuth elevation]);
axis(handles.axes1,[xmin xmax ymin ymax zmin zmax]);
else
set(scatterA1, 'XData', ..., 'YData', ..., 'ZData', ...,'CData',...);
end
if isempty(scatterA2)
scatterA2 = scatter3(scatter data...'Parent',handles.axes2);
set(handles.axes2,'view',[azimuth2 elevation2]);
axis(handles.axes2,[xmin xmax ymin ymax zmin zmax]);
else
set(scatterA2, 'XData', ..., 'YData', ..., 'ZData', ...,'CData',...,'CData',...);
end
...
end

Van Thai Pham
Van Thai Pham on 31 Mar 2019
why don't you post true code?
set(scatterA2, 'XData', ..., 'YData', ..., 'ZData', ...,'CData',...,'CData',...);
does not work.
  1 Comment
Jay
Jay on 28 Aug 2019
the "..." allows for a new line.
type it in like this
set(scatterA2, 'XData', ...
, 'YData', ...
, 'ZData', ...
,'CData',...
,'CData',...
);

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!