Open images based on (partial) name in a macro

Hi all,
I am writing a complex macro. First I define some values and folders with:
var dir=getDirectory("Choose a working directory");
var ch1 = "CH_1";
var ch2 = "CH_2";
var ch3 = "CH_3";
var ch4 = "CH_4";

Based on this, I create a dialog, where the user can change names and decided if he/she wants to do some other stuff:
Dialog.create("Correct Channel names");
Dialog.addCheckbox("Blank substraction for Channel 2?", false);
Dialog.addCheckbox("Substract average RFU outside of ROIs for Channel 2?", false);
Dialog.addCheckbox("Blank substraction for Channel 3?", false);
Dialog.addCheckbox("Substract average RFU outside of ROIs for Channel 3?", false);
Dialog.show();
ch2sub = Dialog.getCheckbox();
ch2sub2 = Dialog.getCheckbox();
ch3sub = Dialog.getCheckbox();
ch3sub2 = Dialog.getCheckbox();

Based on this, I copy all files, which correspond to the input from the dialog into a designated folder (here the variable ch2):

if (ch2sub) {
   var ch2_blank1= dir + "\\CH2_blank1\\";
   File.makeDirectory(ch2_blank1); 
   print("Your folder for blank subtraction for " + ch2 + " has bee successfully created");
   list=getFileList(dir); 
       setBatchMode(true);  
       for(u=0;u<list.length;u++){ 
        if(endsWith(list[u],".tif")&&indexOf(list[u],ch2)>=1){ 
        files=Array.concat(files,list[u]); 
        } 
} 
        for(h=1;h<files.length;h++){
	      setBatchMode(true); 
	       open(dir+files[h]); 
           title=getTitle();
           saveAs("Tiff", ch2_blank1+title);
           run("Close All");
           print("Your "+ ch2 + " images have been succesfully stored in the folder "+ch2_blank1);  
} 
  }

Here is the problem. If I run this code it works. If I just perform this action for a single channel it works.
If I, however, do this for multiple channels, it is saving all previous channels into the folder.

If I run it for “ch2” it is copying all images into the folder for “ch2”.
If I run it for “ch2” and “ch3” it is copying all “ch2”-images into the “ch2”-folder
and all “ch2” and “ch3” images into the folder “ch3”.

Is the problem clear?

Thanks for the help

Great,

and then?

Not very complex yet …

Regards

Herbie

sorry. I accidentally hit enter, before finishing the post…

Note that you are saving everything in a folder that has the hardcoded name CH2_blank1

What are you modifying when you ‘run it for “ch2” and “ch3”’?

I changed
if(endsWith(list[u],".tif")&&indexOf(list[u],ch2)>=1){
to
if(endsWith(list[i],".tif")&&indexOf(list[i],ch3)>=1){
and the following u to i

and

for(f=1;h<files.length;h++){
to
for(h=1;f<files.length;h++){
and the following f to h

and of course

saveAs("Tiff", ch2_blank1+title);

to

saveAs("Tiff", ch3_blank1+title);

stupid me…sorry for bothering you. I found the problem.
It was in
files=Array.concat(files,list[u]);
By not changing this, the arrary files got extended by every new list…

Solution:
files=Array.concat(files,list[u]);
files1=Array.concat(files1,list[h]);

Thanks for your help though

Hey @Smu21101

It’s never a bother !!! The whole point of this is that you find a solution… whether others help you or you figure it out yourself in the end. And the fact that you have posted all this code and an example of such an issue - might just help out others in the future… :slight_smile: So always post and ask!

eta :slight_smile:

1 Like