Comparing a numeric value with text in an if-statement

Hello,

in an ImageJ macro I would like to loop through an array and at a certain point define a variable. The following code illustrates the idea:

run("Blobs (25K)");
setAutoThreshold("Default");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 display clear add");
numbers = newArray(nResults);
	
testvariable = "empty";
	
for (i = 0; i < numbers.length; i++) {
	numbers[i] = getResult("Area", i);
	sum_numbers += numbers[i];
	if (sum_numbers <= 5000 && testvariable == "empty") {
		print("true");
					}
	else {
		print("false");
		testvariable = numbers[i];
	}
}

However, it produces the error:

Number or numeric function expected in line ...

It seems that once testvariable has been defined as a numeric value, it cannot be compared to the text anymore. In this particilar example I could just define testvariable = -1; which would solve the problem, but in case that the array in the for-loop contains negative numbers, it would not work anymore.
I do not want to abort looping through the array once the if-statement does not match the conditions, since I might have to define several variable in this loop.

So my question to the people more experienced with writing macros is: what would be the “proper way” to solve this.
Thanks already!

@MojoDodo

To be honest… my brain is still waking up for the day (only half-way through my tea…) - but I think you can just simply use the call toString(testvariable) == "empty" and that should do it - no? If you need testvariable to be a number once again… in the else{} block - you can convert it back with parseInt(testvariable);.

It’s good to double-check function definitions and options here: https://imagej.net/ij/developer/macro/functions.html

1 Like

Hello Mojo (and Ellen) -

As an alternative to Ellen’s solution, let me propose a non-IJM-specific
idiom that is commonly used.

Use what is called a sentinel value to flag your “empty” condition.
Because getResult ("Area") yields a zero or positive value, it will
never be negative when it’s a meaningful number. So -1 is both
a valid IJM numeric value, but not a “valid” number in your use case.
So you can use -1 to mean “empty.”

testvariable = -1;
...

    if (sum_numbers <= 5000 && testvariable == -1) {
...
    testvariable = numbers[i];

(This is a general approach will work unchanged across languages,
whether weakly or strongly typed (or somewhere in between). You
don’t have to drill down into the nuances of IJM’s type system.)

Thanks, mm

2 Likes

I do not get an error when I run your macro using ImageJ 1.52u, Java 1.8.0_172 (64-bit) and MacOS 10.12.6. What are you using?

1 Like

Hello Wayne -

Two more data points, for what they’re worth:

I see this issue on 1.52p, but not on 1.52t (both running on
ubuntu 16.04 LTS).

Here is my test macro:

testvariable = "empty";
if (testvariable == "empty") {
	print ("true");
}
else {
	print ("false");
}
testvariable = 1.0;
if (testvariable == "empty") {
	print ("true");
}
else {
	print ("false");
}

On 1.52p, my script first prints “true” to the Log window, and
then pops a Macro Error message box: “Number or numeric
function expected in line 9” / “if ( testvariable == <“empty”> ) {”.

On 1.52t, the script works, and prints “true” and “false” to the
Log window.

Thank, mm

1 Like

That’s also the behaviour I am getting with ImageJ 1.52a and Fiji/ImageJ 1.52p on Ubuntu Linux 18.04

Thank you, this works for me using ImageJ 1.52p.

Thank you for this explanation!
The reason why I hesitated doing so was having a scenario in mind where testvariable = numbers[i]; could potentially lead to the variable being -1 again (of course not in the above posted code, since there numbers represents particle area). But thinking about it now, this probably could not happen for any of the image analysis I am doing, so this is also a good way to go :laughing:

Thanks to everyone for the support!

1 Like

This bug is fixed in the current version of ImageJ (1.52u). Use the Help>Update ImageJ command to upgrade.

2 Likes