how to save images in a for loop?

hey, I wrote a code to save a few images in a for loop in a cirtin folder,it won't work,please help
for beta=-1:1
...
...
...
xx=sprintf('C:\Users\Sagipc\Desktop\ImageLibrary\beta%g.jpg',beta);
imwrite(pic1,xx,'jpg');
end

 Accepted Answer

Star Strider
Star Strider on 9 Sep 2014
Simply stating the problem as ‘it won’t work’ doesn’t tell us much.
It looks as though it’s writing ‘pic1’ to every file you create. I would assume you intend a different result.
What are your image file names? How would you want to save them?

5 Comments

i want it to write for each of the loop indexes ,the image name as 'beta-1','beta0',beta1'. pic1 is an image that changes with every differet index of the loop,I didn't write because it isn't importent. my main goal is to save each pic1 in:C:\Users\Sagipc\Desktop\ImageLibrary with the name 'beta(index)',as a jpg. the error message that i get is:
Warning: Invalid escape sequence appears in format string. See help sprintf for valid escape sequences. > In Lipson3Improved at 21 ??? Error using ==> imwrite at 453 Can't open file "C:" for writing. You may not have write permission.
Error in ==> Lipson3Improved at 22 imwrite(pic1,xx,'jpg');
You have to use double backslants ‘\\’ in fprintf and sprintf to (1) not have them interpreted as control sequences by the print functions, and (2) get them to print out as single backslants ‘\’:
beta = 10; % Arbitrary
xx=sprintf('C:\\Users\\Sagipc\\Desktop\\ImageLibrary\\beta%g.jpg',beta);
produces:
C:\Users\Sagipc\Desktop\ImageLibrary\beta10.jpg
and no errors.
Or just use forward slashes . Windows is perfectly happy to work with forward slashes. It figures out what you meant just fine.
that was very helpful, thank u heaps!
My pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!