imagejimagej-macro

Condition or function to verify if a file is missing when opening files sequentially in ImageJ


I'm using an ImageJ macro that opens and processes a group of images from several samples sequentially named - e.g. sample1_image1.tif, sample1_image2.tif, sample2_image1.tif, (...). I'm using a loop to increment both sequences for the samples and for the images.

for (i=0; i<samplenumber.length; i++) // Counter of the samples
    {
    imgnumber = imgspersample[i];
    j=0;
    for (j=0; j<imgnumber; j++) // Counter of the images for a given sample
        {
        k = j + 1;
        file = "sample"+samplenumber[i]+"_image"+k+".tif";
        path = directory+file;
        // Opening, converting and thresholding the image
        open(""+path+"");

However, some of the samples might be missing and an error occurs when the macro tries to open an image that doesn´t exist, breaking the loop. Is there any function or condition that can be added so the loop is not broken when a sample is absent?

For example: if sample 2 is missing, the macro opens and processes all images from sample 1, recognizes that there are no images for sample 2, and then jumps to sample 3?


Solution

  • Maybe considering the macro function "File.exists(path)" helps?