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!

Olly

The Java API is quite flexible:

http://javadoc.imagej.net/ImageJ1/ij/measure/ResultsTable.html

A row can be extracted according to the macro API:

https://imagej.net/developer/macro/functions.html#getResult

getResult("Column", row)

In Java I found, e.g.:

http://javadoc.imagej.net/ImageJ1/ij/measure/ResultsTable.html#getRowAsString(int)
http://javadoc.imagej.net/ImageJ1/ij/measure/ResultsTable.html#getValueAsDouble(int,%20int)
http://javadoc.imagej.net/ImageJ1/ij/measure/ResultsTable.html#getStringValue(int,%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()!

https://imagej.net/developer/macro/functions.html#call

https://imagej.net/developer/macro/functions.html#eval

Hi,

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?

Thanks,

O

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)");
setAutoThreshold("Default");
//run("Threshold...");
run("Analyze Particles...", "display clear include");
row= eval("js","Analyzer.getResultsTable().getValueAsDouble(0,0)");
print(row);

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

run("Blobs (25K)");
setAutoThreshold("Default");
run("Analyze Particles...", "display clear include");
col=eval("js","Analyzer.getResultsTable().getLastColumn()");
//print(col)
for ( i=0; i<col; i++){
    isNull=eval("js","Analyzer.getResultsTable().getColumn("+i+")");
    if(isNull!=""){    
    row= eval("js","Analyzer.getResultsTable().getValueAsDouble("+i+",0)");
    print(row);
    }    
}

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

2 Likes

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) + "  ";
print(line);

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

3 Likes