hclarke
hclarke

Reputation: 27

Plotting with pcolor in matlab

I'm new to Matlab and would like some hints on plotting arrays in pcolor. I have temperature, longitude and latitude data and would like to plot temperature (T) maps at different times. I've tried plotting arrays but doesn't work. T has four dimensions: lon, lat, depth and time. I just want to plot the surface temp, so I want to keep my array as T(:,:,1,:). To plot the temp at the first time iteration, what I do is:

T001=T(:,:,1,1);
pcolor(lon_rho, lat_rho, T001);

and to plot the second iteration:

T002=T(:,:,1,2);
pcolor(lon, lat, T002);

Which all works fine. However, I would like to generate all the plots for all time iterations and then put them into an animation.

I've tried something like:

pcolor(lon, lat, T(:,:,1,:))

which doesn't work. Can Someone help?

Thanks.

Upvotes: 0

Views: 415

Answers (1)

Siva Srinivas Kolukula
Siva Srinivas Kolukula

Reputation: 1251

YOu can run a loop to the length of time and use pcolor to plot the data, and save the animation into .gif file. You may check the below code:

h = figure;
axis tight
filename = 'myfile.gif';
for n = 1:length(t)    
    pcolor(lon_rho, lat_rho, T(:,:,1,n)) ;
    drawnow
    % Capture the plot as an image
    frame = getframe(h);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    % Write to the GIF File
    if n == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end
end

Upvotes: 1

Related Questions