Search code examples
arraysmacrosimagejimagej-macrofiji

Printing Array Items by Index Number in ImageJ (Fiji)


I'm trying to figure out how I can access an item in an array by it's index number. I've written a script which will generate an array, with some number of variables. There's an old script on the imageJ mailing list archive (shown below) which is able to print given index value for a known value within the array, but is there a way to find the value within the array itself? I.e. if I have the user input the number of values that should be in the array, can I have the macro call up the values in the array from that?

My array generator:

Dialog.create("Time Point Input");
Dialog.addNumber("How many time points?", 0)
Dialog.addString("What are your time points (comma separated, no spaces)?:",0);
Dialog.show();
time = Dialog.getNumber();
points = Dialog.getString();
Fpoints = newArray(points);

Where the readouts could be something like:

time = 4
points = 5,10,12,27

Fpoints[0] = 5
Fpoints [1] = 10
Fpoints [2] = 12
Fpoints [3] = 27

Calling up index from array number value example code:

arr = newArray(1,5,3,12); 
i = index(arr, 5); 
print("index = "+i); 

  function index(a, value) { 
      for (i=0; i<a.length; i++) 
      if (a[i]==value) return i; 
  return -1; 
 } 

Thank you!


Solution

  • I am not 100% sure if I get your question right.

    but is there a way to find the value within the array itself?

    The problem is that you cannot create an Array with points since it's a String. Try something linke:

    Fpoints = split(points, ',');
    

    Then you can iterate over Fpoints with a loop, or use you index function to get an index of a given value.

    for (i = 0; i < Fpoints.length; i++) {
        print(Fpoints[i]);
    }