matlab学习笔记(4): matlab画图04-gif|avi


目录:
  • 接下来我分享我之前做的一些实例操作,主要使利用matlab做gif动图和avi动图

  • 做动图的目的是为了能够再ppt中插入我的科研成果,让别人更好的理解

  • 话不多说,先看第一个实例,之后还有两个实例

1 数据说明

数据是30多年的土地利用分类数据,tif格式

2 展示

为了个更好的展示土地类型的时空变化,其中一个是空间变化,一个是土地利用类型的面积变化

3 结果

4.详细的代码

clear;clc;

filename = 'img/' ;

fn=dir([filename 'site4_*.tif']);

fn=struct2cell(fn);

fn=fn(1,:)';

img  = cell(length(fn),1);

for i = 1: length(fn)

[A,~]= geotiffread([filename  fn{i,1}]);

img{i,1} = A;

mangrove = nansum(nansum(A(:,:,1)));

water =  nansum(nansum(A(:,:,4)));

spartina =  nansum(nansum(A(:,:,2)));

tidal =  nansum(nansum(A(:,:,3)));

fn{i,2} = mangrove;

fn{i,3} = water ;

fn{i,4} = spartina;

fn{i,5} = tidal ;

end

B(1,1:5)={ 'site_time','mangrove','water','spartina','tidal'};

B= [B;fn];

data = cell2mat(B(2:end,2:end))*900/1e6;

for j= 1:32

cl=[ 1 0 0;0 0.5 0;0.99 0.92 0.80;0.13 0.95 0.95;0 0 0;];

B=flipud(img{j,1});

figure(1);

set(gcf,'Position',[50,80,1900,800]);

clf;

ax1=  subplot(1,2,1);

for i=1:4

[m,n]=find(B(:,:,i)>0);

m1=m-1;

n1=n-1;

A1=[n m;n1 m1;n m1;n1 m];

B1=reshape((1:size(A1,1))',[],4);

hold on

z(i)=patch('Faces',B1,'Vertices',A1,'FaceColor',cl(i,:),'LineStyle', '-', 'EdgeColor',cl(i,:),'EdgeAlpha',0.5,'FaceAlpha',0.5);

end   % 直接用patch画图为了更好的加上图例和颜色控制

set(ax1,'xlim',[-8 size(B,2)+8],'ylim',[-10 size(B,1)+20],'FontSize',16,'FontWeight','bold')

set(ax1,'xtick',[],'ytick',[])

text(size(B,2)/2*0.7,size(B,1) +10,[num2str(j+1985),'年'],'FontSize',16,'FontWeight','bold')

box on

legend([z(2),z(1),z(3),z(4)],{'slSpartina alterniflora','Mangrove forest',...

'Intertidal mudflat','Water body' },'FontSize',...

12,'FontWeight','Bold','box','off'); %加上图例

ax2 = subplot(1,2,2);

x = (1985 +1):(1985+j);

y= data(1:(j),[1,3]);

h= plot(x,y);

set(ax2,'xlim',[1985 2018],'ylim',[0 max(data(:,3))+0.5],'FontSize',16,'FontWeight','bold')

set(ax2,'xtick',[1985:5:2018 2018])

set(ax2,'ytick',0:0.5:max(data(:,3))+0.5)

%     set(ax2,'ytick',[0,5,10,15,20])

xlabel('year','FontSize',16,'FontWeight','bold')

ylabel('area/km^2','FontSize',16,'FontWeight','bold')

box on

grid on

set(h(1) , 'linestyle','-','LineWidth',2,'Color',[1 0 0])

set(h(2), 'linestyle','-','LineWidth',2,'Color',[0 0.5 0])

legend([h(2),h(1)],{'slSpartina alterniflora','Mangrove forest'},'FontSize',...

12,'FontWeight','Bold','box','off');

f(j) = getframe(gcf);

frame=getframe(gcf);

im=frame2im(frame);%制作gif文件图像必须是index索引图像

[I,map]=rgb2ind(im,256);

if j == 1;

imwrite(I,map,'test03_area.gif','gif', 'Loopcount',inf);

else

imwrite(I,map,'test03_area.gif','gif','WriteMode','Append');

end

end

movie2avi(f,'test03_area.avi','fps',5,'quality',100,'compression', 'None');