How Can I access the variables in tables in a macro

imagej
macro

#1

What is the IJ macro method for accessing the variables in tables other than the results table like the OrientationJ Table Vector Field and Overlay Elements tables shown here…something like getResult().?


#2

Dear @rondespain,

there was a similar discussion in

This table, however, looks like a custom JTable. In that case, I don’t see a chance that you can get the information via the macro language. You might have a chance with a scripting language that lets you access the instance directly:

table = // ... get instance ...
model = table.getModel();
model.getValueAt(0, 0); // getValueAt(rowId, columnId)

I am not entirelt sure, however, how to easily get hold of the instance.

Best,
Stefan


#3

Please have a look at:
http://forum.image.sc/t/macro-for-plug-in-orientationj/7224/8?u=herbie

Regards

Herbie

PS:
The table you are referring to isn’t an ImageJ table, i.e. neither a results table nor a renamed results table. It is a custom coded table that you can’t access from an ImageJ macro.


#4

I referenced two tables…are they both un renamable? They are:

The overlay elements table from Image>Overlay>List Elements

The Table Vector Field from the OrientationJ Vector Field plugin>Show Table button

I note that Wayne has a workaround for the Nnd table that works well, but doesn’t work for the OrientationJ table…I’m assuming that the two tables are of a different class.

Here’s Wayne’s code that works wity Nnd

//begin macro
run(“Blobs (25K)”);

setAutoThreshold(“Default”);

run(“Analyze Particles…”, “display exclude clear include”);

run("Nnd ");

IJ.renameResults(“Table Vector Field”,“Results”);

for (i=0; i<nResults; i++)

print((i+1)+" "+getResult(“X”,i));

//end macro

Is there a way to write a utility in Java that will be callable from a macro that will convert tables to renamable and rename them to “Results” so getResult() can be used?..this would be a great addition to the macro language.

Ron


#5

Ron,

if you followed the thread with Wayne’s suggestion, you should be aware that it is well-known to me and that I myself have made another suitable suggestion …

This fact and the link I’ve posted explain which table is inaccessible.

The other table may be accessible from a macro, but I didn’t try. Did you?

Because there are infinite ways to code tables in Java, there won’t be the desired plugin…

Regards

Herbie


#6

IJ.renameResults(“Table Vector Field”,“Results”);
IJ.renameResults(“Overlay Elements”,“Results”);

Both cakks respond with “Table Not Found”

They are both active objects within IJ as their numerical contents can be selected, copied and pasted into Excel, so they should be programmatically accessible…I’ve seen many of these tables from other plugins that act the same way, so there is probably a common type that programmers use to display a table within Java…I’ll look into it…Please let me know if anyone else finds a way to make these tables accessible within a macro…the “Overlay Elements” table is a table generated from the interactive drop down menus in the main ImageJ window. I’m surprised that it’s not renamable.

This call works on the table from Nnd:
IJ.renameResults(“Nearest Neighbor Distances”,“Results”);

The table in Nnd is evidently renamable, so would the author of Nnd please let me know what type (class) of table he/she used in their macro (and maybe a link to the Java API page for it)?

Thanks
Ron DeSpain


#7

Ron,

not sure to whom you’ve responded.

As I’ve written, the “Table Vector Field” is not accessible from an IJ-macro and I see little hope for any approach to change this situation except the author is willing to adapt his code correspondingly. As mentioned, he already made a central table column accessible.

Here is again one previously posted macro that makes the “Overlay Elements”-table accessible:

nme = "Overlay Elements";
selectWindow( nme );
path = getDirectory( "temp" ) + nme + ".txt";
saveAs( "Results", path );
valArray = split( File.openAsString( path ), "\n" );
Array.show( valArray );

Please tell us if it works for you.

Regards

Herbie


#8

Thanks Herbie for the great tip…here’s the code that does what I wanted.

Is there a macro call that will get the table title when its window is active so I don’t have to explicitly define it?

