フィルターのクリア

Numbering images using function 'websave'.

2 ビュー (過去 30 日間)
Salad Box
Salad Box 2017 年 11 月 15 日
コメント済み: Salad Box 2017 年 11 月 16 日
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 Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by