Login  Register

Re: setting up multiple arrays using sequential name.

Posted by Wayne Rasband on Feb 08, 2009; 3:07pm
URL: http://imagej.273.s1.nabble.com/setting-up-multiple-arrays-using-sequential-name-tp3693809p3693816.html

> Hello,
>
> I am trying to write a macro to analysis multiple image stacks. For  
> each
> stack, the analyzed result would put into an array respectively. As  
> I have n
> stacks, I would need to define n arrays.
> Ex:
> a1 = newArray(nSlices);
> a2 = newArray(nSlices);
> .
> .
> .
> .
> an = newArray(nSlices);
>
> Could I use "for loop" to do this job? Like:
> for (i=1; i<nImages;i++){
>      "a"+i = newArray(nSlices);
> }
>
> I tried this but it seems to me that by concatenating a string and a
> variable couldn't be assigned as the name of array.

You can use the Results table as if it were a 2D array. Here is an  
example that creates 10x100 results table and saves it in the user's  
home directory as a spreadsheet-compatible text file.

       // generate 10x100 results table
       columns = 10;
       rows = 100;
       run("Clear Results");
       for (n=1; n<=columns; n++) {
           for (i=0; i<rows; i++) {
               setResult("a"+n, i, n+i/100);
           }
       }
       updateResults();

       // randomly access results
       for (i=0; i<10; i++) {
           n = round(random*columns+1);
           index = round(random*rows);
           value = getResult("a"+n, index);
           print("n="+n+", index="+index+", value="+value);
       }

       // save in users home directory
       saveAs("Measurements", getDirectory("home")+"Results.xls");

-wayne