//BEGIN MACRO*****
//save overlay table and reopen as Results by Ron DeSpain
//setup

newImage(“Overlay Generation”, “RGB white”, 512, 512, 1);
//setTool(“rectangle”);
makeRectangle(268, 52, 101, 105);
run(“Add Selection…”);
//setTool(“oval”);
makeOval(189, 208, 98, 98);
run(“Add Selection…”);
//setTool(“line”);
makeLine(301, 364, 411, 261);
run(“Add Selection…”);
run(“List Elements”);
//selectWindow(“Overlay Generation”);

//***table conversion
nme = “Overlay Elements”;

//I don’t see how to get the active non-image window title so you can skip the next line if the table is the active window
//selectWindow( nme );
path = getDirectory( “temp” ) + nme + “.xls”;
print(path);
saveAs( “Results”, path );
//valArray = split( File.openAsString( path ), “\n” );
//Array.show( valArray );
open(“C:/Users/User/AppData/Local/Temp/”+nme+".xls");
//Some example variable acquisition and manipulations*********
x = getResult(“X”,0);
print("X0= “+x);
a = getResult(“Index”,1);
b = getResult(“Index”,2);
print(“Index1+Index2 = “+a+”+”+b+” = "+ a+b);
//END MACRO***

It also works for the table output from OrientationJ Vector Field table

//**BEGIN MACRO
//save Table Vector Field table and reopen as Results by Ron DeSpain
//setup
nme = “Table Vector Field”;

//***table conversion

//I don’t know how to get the active non-image window title so you can skip the next line if the table is the active window
//selectWindow( nme );
path = getDirectory( “temp” ) + nme + “.xls”;
print(path);
saveAs( “Results”, path );
//valArray = split( File.openAsString( path ), “\n” );
//Array.show( valArray );
open(“C:/Users/User/AppData/Local/Temp/”+nme+".xls");
//Some example variable acquisition and manipulations*********
x = getResult(“X”,0);
print("X0= “+x);
a = getResult(“Index”,1);
b = getResult(“Index”,2);
print(“Index1+Index2 = “+a+”+”+b+” = "+ a+b);
//END MACRO**


#9

Ron,

I had a quick look at your code.

It also works for the table output from OrientationJ Vector Field table

It doesn’t work for me, because the window “Table Vector Field” can’t be selected by the macro call “selectWindow”. It doesn’t even work if the table is the frontmost window. As mentioned before, it is a custom-made window that is inaccessible from the macro language.

Why don’t you use

open(path);

instead of :

open(“C:/Users/User/AppData/Local/Temp/”+nme+".xls");

Regards

Herbie


#10

you don’t have to select the window, just have it open. It works great for me. The choice of saving as an XLS is crucial. Saving as txt doesn’t work.

The first code for the overlay table generates it’s own data so you just have to run it.

The second code requires you display the OrientationJ table prior to running it.

open(path); would probably work, but then you’d have to select the saved file manually…it’s bad enough that you have to know the table name you’re converting apriori, since getTitle(); doesn’t work with non image windows.

Ron


#11

The second code requires you display the OrientationJ table prior to running it.

I did this, but as I’ve written:

It doesn’t work for me […] It doesn’t even work if the table is the frontmost window.

open(path); would probably work, […]

It works for logical reasons without further ado.

Regards

Herbie


#12

Herbie

I’m running Win10 64bit OS and it works with ImageJ v1.51u11, and FIJI with IJ v1.51v18 on my machine. It allows me to import the variables in non “Results” tables just fine. I’m sorry it isn’t working for you as it is very helpful.

Here’s a screen shot of my output

Ron


#13

Ron,

you wrote

It also works for the table output from OrientationJ Vector Field table

The second code requires you display the OrientationJ table prior to running it.

but you don’t show this case in your screen-shot. Does it really work for you?
As I wrote, it doesn’t work for me (“Table Vector Field”-case).

Of course it works in the “Overlay Elements”-case and I wrote it for such tables:

Here is again one previously posted macro that makes the “Overlay Elements”-table accessible:

Clueless,

Herbie


#14

Sorry Herbie…I left a hard coded path in the code. Try this code…It runs on both of my windows machines (win10 and win8).

//save overlay table and reopen as Results by Ron DeSpain 180311
//--------------setup---------------------
tablename = “Demo”; //Change This for another table

//------------DEMO CALCULATIONS--------
if(tablename == “Demo”){
newImage(“Overlay Generation Image”, “RGB white”, 512, 512, 1);
makeRectangle(268, 52, 101, 105);
run(“Add Selection…”);
makeOval(189, 208, 98, 98);
run(“Add Selection…”);
makeLine(301, 364, 411, 261);
run(“Add Selection…”);
run(“List Elements”);

tablename = “Overlay Elements”;

}
//------------------table conversion--------------
selectWindow(tablename);
temppath = getDirectory( “temp” );
print(temppath);
print(temppath+tablename+".xls");
filepath=getDirectory( “temp” ) + tablename + “.xls”;
print("filepath= "+filepath);
saveAs( “Results”, filepath);
if(File.exists(filepath));
open(filepath);
updateResults();

//--------Demo example variable acquisition and manipulations---------
if(tablename == “Overlay Elements”){
print(“SOME SIMPLE CALCULATIONS FROM THE IMPORTED TABLE”);
x = getResult(“X”,0);
print("X0= “+x);
a = getResult(“Index”,1);
b = getResult(“Index”,2);
print(“Index1+Index2 = “+a+”+”+b+” = "+ a+b);
}
exit();




Let me know if it works for you. Do you run Unix?

Ron


#15

Ron,

the problem is not with your code, that is based on my macro.

The problem appears to be due to the fact that I can’t run the latest version (Jan. 2018) of OrientationJ which requires Java 8. For various reasons I still use Java 6 and an older version of OrientationJ. With this older version of OrientationJ the “Table Vector Field” can’t be accessed.

I still insist, however that in general

nme = "Demo";
selectWindow( nme );
path = getDirectory( "temp" ) + nme + ".csv";
saveAs( "Results", path );
open(path);

works for logical reasons, i.e. no need for

filepath=getDirectory( “temp” ) + tablename + “.xls”;
open(filepath);

Regards

Herbie


#16

This works so well for me I recommend it be shared in the Macros repository.

//Table2ResultsMacro 180401
//ron_despain@hotmail.com
waitForUser(“Select the table you want to import\nFrom the Window drop down menu\nor by clicking on the table\nThen click OK”);
//--------ADD THIS CODE TO YOUR MACRO--------------
//—WHEN THE TABLE YOU WANT TO USE IS SELECTED----
filepath=getDirectory( “temp” )+“savedtable”+".xls";
saveAs( “Results”, filepath);
open(filepath);
updateResults();
//----YOU CAN THEN USE getResult() for variables—


#17

With the latest ImageJ daily build (1.52a30), tables created by the Image>Overlay>List Elements command can be renamed “Results” and the values accessed using the getResults() and getStringResult() macro functions. Here is an example:

  run("Image with Overlay");
  run("List Elements");
  IJ.renameResults("Overlay Elements", "Results");
  x = getResult("X", 0);
  y = getResult("Y", 0);
  color = getResultString("Color", 0);
  print("x="+x+", y="+y+", color="+color);

#18

Hi Wayne

The problem is still identifying the current name of the table to be renamed… programmatically. The table window name output from a plugin may vary with the input image in order to keep it identified with the original image. This means that the macro developer won’t know the window name to reference in IJ.renameResults(). A macro programmer needs a function like tabletitle=getTitle() for table windows (or any window) so he/she can then call IJ.renameResults(tabletitle, “Results”); from anywhere in his/her code. Though this change is great for most situations. Thanks for the change.

Ron