我试图为fgoalattain的每次迭代保存一个文件。我每次打印都很好。但是使用savefig函数将每次迭代保存为完全相同的名称,这将删除先前的迭代数字。如何为每个生成唯一的已保存图形?
figure
%TEST diff 200, 100, 50 FOR TIMES
while diff > 200
iterations = iterations +1;
disp('running fgoalattain')
xz = fgoalattain(obj_Func,xz,goal,weight,[],[],[],[],zeros(x3,x3),ones(x3,x3),[],Options);
diff = sum(sum(abs(xz - a),2));
pcolor(xz)
drawnow
savefig('iterpic.fig');
end
答案 0 :(得分:2)
savefig的参数是文件名,由于它没有改变,你每次都只是覆盖同一个文件。
尝试类似:
savefig(sprintf('interpic-%d.fig',iterations));
将迭代编号附加到文件名。
答案 1 :(得分:1)
简单:使用计数器和sprintf
figure
%TEST diff 200, 100, 50 FOR TIMES
kk = 1; %//counter of files
while diff > 200
iterations = iterations +1;
disp('running fgoalattain')
xz = fgoalattain(obj_Func,xz,goal,weight,[],[],[],[],zeros(x3,x3),ones(x3,x3),[],Options);
diff = sum(sum(abs(xz - a),2));
pcolor(xz)
drawnow
kk = kk+1;
savefilename = sprintf('interpic%d.fig',kk); %//get filename
savefig(savefilename);
end
sprintf
根据指定的格式生成字符串,在这种情况下为interpic%d.fig
。 %d
是一个调用变量的参数,并将其表示为一个整数,在逗号之后,在这种情况下为kk
。