Login  Register

Re: "continue" in a for-loop?

Posted by Angeline Lim-2 on Oct 09, 2015; 10:07pm
URL: http://imagej.273.s1.nabble.com/continue-in-a-for-loop-tp5014592p5014596.html

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