> On Oct 9, 2015, at 2:32 PM, Angeline Lim <
[hidden email]> wrote:
>
> Hi all,
>
> I wrote a for-loop to run particle analysis in a directory of images.
> Some of these images do not have any "particles" to count and i get a message
> " the selection is empty" and then it exits the loop.
>
> How do i have the loop continue here and process the rest of the
> images instead of exiting?
Use the ‘continue’ statement to skip the rest of the loop if there are no particles. Here is an example:
dir = getDirectory("Choose Source Directory ");
list = getFileList(dir);
for (i=0; i<list.length; i++) {
open(dir+list[i]);
setAutoThreshold("Default");
run("Analyze Particles...", "display clear");
if (nResults==0) {
print("skipping image "+i);
continue;
}
print("image="+i+" particles="+nResults);
close();
}
This macro outputs
image=0 particles=64
image=1 particles=61
skipping image 2
image=3 particles=54
image=4 particles=64
when I run it on a directory containing five images, one of which is blank.
-wayne
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html