More about the macro language – basic syntax and operators

Until now, you have learned how to keep track of the menu options you select in order to create a program that executes all of them in a sequential way.

The ImageJ macro language is much richer than just the options output by the macro recorder. This section explains a bit more about the macro programming language.

Variables

You can define your own variables inside the macro language. A very simple example is as follows:

message = "Hello, World!";
print(message);

This macro simply causes the message, Hello, World! to appear on the ImageJ log window. The print(message) command outputs its messages there. In this case, the text is contained in the message variable. Variables in ImageJmacros may contain any type of data (numeric, strings, arrays) and they need not be declared, they are created when their values are assigned. Another example is as follows:

message1 = "Hello, World!";
message2 = 40 + 2;
print(message1, message2);
print(message1 + "" + message2);

As you can see, both the methods print the same message (Hello, World! 42), despite the message2 variable storing a numeric value. It is automatically converted to a string when printed.

Tip

Note that the message2 variable stores the value 40 + 2 as 42. All the common mathematical operations are available in the macro language: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). Exponentiation is done using the pow(base,exponent) function (for example, pow(2,3) = 8). With this example, you can also see that in string variables, the + symbol is used to concatenate values.

An array is a special type of variable that stores several values at once. These values can be accessed or modified by addressing their position inside the array using square brackets. Arrays are created using the newArray(arg) function, with arg being either the dimensions of the array or the actual contents separated by commas. An array may contain different type of values. Edit and run the following example:

array1 = newArray(2);
print(array1[0], array1[1]);
array1[0] = 42;
array1[1] = "A string value";
print(array1[0], array1[1]);

In this macro, we have created an array variable (array1) with only two positions. In the beginning, both positions a have value of 0. We then proceed to assign two values, one numeric and another a string. Array positions are addressed using the variable name and then the position (starting at 0) in square brackets. You can see how many elements a given array contains with the lengthOf(arg) function, where arg is an array variable. If you try to address a position beyond the current capacity of the array (say, array1[2] in the previous example), you will get an error message as shown in the following screenshot:

Variables

A brief note on debugging a macro

Now that we are at it, in the previous dialog you will have noticed the Show "Debug" Window checkbox. If you check it and then click on the OK button, the Debug window will appear. This window contains all the variables currently stored in your macro file, along with their values or their dimensions, in case of arrays. This is useful when we are trying to find where a coding error may have happened. This window can also be called from the editing window by navigating to the Debug | Debug Macro menu option (Ctrl + D) of the editing window. Once this window is open, you can use the step-by-step tool to go through each line of your macro to check where the problem lies. When you choose the Debug Macro option, the first line of your macro will be highlighted as follows:

A brief note on debugging a macro

In this debugging mode, navigating to Debug | Step (Ctrl + E) will execute the highlighted line, and the cursor will move to the next line. By navigating to Debug | Trace (Ctrl + T), all the lines of code will be executed in a step-by-step fashion.

Control structures

As in any other programming language, the usual control structures (loops, conditionals) are available in the ImageJ macros. The following brief examples show very simple pieces of code to explain their use. You are encouraged to try them on your own and play with them to see how they behave. If you already know some programming, you probably may want to skip this section.

The for loop

The for loop consists of a variable initialization, a continue condition, and an operation that is made after the contents of the loop are executed. Typically, it consists of a counter variable that is incremented until it reaches a certain limit. An example of the for loop is as follows:

for(i=0;i<10;i++){
  print("Value of i ="+i);
}

The output of the preceding code is as follows:

10 lines of text are displayed starting from Value of i = 0 to Value of i = 9.

The variable i is created inside the loop and has no meaning outside it. The value of the variable has to be less than the specified limit (i<10) and is incremented after each iteration of the loop (i++).

Tip

The i++ construction you have just seen means "increment the value of i in one unit". There is an equivalent to this, i-- that subtracts 1 from the variable's present value. These are "post-increment" and "post-decrement" operators. There also exist the "pre-increment" and "pre-decrement" equivalents (++i, --i). The difference between them is that in post-increment the variable is evaluated and then incremented, and in pre-increment it works the other way round. If i equals 0, print(i++) prints 0, but print(++i ) prints 1.

The while loop

The while loop is similar to the for loop, it executes its contents as long as the specified condition is true. An example is as follows:

i=0;
while(i<10){
  print("Value of i ="+i);
  i++;
}

The output displayed on the screen is exactly the same as in the previous macro.

The if (condition) and if (condition) … else statements

The if statement allows you to perform some operation only if the condition is true. The following macro is similar to the one that was used to explain the for loop, but the printed message depends on the content of the variable, i.

for(i = 0; i< 10; i++) {
if (i % 2 == 0) {
    print(i + " is even.");
  } else {
    print(i + " is odd.");
  }
}

This little loop identifies which numbers are even and which are odd. The condition checks whether the number modulo 2 is 0. In that case, the number is even. If not (else), the number is odd.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset