Negative arguments

Dear all,
I wrote this code in macro:

images= getTitle();
for (i=0 ;i<images.length;i++) {
if(matches(images[i], “.Three.”) && matches(images[i], “.!=Snapshot.”));}

but it did not work.

I want to select specifically an image that contains “Three” in the title but not “Snapshot”.

Is it possible?


Hi @mvizoso,

if(indexOf(images[i],"Three")!=-1 && indexOf(images[i], "Snapshot") == -1){
	print ("Yeeha");

Or is it that you want to understand the formatting of regular expressions?

A general remark: after opening an image, or creating a new image as a result of an operation, getImageID() gets you a negative number that uniquely identifies that image. You can save it in your array and refer to it using selectImage(id) , isOpen(id) and isActive(id) , which is sounder and offers more flexibility than going by the title. An exception may be when one command leads to numerous images (e.g. stack to images).


Hi again,
I do not understand very well in your code which is the meanning/function of “-1”.

Yes I am trying to understand macro language and its rules all this days in confinement hahaha.

I am curious about negative arguments, there must be a way to select an image if contains certain word in the title but not other. Imaging I have many images opened and some are called:

Image 1: Membrane_C=1.tif
Image 2: Membrane_snapshot_C=1.tif
Image 3: Nucleous_C=1.tif
Image 4: Nucleous_snapshot_C=1.tif

And I want to select the one called only membrane from C=1 channel.

Hi @mvizoso,

The indexOf(A, B) method returns the position of where string B was found in string A. If it’s -1 that means it wasn’t found.

This little program

images = newArray(

for (i = 0; i < images.length; i += 1) {
	if(indexOf(images[i],"Membrane") != -1 && indexOf(images[i], "snapshot") == -1){
		print(images[i] +": Yes!");	
	} else {
		print(images[i] +": No!");	

outputs this:

Let us know if we can help you further with learning ImageJ macro! :slight_smile:


1 Like

Thank you so much for the suggestion. I was updating my macro with your suggestions. I wrote a similar code but somehow is not recognizing the second variable (imageToSelectString2), so the script is applying the full function to all windows that contains C=1. Can you see the error in the code? I could not identify it! Thanks

imageToSelectString1 = “C=1”;
imageToSelectString2 = “Snapshot”;
selectImageByString0(imageToSelectString1, imageToSelectString2);
function selectImageByString0(imageToSelectString1, imageToSelectString2) {
images = getList(“image.titles”);
for (i=0 ;i<images.length;i++) {
if(matches(images[i], “."+imageToSelectString1+".”) && indexOf(images[i], “."+imageToSelectString2+".”) == -1)
run(“Duplicate…”, " ");
img1 = getTitle();
a = substring(img1, indexOf(img1,“Position”), lengthOf(img1)); // To select which part of the image title I want. In this case from word position onwards.
b = indexOf(a, " “); // To detect in which position is the first space.
c = parseInt(substring(a, b, indexOf(a, " “, b+1))); // To get the numbers just after the first space.
rename(“Position “+c+” deadcellmask”);
else {
print(images[i] +”: No!”);

What are the .dots-stars doing? :upside_down_face: We didn’t have them in our examples :wink:

Hi @haesleinhuepf,

I guess @mvizoso has seen some example command that can take a regular expression.

These strings may have ment to be “(.*)” which translates to a string (the two quotes), containing a part of a regular expression (the parentheses), and zero or more times any character (the star being zero or more times, the dot representing any character), in short: zero or more characters.

That is why I asked @mvizoso if he is interested in regexps or is interested in matching a string within a string in another way, being the indexOf(). Looking back on my education on regexps I remember mainly confusion, as regular expressions can lead a life of their own.

Even if the regexp would work in the matches, the spelling of @mvizoso’s example most likely doesn’t work as intended, as it contains a space, so the expression as written down the first time means any character, followed by zero or more spaces , the second means zero or more characters, followed by a space… If I’m not mistaken (and still confused :sunglasses: ).