今天介绍一下如何用MATLAB生成gif文件。
filename
是我想要保存的文件名,包括我想要保存的文件的路径。
1 2 3 |
filename = strcat(path0,'\',taskcode,'_',case_code,... '_dynamics_of_i.gif'); |
用figure
命令创建一个画框。idx
是我想要作出gif图的index,通过idx
更新对gif进行迭代。然后我通过subplot
对我之前已经计算好的数据进行作图。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
f1 = figure; for idx = 1:length(target_2) if strcmp(case_code,'2_1') alp = target_2(idx); elseif strcmp(case_code,'2_2') m = target_2(idx); elseif strcmp(case_code,'2_3') s = target_2(idx); elseif strcmp(case_code,'2_4') r = target_2(idx); elseif strcmp(case_code,'2_5') g = target_2(idx); elseif strcmp(case_code,'2_6') T = target_2(idx); elseif strcmp(case_code,'2_7') bT = target_2(idx); end time=(1:bT); % computation part % H_t ax1=subplot(3,3,1); Ht = cell_Ht{idx}; plot(time,Ht(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$H_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax2=subplot(3,3,2); Mt = cell_Mt{idx}; plot(time,Mt(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$M_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax3=subplot(3,3,3); It = cell_It{idx}; plot(time,It(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$I_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax4=subplot(3,3,4); Rt = cell_Rt{idx}; plot(time,Rt(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$R_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax5=subplot(3,3,5); Ct = cell_Ct{idx}; plot(time,Ct(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$C_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax6=subplot(3,3,6); Wt = cell_Wt{idx}; plot(time,Wt(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$W_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); % ax7=subplot(3,3,7); It = cell_It{idx}; Wt = cell_Wt{idx}; temp = It./Wt; plot(time,temp(time(1):time(end)),'LineWidth',2); xlabel('$t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); ylabel('$I_t/W_t$','interpreter',... 'Latex','FontSize',18,'FontWeight','bold'); xlim([time(1),time(end)]); str_leg=sprintf(strcat(str_var,str_fmt),target_2(idx)); legend(str_leg,'Location','northeastoutside'); grid(ax1,'on');grid(ax2,'on');grid(ax3,'on');grid(ax4,'on'); grid(ax5,'on');grid(ax6,'on');grid(ax7,'on'); set(gcf,'Position',get(0,'Screensize'));%Maximize figure |
drawnow
命令:Update figures and process callbacks
更新数据和处理回调
通过im = frame2im(frame)
命令,返回图像数据和相关的色彩表。输出的色彩表是一个三列的矩阵,矩阵的每一行是RGB三元组值。该RGB三元组值定义色彩白哦的颜色。函数getframe
和im2frame
创建一个电影帧(Movie Frame)。
再通过[X,map]=rgb2ind(RGB,n)
命令将色彩表生成索引图像X
和色彩表map
。rgb2ind
使用最小方差量化和抖动方法(minimum variance quantization and dithering)将RGB图像转换为索引图像X
。map
最多包含n
种颜色。n
必须小于或等于65,536。
imwrite(A,map,filename)
将图像数据A
和色彩表map
写入文件filename
。其中'Loopcount',inf
使动画不断循环,'WriteMode'
有'append'
和'overwrite'
两种。DelayTime
是定义相邻两张图像的延迟时间。详情可以查看MATLAB help文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
% GGIF GENERATION PART drawnow frame = getframe(1); im = frame2im(frame); [A,map]=rgb2ind(im,256); if idx==1 imwrite(A,map,filename,'gif','Loopcount',inf,'DelayTime',0.5); else imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.5); end end close(f1); |
生成的gif文件如下所示:
You must be logged in to post a comment.
Be the first to comment.