Numbering images using function 'websave'.

2 views (last 30 days)
Hi
I created a cell (10 x 1) and stored 10 web URLs in the cell.
The URL would look something like 'http://juliandance.org/wp-content/uploads/2016/01/RedApple.jpg'
I would like MATLAB to read each line of URL in the cell and try to save it as a .jpg file using function 'websave'.
However, not all 10 URLs are valid URLs. Some of it can be invalid and does not lead to any image.
What I would like to do is:
Try to save the first URL, if it can be saved as a .jpg file, name it '01.jpg'. Then move on to the second URL, if it can be saved, name it '02.jpg'; if it can't be saved, move on to the third URL, if it still can not be saved, move on to the fourth URL...and so on and so forth.
If out of 10 URLs it only successfully saved 5 images, regardless the 5 working URLs might be the 1st,3rd,5th,7th and 9th URL, I would still like to name the image in a continuous number from 1-5, rather name them 01.jpg, 03.jpg, 05.jpg, 07.jpg and 09.jpg.
So what is the logic relationship between the URL number and the image number?
Many thanks!

Accepted Answer

Rik
Rik on 16 Nov 2017
Edited: Rik on 16 Nov 2017
This is what try and catch are for.
filenamecounter=0;
for n=1:10
try
filenamecounter=filenamecounter+1;
filename=sprintf('%s%02d.jpg',explicit_path,filenamecounter);
%call to websave here
catch
filenamecounter=filenamecounter-1;%undo increment on error
end
end
Ideally you would also do some error checking by looking at the exception, so you can verify that it is indeed the error you expected.
  3 Comments
Rik
Rik on 16 Nov 2017
Yes, you are right. These are the type of mistakes you will keep making if you want to go too fast. I'll edit my answer.
Salad Box
Salad Box on 16 Nov 2017
Thank you for your answer. Very much appreciated!

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!