Faster way than findobj to find and delete plots in a GUI interface

6 views (last 30 days)
Hi everyone,
I am currently working on a gui interface, in which I am plotting a 3D animation of vehicles within a loop. At each iteration, i plot several items, such as the posiiton of each vehicle, their velocity vectors, the vehicles themselves, etc..
Some of these items need to stay on the figure, but most of these need to be deleted after each iteration.
The best way to do this that i have found to give the plot i need to delete a particular display name, and then delete the corresponding items at the end of the loop :
quiverEnemyVehicle = quiver3(enemy_x, enemy_y, enemy_z, enemy_u, enemy_v,enemy_w, 5, 'DisplayName', 'deleteAfterRefresh');
...
delete(findobj(handles.axes1, 'DisplayName', 'deleteAfterRefresh'));
However, as my simulation is quite long (about 3000 iterations of the loop), the longer it goes the slower the function findobj gets, making the display really slow.
I have tried other methods, like assigning each plot to a variable in my handles, and then trying to delete it :
quiverEnemyVehicle = quiver3(enemy_x, enemy_y, enemy_z, enemy_u, enemy_v,enemy_w, 5);
field = sprintf('plot_%i', handles.simulation.plot_index);
handles.simulation.(field) = quiverEnemyVehicle;
handles.simulation.plot_index = handles.simulation.plot_index + 1
...
for j=1:handles.simulation.plot_index
plot_name = sprintf('handles.simulation.plot_%i', j);
delete(plot_name);
end
However this solution doesn't seem to be working, as the corresponding items do not disappear from my GUI interface.
Do you have any idea of the best way to do that ?
Thanks,
Lilian
  2 Comments
Lilian Darracq
Lilian Darracq on 25 Apr 2017
Sorry to insist, but for some reason my question was flagged as potential spam, so it disappeared from browse, anyone here has a piece of advise for that matter ?
Cheers
Jan
Jan on 25 Apr 2017
@Lilian: This question is not spam. I assume the flagging was an accident. The team of editors can and will fix the problem.

Sign in to comment.

Accepted Answer

Adam
Adam on 25 Apr 2017
That output argument that you assign as:
quiverEnemyVehicle = quiver3(enemy_x, enemy_y, enemy_z, enemy_u, enemy_v,enemy_w, 5, 'DisplayName', 'deleteAfterRefresh');
should be deleted directly as
delete( quiverEnemyVehicle )
so long as you keep hold of the handle. This is by far the best way to deal with any graphics objects. Never use findobj or findall unless you are desperate!
You can store graphics handles in an array rather than in a struct too which should be quicker. I'm not sure I understand what you mean by 'the corresponding items do not disappear from my GUI interface' though. If you are deleting the correct handle then it should disappear. Are you sure your sprintf evaluates to a valid object?

More Answers (2)

Jan
Jan on 25 Apr 2017
In your code:
for j=1:handles.simulation.plot_index
plot_name = sprintf('handles.simulation.plot_%i', j);
delete(plot_name);
end
plot_name is a string and you try to delete it afterwards. This will e.g. delete a file with this name, if it exists in the current folder, so be careful with such tricks. But you do not want to delete the contents of the string, but the value of the variable:
for j=1:handles.simulation.plot_index
plot_name = sprintf('plot_%i', j);
delete(handles.simulation.(plot_name));
end
Even if this solves the problem, don't do this, because it is too indirect to be efficient. See Adam's better suggestions: Store the handles in a vector, such that you can use an index to address single elements. This is better than creating a bunch of struct fields using an index hidden in the field name.

Lilian Darracq
Lilian Darracq on 26 Apr 2017
Edited: Lilian Darracq on 26 Apr 2017
Thanks, i tried to store the differents plots into an array, but it did not work, as i tried to store them in to a classical zeros/ones array.
I just discover the gobjects function, which i thought would do the trick. I am almost there, but i have one more issue i need to fix... This is what i am doing right now :
% Beginning of the loop
handles.simulation.plots = gobjects(0);
handles.simulation.plot_index = 1;
% Calling the function in which i plots the different items;
draw_graphics(hObject, handles);
% In draw_graphics
handles.simulation.plots(handles.simulation.plot_index) = quiver/plot3(...);
handles.simulation.plot_index = handles.simulation.plot_index + 1;
...
% At the end of draw_graphics
drawnow;
pause(0.1)
delete(handles.simulation.plots)
I have two problems :
  • Either i do as written above, but drawnow is taking more and more time to plot after each iteration, and ultimately becomes way too slow for what i am doing, but if i don't use drawnow/pause, since i use the delete(handles.simulation.plot) right at the end of draw_draphics, it doesn't even display the plots on the screen.
  • If try to pass the handles.simulation.plots to the main function with guidata(hObject,handles), and try to delete it at the beginning of the loop, before the call to draw_graphics. But if i do this, when i look at handles.simulation.plots right after the call to draw_graphics, it is still a 0X0 GraphicPlaceholder, and thus i can't delete it.
I am really a beginner in GUI interface, and I feel like i am missing something in the concept of handles and i am doing it the wrong way.
Do you guys mind helping me one more time ? ;)
Cheers,
Lilian
  1 Comment
Adam
Adam on 26 Apr 2017
Are your number of vehicles fixed within a loop? If so the best way to deal with plots is to update the data in an existing plot rather than keep deleting and plotting a new one. Recreation of graphics objects can be quite an expensive operation.
I'm not really familiar with a quiver plot, but I assume it has similar properties to e.g. a line where you would plot it once as
hLine = plot( xData, yData );
then later on when your data changes and you may have newXData and newYData you can just use
set( hLine, 'XData', newXData, 'YData', newYData )
and any other properties that also need updating. This is far more efficient as you create each graphics object just once and then keep updating those same graphics objects.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!