简体   繁体   中英

Animate surface plot in Matlab

I have a matrix representing height of a 10x10 square grid over time. The height is updated in a for loop over the rows and columns. My attempt was to simply put the surf(height) within the this loop, with a 0.1 second pause between plots, because this is how I done it with a 2-d plot. How can I make this work?

I think the best way is to update the data directly from your surface plot.

To do so, assign it a handles (eg. named hSurf ) during its creation and then update the ZData property using, for example,

set(hSurf,'ZData',SomeValues)

Sample code:

clear
clc
close all

figure(1)

%// Generate data
Z = peaks(25);

%// Create handles to access/modify data.
hSurf = surf(Z);

k = 0;

%// Set up name to create animated gif.
filename = 'AnimateSurf.gif';

%// Just a loop
while k < 10

    %// IMPORTANT part. Update the Z data
    set(hSurf,'ZData',k*Z);

    %// Set limits so the graph looks nice.
    zlim([-80 80])
    drawnow

    %// Capture frame to write to gif.
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    if k == 0;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end

    pause(.15)

    k = k+1;
end

And output:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM