Chapter 01-3: Adding values to the results table


This third chapter describes how to add values to the results table in imagej. Basically you could store  values in variables and arrays. But sometimes it is easier or more convenient to add them to the results table, especially if you want to quickly display them on the screen.


Step 01: Create new Text Window


In this case there is no image necessary. Just open a new text window: --> File --> New --> Text Window


Step 02:   Compute the simple macro

Some simple code sample for the imagej macro language. Just copy the code into a new imagej text window and run the macro.


// This is an example for adding values to the results tabel. No image is necessary.

// Add a single value to the results tabel
x = 100;    // example value
y = 100;    // example value

if (nResults>=0) run("Clear Results");     // Check whether the results table is empty, if not clear it.
i = nResults;    // variable for counting, initialising with 0

setResult("Label", i, "point"+i+1);            // add a "Label" column to the results table and name the entry "point1"
setResult("X", i, x);                                    // add to the same row a "X" column and add the x-value
setResult("Y", i, y);                                    // add to the same row a "Y" column and add the y-value
print(nResults);                                         // print the numbers of existing rows in the table

// Add additional values from an array to the results tabel:

x_coord = newArray(10,20,30);
y_coord = newArray(11,22,33);

// Here starts the loop to read the values in the array and put them to the results table, without overwriting the first entry.

for(n = 0; n < x_coord.length; n++)    {
                i = nResults;
                setResult("Label", i, "point"+i+1);
                setResult("X", i, x_coord[n]);
                setResult("Y", i, y_coord[n]);
                }
updateResults();
print(nResults);



Step 03: Enjoy the results table


As you can see the labeling starts with point1. The first row in the results table shows the single point in the first part of the macro. After this entry the nResults (nResults is a imagej variable for the amount of rows in the results table) is exactly 1 as you can see in the first row in  the Log window. It's always good to  use the print() command in imagej if you want to display the contents of your variables.  The rows 2-4 are showing the values of the x-/y-coord array.