Need help debugging a channel-merging macro

Hello @imagejan, following your example I modified my macro but it still won’t work. Each folder has the same number of images.
If I leave the macros as they are the error states: “Texas” is not a valid choice for c1, so I imagine it just takes the name of the image. If I change the content of Run function and write eg TRed[k] and DAPI[k], the error states: Array expected in line 18: run ( "Merge Channels..." , "c1=" + TRed [ k <]> + "c3=" + DAPI [ k ] + "create" ) ;

The macros are the following

Mdir = getDirectory("folder path");
MMdir= getDirectory("folder path");
list=getFileList(Mdir);
for(i=0;i<list.length;i++){
print(list[i]);
}
llist=getFileList(MMdir);
for(j=0;j<llist.length;j++){
print(llist[j]);
}
for(k=0;k<list.length;k++) {
	DAPI=list[k];
	TRed=llist[k];
	open(Mdir+DAPI);
	open(MMdir+TRed);
	run("Subtract Background...", "rolling=25");
	run("Merge Channels...", "c1="+ TRed + "c3="+ DAPI + "create");
	selectWindow("Composite");
	run("RGB Color");
	run("Set Scale...", "distance=156 known=100 pixel=1 unit=μm global");
	run("Scale Bar...", "width=100 height=5 font=18 color=White background=None location=[Lower Right] bold");
}

You should be able to pass image IDs as the c1 and c3 values. Something like this:

DAPI=list[k];
TRed=llist[k];
open(Mdir+DAPI);
dapiID = getImageID();
open(MMdir+TRed);
tRedID = getImageID();
run("Subtract Background...", "rolling=25");
run("Merge Channels...", "c1="+ tRedID + "c3="+ dapiID + "create");

(You can also pass image titles—see getTitle() macro function—but this is less reliable because it is possible for multiple images to have the same title, which introduces ambiguity.)

Thank you ctrueden, I did as you suggested but it gives me an error that -942c3=-942 is not a valid choice for c1. Apparently it assigns the imageID number in the channels rather than the image itself.

It seems that in my initial coding I should have put “c1=[” TRed + “]c3=[” DAPI to work and it works up to the merging point. Thank you for your help!

@meletis3012 We were missing crucial spaces! Sorry about that. Here is the corrected line:

run("Merge Channels...", "c1="+ tRedID + " c3=" + dapiID + " create");

Note the space before c3= now. And another one before create. The variables will be replaced and concatenated, so you end up with something like:

run("Merge Channels...", "c1=-1 c3=-2 create");

The c1 and c3 and create are all separate arguments to the “Merge Channels” command, written as one space-separated string. But since we forgot the spaces, it was being populated like this:

run("Merge Channels...", "c1=-1c3=-2create");

Which the program saw as "for the value of c1, use -1c3=-2create". And -1c3=-2create is of course not a valid image identifier—not an ID number, and not an image title.