MATLAB: Trying to change name of image file in for loop – use %s

imagepsychtoolbox

Hi,
I'm trying to create a for loop, where I gather screen shots and save them (trial 1, trial 2…, etc.). I tried to use the following:
imwrite(imageArray, '%s.jpg',trial)
to create different names for each image, as the loop continues. However, this didn't work. Is there any other way to do this, other than "if trial 1….else if trial 2, etc.?"
I am using PTB, if that's useful.
Thank you!

Best Answer

Use the filename as the following
for ii = 1:10
filename = ['trial' num2str(ii) '.jpg']
imwrite(I,filename)
end
It looks like what you were trying to do was use sprintf inside of imwrite. This would work too, but it a little more power than you need:
filename = sprintf('trial%i.jpg',ii)
Related Question