Results table - copying entire rows in a macro

Hi Everyone,

Is there an easy way to select and copy rows out of the results table within a macro using the row index value?

I have a time lapse series of cell nuclei in a stack. I have measurements for each object in each slice. My script tracks the objects, and in the process I get the row number for the corresponding data in the results table (I use that to find the centroids for tracking). I’d like to then copy that entire row of the results table and drop it into a custom table.

getResults requires a column heading - can I obviate that argument so it takes all columns?
String.copyResults needs rows to be selected manually - is there a way to do that in the macro?
Is there another way I’ve not thought of?

Many thanks for any insight you can offer - I’m just going in circles through the documentation with this problem!


The Java API is quite flexible:

A row can be extracted according to the macro API:

getResult("Column", row)

In Java I found, e.g.:,%20int),%20int)

If you use Jython or JavaScript the API could be reused similar.

You can also try to use an hybrid approach to call a Jython or JavaScript function inside a Imagej macro, see:

Using eval() or call()!


Thanks for the fast reply. I’m not sure how to do what you’re suggesting with calling other languages - I only really have experience with matlab and ijm, and learning another language is a daunting prospect.

I saw the getResult() function, but the problem I have is that it needs two arguments, the first of which specifies a column. is there something I can put in place of that specifier so that it copies all the columns?



You can stay in a macro but you can use the Java API with an intermediate scripting command. In the following example I use JavaScript in line 6 to extract a value of the results table at row 0, column 0:

run("Blobs (25K)");
run("Analyze Particles...", "display clear include");
row= eval("js","Analyzer.getResultsTable().getValueAsDouble(0,0)");

It’s not very fast in a macro to iterate with this approach over all (enabled measurements) columns, however:

run("Blobs (25K)");
run("Analyze Particles...", "display clear include");
for ( i=0; i<col; i++){
    row= eval("js","Analyzer.getResultsTable().getValueAsDouble("+i+",0)");

More options can be derived from the Java API I posted before.


Ok, it turned out to be simple enough to just loop through the headings. For any other newbie programmers like myself, this snippet will get your row and print it to the log:

headings = split(String.getResultsHeadings);
row = 0 // variable that tells you what row number to take. 0 is the first row.
line = "";
for (a=0; a<lengthOf(headings); a++)
    line = line + getResult(headings[a],row) + "  ";

Now I just need to make it save the log as a csv or xls file, and this thing will work…