如何多次保存相同的数字?

时间:2015-10-15 18:01:31

标签: matlab matlab-figure

我试图为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

2 个答案:

答案 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