Rename images using counter

3 ビュー (過去 30 日間)
Olu adroit
Olu adroit 2015 年 2 月 8 日
コメント済み: Olu adroit 2015 年 2 月 8 日
Hi All,
I have a matlab script with a loop that includes a python script. This python script generates an image file with the name 'A.tiff' for every run of the loop. The problem is that the new image generated overwrites the previous one but I really want all the images generated everytime the loop executes. I think of renaming the image file after its generated by adding a counter to the file name e.g. A_1, A_2, A_3, etc. I can use the 'movefile = (OldFileName, NewFileName)' but what lines of code can I use to insert a counter into the NewFileName argument?
Thanks for your help in advance.

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 2 月 8 日
Olu - you can use sprintf to create the new file name. Something like
for k=1:10
% call the python script to generate the A.tiff file
% create the new file name
newFileName = sprintf('A_%d.tiff',k);
% move the file
movefile('A.tiff',newFileName);
end
Try the above and see what happens!

その他の回答 (1 件)

Erik S.
Erik S. 2015 年 2 月 8 日
編集済み: Erik S. 2015 年 2 月 8 日
You can use num2str() to convert the loop index to a string. With strcat() you can combine 2 strings such as A and loopindex
for i = 1:n
loopindex = num2str(i);
filename = strcat('A',loopindex);
% Add you code
end
  1 件のコメント
Olu adroit
Olu adroit 2015 年 2 月 8 日
Thank you. It worked as weel

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by