Friday, September 9, 2011

Using arrays (part 2.1): Table window


The Results window [see post] is a 'must' in ImageJ for storing numbers like a spreadsheet and also contains tools to explore the data. A more generalized multicolumn window exists in ImageJ termed the Table window which is very useful specially if you need to play with strings and numbers.


1- Creation

The creation of a Table window is done in two steps:
(i) the creation of the window 
run("Table...", "name=[<tabName>] width=350 height=250");

(ii) the columns are created by giving their names ...
 print("[<tabName>]","\\Headings:\t<col#1>\t<col#2>...\t<col#N>");

In this case, the built-in function print is used with TWO arguments:
  • the first is the name of the table enclosed between brackets [...]
  • the second is a string composed of the keyword "\\Headings:" followed by the list of the column names separated by a Tab character (in IJ, the Tab is encoded by the special character '\t' (backslash + t) ).
Note(s):
(i) A Tab (\t) is required between the keyword "\\Headings:" and the first column name.
(ii) The print(f,"\\Head...) must be typed just after the run("Table...",...)
(iii) You can't append a new column like in the Results window.
(iv) Don't be confused !! To fill the data, the name of the table must be enclosed between brackets [...], however to select this window with selectWindow(), don't use the brackets.

Here is an example of code...

+++ IJ snippet: create_table.ijm +++
+++ End of IJ snippet +++  

... and the results in image...
Fig.1: Table: a multicolumn window mixing strings and numbers

2- Write/replace Access of a row

To add a new row:
print(tableName,"col1\tcol2\t...colN");
To replace a row by a new one, start the string with the keyword "\\Update" followed by the row index (without space character):
print(tableName,"\\Update<row>col1\tcol2..colN");

For example, replacing the contents of the row #2:
print(f,"\\Update2:icosahedron\t20\t30\t12");

leads to ...
Fig. 2: The third row (index #2) is replaced.

3- Read/Write Access of cells

There are no utility functions to read/write cells in a Table window like in Results window, you need to develop your own. Here are my versions of nResults(), getResult(...), and setResult(...): nRows(), getCell(), and setCell().

3-1- function nRows(table): To get the number of rows in Table...
+++ IJ snippet: function nRows(tableName) +++
+++ End of IJ snippet +++

3-2- function getCell(...)
I use two versions of the function getResult("column",row) whether the column is accessed by:
- its name getCell(table,"column",row) or
- its number (0, 1, ...n) getCellByIndex(table,col,row).

+++ IJ snippet: function getCellByIndex(...) and getCell(...) +++
+++ End of IJ snippet +++

3-3- function setCell(...)
As previously, two versions of setCell(...) depending of the column access (by name or by index).
+++ IJ snippet: function setCell(tableName,"column",row) +++
+++ End of IJ snippet +++


Here is the complete code to read the previous table
+++ IJ snippet: table_window_read.ijm +++
+++ End of IJ snippet +++


4-Remarks

- You can't use the function getTitle() to get the window's name because it is only available for image windows. To skip this limitation, store your table's name in a variable as shown in my example.

5- Conclusion

This Table window is really interesting even though there is no access function.

Pros: numbers and string supported, multiple tables allowed
Cons: no direct access to cells, needs extraction.

No comments:

Post a Comment