For-loop concatenation

Hello everyone,

is it possible to run a for-loop inside a for-loop? I would like to analyze rois in each slice of a stack (1st for-loop), re-sort them (2nd for loop) and measure only the first roi of each slice (if statement).

I tried different things, but when I start the macro it doesn’t stop anymore.

Thanks in advance!

Best,

David

Hi @KleinhansDa,
welcome to the forum! Nested loops should pose no problems maybe you have an error in your code that makes it run forever. If you post your code we can help you figure out what is going on.

3 Likes

Hello @gab1one,

thanks for the welcome, this forum was quite helpful so far.

This is my code:

	input = getImageID();
			for (i=1 ; i<nSlices(); i++) {
			selectImage(input);
			setSlice(i);
			roiManager("reset");
			run("Analyze Particles...", "add slice");
				for (i=0 ; i<roiManager("count"); i++) {
    			        roiManager("select", i);
    			        run("Set Scale...", "distance=3.8760 known=3.8760E-4 pixel=1 unit=micron");
    			        run("Set Measurements...", "centroid redirect=None decimal=2");
   			 	roiManager("measure");
    			        x = getResult("X",0);
    			        w = getWidth();
    			        a = w/x;
    			        roiManager("rename", a);
    			        run("Clear Results");
				}
					roiManager("Sort");
					run("Set Measurements...", "centroid area decimal=2");
					selectImage(input);
    				        run("Set Scale...", "distance=3.8760 known=1 pixel=1 unit=micron");
    				        roiManager("Select", 0);
    				        roiManager("measure");
    				        //NC = getResult("Area");
    				        //String.copyResults();
    				        //roiManager("reset");
    				        //waitForUser("WAIT");
			}

And this would be a sample image.

Thanks in advance.

Best,

David

You shouldn’t use the same variable i in both nested loops: your inner loop will set i to 0, then the outer loop will increase i++, but in the next iteration, i will be set to 0 again, resulting in an infinite loop.

Try using a different name, such as j, in the inner for loop.

4 Likes

Thanks Jan,

somehow I thought this could be a problem but then I also thought ‘i’ would just be overwritten. Ok, however, I made it work. Thanks!

Best,

David

1 Like