Reputation: 1336
I'm trying to save a file for each iteration of the fgoalattain. I have it printing the figure each time just fine. But using the savefig function saves each iteration as the exact same name, which erases previous iterations figures. How do I generate a unique saved figure for each?
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
Upvotes: 1
Views: 105
Reputation: 18187
Simple: create unique filenames using a counter and 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
generates a string based on the format specified, i.e. interpic%d.fig
in this case. The %d
is an argument to call a variable and represent it as an integer, which is after the comma, being kk
in this case.
Upvotes: 1
Reputation: 143
The argument to savefig is the file name, and since that's not changing, you're just overwriting the same file every time.
Try something like:
savefig(sprintf('interpic-%d.fig',iterations));
to append the iteration number to the file name.
Upvotes: 2