Borrar filtros
Borrar filtros

How to write script for multiple files

1 visualización (últimos 30 días)
Hosup Song
Hosup Song el 1 de Jul. de 2016
Comentada: Walter Roberson el 1 de Jul. de 2016
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Jul. de 2016
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 comentarios
Hosup Song
Hosup Song el 1 de Jul. de 2016
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 el 1 de Jul. de 2016
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();

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by