Batch open file that contain special string

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

Batch open file that contain special string

Robert Niestroj-Pahl
Hi Folks,

I try to write a macro for opening and processing only files that contain a
certain string in their name.
There is already some code, but unfortunately it doesn't work for me.
I want to open all files that contain the string "ch00".
I have a bunch of files, and they are named "series001_ch00.tif",
"series001_ch01.tif" or "series001_ch02.tif".
I want to use that macro to just choose the folder and then the automated
analysis is done.

This is the part of my code, that should open the specific file from the
specified folder:

files=newArray(0);
dir=getDirectory("Select the directory");
list=getFileList(dir);
for(i=0;i<list.length;i++){
if(endsWith(list[i],&quot;.tif&quot;)&amp;&amp;indexOf(list[i],&quot;*ch00.tif&quot;)>=1){
        files=Array.concat(files,list[i]);
        }
}
for(f=1;f<files.length;f++){
        open(files[f]);

The macro asks for the folder, but the image is not opened...
Thanks

Robert




--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: Batch open file that contain special string

Robert Niestroj-Pahl
Hi,

I also tried it with a different version:

        dir=getDirectory("Select the directory");
        list=getFileList(dir);
        for(i=0;i<list.length;i++){

if(endsWith(list[i],&quot;.tif&quot;)&amp;&amp;indexOf(list[i],&quot;ch00&quot;)>=0)
        title = getTitle();
        open(title);
        {
    run("Set Measurements...", "  redirect=None decimal=3");
        run("8-bit");
        run("Subtract Background...", "rolling=50");
        run("Gaussian Blur...", "sigma=2");
        setAutoThreshold("Triangle dark");
        setOption("BlackBackground", false);
        run("Convert to Mask");
        run("Watershed");
        run("Analyze Particles...", "size=100-15000 pixel display clear add");
        close();
        }
}
}

But this gives me the error: "undefined variable in line 7" - open(title);

Sorry, but I am really new to programming...

Robert




--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: Batch open file that contain special string

Michael Schmid-3
Hi Robert,

well, there is not title before you open an image, so getTitle does
nothing. Anyhow, to open a file, you need a path, i.e. directory and
name, not the title.
So I guess you need
   open(dir+list[i]);
in line 7.

Also, you want to open the file and do all the other operations on it
only if its name fulfills the criteria of the 'if' statement. So you
need to open the curly braces directly after the line of the 'if':

  dir=getDirectory("Select the directory");
  list=getFileList(dir);
  for (i=0;i<list.length;i++) {
   if (endsWith(list[i],".tif")&&indexOf(list[i],"ch00")>=0) {
     open(dir+list[i]);
     run("Set Measurements...", "  redirect=None decimal=3");
     ...


Michael
________________________________________________________________
On 07/06/2018 15:26, Robert Niestroj-Pahl wrote:

> Hi,
>
> I also tried it with a different version:
>
> dir=getDirectory("Select the directory");
> list=getFileList(dir);
> for(i=0;i<list.length;i++){
>
>    if(endsWith(list[i],".tif")&&indexOf(list[i],"ch00")>=0)
> title = getTitle();
> open(title);
> {
>      run("Set Measurements...", "  redirect=None decimal=3");
> run("8-bit");
> run("Subtract Background...", "rolling=50");
> run("Gaussian Blur...", "sigma=2");
> setAutoThreshold("Triangle dark");
> setOption("BlackBackground", false);
> run("Convert to Mask");
> run("Watershed");
> run("Analyze Particles...", "size=100-15000 pixel display clear add");
> close();
> }
> }
> }
>
> But this gives me the error: "undefined variable in line 7" - open(title);
>
> Sorry, but I am really new to programming...
>
> Robert

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

Re: Batch open file that contain special string

Robert Niestroj-Pahl
In reply to this post by Robert Niestroj-Pahl
Many Thanks Michael! That is the solution!
I guess I have to go a steep learning curve!

Robert



--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: Batch open file that contain special string

Robert Niestroj-Pahl
In reply to this post by Michael Schmid-3
Uhmm, sorry, but I guess it don't works. If I change the string name to
"ch01", there is no image opened!
The macro opens only the first image in the folder...

Robert



--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: Batch open file that contain special string

Michael Schmid-3
Hi Robert,

hmm, no idea what happens there.
Maybe have a simple test macro that just prints the list of files to the
log, e.g.

      dir=getDirectory("Select the directory");
      list=getFileList(dir);
      for (i=0;i<list.length;i++) {
        if (endsWith(list[i],".tif") && indexOf(list[i],"ch01")>=0) {
          print(dir+list[i]);
        }
      }

If the test macro works correctly, it is likely that there is some
problem with the code that follows. E.g. check all curly braces, to see
whether they close correctly.

Also note that endsWith and indexOf are case-sensitive, so it will
neither work with "ch01.TIF" nor with "Ch01.tif".

Michael
________________________________________________________________
On 08/06/2018 16:02, Robert Niestroj-Pahl wrote:

> Uhmm, sorry, but I guess it don't works. If I change the string name to
> "ch01", there is no image opened!
> The macro opens only the first image in the folder...
>
> Robert
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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