Numbering images using function 'websave'.

2 次查看(过去 30 天)
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!

采纳的回答

Rik
Rik 2017-11-16
编辑:Rik 2017-11-16
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 个评论
Rik
Rik 2017-11-16
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 2017-11-16
Thank you for your answer. Very much appreciated!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by