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 |
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 |
I use this method a lot. And adding another loop around the for i loop allows for adding columns to the outputString for each row incrementally. Example output of the version below at https://www.flickr.com/photos/mcammer/28185160856/
This example generates a sequence of ten positive integers less than 100 and leaves the field blank if less than or equal to threshold value. In the Log window the numbers don't look aligned, but the tabs are there and get copied (or save as text and then open) into your favorite spreadsheet or stats program. /* This method reinvents the table methods for on the fly use in the Log or other text output windows */ threshold = 16; outputString = ""; // this could also be outputString = newArray(length); and fill with "" for (i=0; i<10; i++) { result = round(random() * 100); if (result > threshold) outputString = outputString + result + " \t"; else outputString = outputString + " \t"; } print (outputString); _________________________________________ Michael Cammer, Optical Microscopy Specialist http://ocs.med.nyu.edu/microscopy http://microscopynotes.com/ Cell: (914) 309-3270 ________________________________________ From: ImageJ Interest Group [[hidden email]] on behalf of Volko Straub [[hidden email]] Sent: Sunday, July 10, 2016 2:11 AM To: [hidden email] Subject: Re: writing results of different length into columns 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: https://urldefense.proofpoint.com/v2/url?u=http-3A__www.richardwheeler.net_contentpages_text.php-3Fgallery-3DImageJ-5FMacros-26file-3DArray-5FTools-26type-3Dijm&d=CwIC-g&c=j5oPpO0eBH1iio48DtsedbOBGmuw5jHLjgvtN2r4ehE&r=oU_05LztNstAydlbm5L5GDu_vAdjXk3frDLx_CqKkuo&m=SN__168OD-1g6I_woOqyXQGvv3oXxH-zYU1-THoPnSw&s=xplI8Y6HwkrgS-NF2dGYjnKXJiAE-JxoRvKJlJW322U&e= 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: https://urldefense.proofpoint.com/v2/url?u=http-3A__imagej.1557.x6.nabble.com_writing-2Dresults-2Dof-2Ddifferent-2Dlength-2Dinto-2Dcolumns-2Dtp5016837.html&d=CwIC-g&c=j5oPpO0eBH1iio48DtsedbOBGmuw5jHLjgvtN2r4ehE&r=oU_05LztNstAydlbm5L5GDu_vAdjXk3frDLx_CqKkuo&m=SN__168OD-1g6I_woOqyXQGvv3oXxH-zYU1-THoPnSw&s=ld5tSPm3KQEsEgyTTjfGFpI3KmM0ZIvcCzMWYZgECAM&e= > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=http-3A__imagej.nih.gov_ij_list.html&d=CwIC-g&c=j5oPpO0eBH1iio48DtsedbOBGmuw5jHLjgvtN2r4ehE&r=oU_05LztNstAydlbm5L5GDu_vAdjXk3frDLx_CqKkuo&m=SN__168OD-1g6I_woOqyXQGvv3oXxH-zYU1-THoPnSw&s=z4AoOr0QkD3zN47h3P6vIOcq_MLcUN5rwU6TDZ_Z7n8&e= -- ImageJ mailing list: https://urldefense.proofpoint.com/v2/url?u=http-3A__imagej.nih.gov_ij_list.html&d=CwIC-g&c=j5oPpO0eBH1iio48DtsedbOBGmuw5jHLjgvtN2r4ehE&r=oU_05LztNstAydlbm5L5GDu_vAdjXk3frDLx_CqKkuo&m=SN__168OD-1g6I_woOqyXQGvv3oXxH-zYU1-THoPnSw&s=z4AoOr0QkD3zN47h3P6vIOcq_MLcUN5rwU6TDZ_Z7n8&e= ------------------------------------------------------------ This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is proprietary, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, disclosure, or distribution is prohibited. If you have received this email in error please notify the sender by return email and delete the original message. Please note, the recipient should check this email and any attachments for the presence of viruses. The organization accepts no liability for any damage caused by any virus transmitted by this email. ================================= -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thank you Volko and Michael, also for the quick response! This should work for me.
Best Sylvia
|
Free forum by Nabble | Edit this page |