Macro to iterate over filename for image sequence

Tags: #<Tag:0x00007fd542a432c8>



I have files named field and wavelength and would like to import 5 image stacks with same field and wavelength using macro or python code. each Field has seperate stack for 4 different wavelength and each field+wavelength has 5 stacks corresponding to time sequence.

For example, x_f00y_w000z. x is the stack number ranging from 1-5, f00y is field ranging from 0-14 and w000z is wavelength ranging from 0-3. So when I use run(Image Sequence…) for a specific folder, I want it to import stacks 1-5 in order for f0000_w0000 do the analysis, then f0000_w0001… and then f0001_w0000.

I am using

run("Image Sequence...", "open=/dir/ file=f000_w0000 sort");

to import my stacks combined stack going from 1_f0000_w0000 to 5_f0000_w0000 and manually changing the filename for every field and wavelength. I was trying to write a for loop to enter the “file=” as it goes through the each wavelength for each field.

Please let me know if I need to give more information.



@meahgem, you can use two nested for loops to achieve this:

for (y = 0; y <= 14; y++) {
  for (z = 0; z <= 3; z++) {
    fileParam = "f" + IJ.pad(y, 4) + "_w" + IJ.pad(z, 4);
    print(fileParam); // look at the Log window to see if this is what you expect
    run("Image Sequence...", "open=/dir/ file=" + fileParam + " sort");

Hope that helps.


Thank you, imagejan, that helped. I added a “path” line because imageJ would report no image found even though it printed the right fileParam. I realized that when I use the interface I select an image in a folder before Image sequence dialog box opens.

Here is my final code.

for (y=0; y<=11; y++){
	for (z=0; z<=3; z++){
		fileParam = "e" + IJ.pad(y,4) + "_w" +IJ.pad(z,4)+ ".tif";
		run("Image Sequence..." , "open=[path] file=" + fileParam+ " sort");