필터 지우기
필터 지우기

How to write script for multiple files

조회 수: 1 (최근 30일)
Hosup Song
Hosup Song 2016년 7월 1일
댓글: Walter Roberson 2016년 7월 1일
so I have my files named 'abcd001' ~ 'abcd999' I'm trying to write a script using for loop to perform some function on these files. How do I write the code so that it will automatically read each file? I thought about doing something like
for i = 1:999
TIFFdata=Tiff('abcd'num2str(i)'.tif','r');
firstlevelTIFF'i'=TIFFdata.read();TIFFdata.nextDirectory();
secondlevelTIFF'i'=TIFFdata.read();TIFFdata.nextDirectory();
thirdlevelTIFF'i'=TIFFdata.read();
end
but that wouldn't work because it would become 'abcd1' instead of the needed 'abcd001'.
How do I maintain the three digit form for this code?

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 1일
thisfile = sprintf('abcd%03d.tif', i);
TiffDta = Tiff(thisfile, 'r');
firstlevelTIFF{i} = TIFFData.read(); TIFFdata.nextDirectory();
secondlevelTIFF{i} = TIFFData.read(); TIFFdata.nextDirectory();
thirdlevelTIFF{i} = TIFFData.read();
  댓글 수: 2
Hosup Song
Hosup Song 2016년 7월 1일
why do i need the thisfile part? is there a way to incorporate the %03d into the first line of the code?
Walter Roberson
Walter Roberson 2016년 7월 1일
Using a variable makes it easier to read, and also makes it easier to issue error messages because in your real code you would have put in a try/catch in case opening the file failed. But it is not strictly necessary.
TiffDta = Tiff(sprintf('abcd%03d.tif', i), 'r');
firstlevelTIFF{i} = TIFFData.read(); TIFFdata.nextDirectory();
secondlevelTIFF{i} = TIFFData.read(); TIFFdata.nextDirectory();
thirdlevelTIFF{i} = TIFFData.read();

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by