Login  Register

Re: writing results of different length into columns

Posted by Straub, Volko A. (Dr.) on Jul 10, 2016; 5:55am
URL: http://imagej.273.s1.nabble.com/writing-results-of-different-length-into-columns-tp5016837p5016838.html

Dear Sylvia,

I don't have a direct solution for your problem, but rather than writing
each selection co-ordinate pair directly into a results table (I assume
that is what you are doing), why don't you keep co-ordinates in a
one-dimensional array? You have then somewhat more flexibility how to
print the coordinates at the end of your macro. The code below prints
the coordinates into a text window in a format that can easily be copied
to Excel and split into columns where missing coordinates are shown as
empty cells. I think that is what you wanted.
The example has just two pairs of x/y coordinates (total of 4 arrays),
but it could easily be extended to more pairs. You can also write a
small macro function to dynamically add values to an array so that you
don't have to define the length of the required array at the start of
the macro. For examples, see:
http://www.richardwheeler.net/contentpages/text.php?gallery=ImageJ_Macros&file=Array_Tools&type=ijm

Hope this helps,
Volko

x1=newArray(2,4);
y1=newArray(7,5);
x2=newArray(1,4,10);
y2=newArray(8,10,5);

maxArrayLength=maxOf(x1.length,x2.length);

title1 = "Result Window";
title2 = "["+title1+"]";
f = title2;
if (isOpen(title1)){
     print(f, "\\Update:"); // clears the window
}else{
     run("Text Window...", "name="+title2+" width=72 height=8 menu");
};

for(i=0;i<maxArrayLength;i++){
     if(i<x1.length){
         print(f,x1[i]+", "+y1[i]);
     }else{
         print(f,", ");
     };
     if(i<x2.length){
         print(f,", "+x2[i]+", "+y2[i]);
     }else{
         print(f,", ");
     };
     print(f,"\n");
};




On 09/07/2016 23:28, Sylvia Neumann wrote:

> Hi,
>
> I am using the imageJ macro language, not Java. I want to write selection
> coordinates from an image analysis into a table. Something like this below:
>
> X1 Y1 X2 Y2 XN YN
> 1 6 2 9 12 15
> 4 10 18 18 12 20
> 60 17 50 75
> 70 110
> 80 130
>
> I can add them to the results Table, but because the columns have different
> length, I have to prefill the table with NaNs, see below.
>
> X1 Y1 X2 Y2 XN YN
> 1 6 2 9 12 15
> 4 10 18 18 12 20
> 60 17 50 75 NaN NaN
> NaN NaN 70 110 NaN NaN
> NaN NaN 80 130 NaN NaN
>
> Because I want to use these values later in a xls file, it is really hard to
> look at. It seems like a simple thing to do, but I have looked through so
> many posts in this forum but cannot find a solution that works for me. For
> other macros I have used Array.show (X1, X2) etc, but for this the number of
> selections changes with every experiment.
>
> Thanks for your help!
> Sylvia
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/writing-results-of-different-length-into-columns-tp5016837.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html