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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by