"continue" in a for-loop?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

"continue" in a for-loop?

Angeline Lim-2
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?


Thanks

Angeline

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: "continue" in a for-loop?

Rasband, Wayne (NIH/NIMH) [E]
> 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
Reply | Threaded
Open this post in threaded view
|

Re: "continue" in a for-loop?

Angeline Lim-2
I put the IF statement in, fixed the problem. Thanks!


Angeline

On Fri, Oct 9, 2015 at 1:26 PM, Rasband, Wayne (NIH/NIMH) [E]
<[hidden email]> wrote:

>> 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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html