most efficient way of plotting surf/mesh from live data

8 views (last 30 days)
For demo purposes, I've made a little class that streams live data from some other software I programmed via TCP and I'd like to plot the incoming data as a 3D mesh or surface. The code looks something like this:
c = MyClient( 'localhost', 5555 );
ButtonHandle = uicontrol('Style', 'PushButton', ...
'String', 'Stop loop', ...
'Callback', 'delete(gcbf)');
while true
if ~ishandle(ButtonHandle)
fprintf( 'Loop stopped by user\n' );
[ret,m] =;
if( ret == true )
surf( m );
zlim([0 1]);
pause( .05 );
I would like the value range to be constant, so I inserted zlim. This works fine, basically. Now when I add a colorbar, everything seems to become really slow. I'm not sure how the plotting works internally, but I thought maybe it's not meant to be used this way. My hypothesis is that the plot is created from scratch each loop. If that's true, is there a way of reusing the mesh and just update the values? If not, how would I do something like that in MATLAB?
I guess my overall question is what is a recommended way of plotting live data? I also noticed that when 3D rotating the plot, it jumps back to initial perspective all the time, so ideally I would like to have a mesh I can just update with new values. Is that even possible?

Accepted Answer

jonas on 17 Aug 2020
Edited: jonas on 17 Aug 2020
It is correct that creating a new surface object every time is slowing you down. Updating the existing object is much faster. Compare these two
[X,Y] = meshgrid(1:100,1:100);
Z = rand(size(X));
for i = 1:10
t1 = toc;
h = surf(X,Y,Z);
for i = 1:10
Z = rand(size(X));
h.ZData = Z;
t2 = toc;
ans =

More Answers (0)

Community Treasure Hunt

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

Start Hunting!