Expandable arrays

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Expandable arrays

Stein Rørvik
The ExpandableArrays option is useful when the number of elements to work with is unknown beforehand.

The example in the documentation
https://imagej.nih.gov/ij/macros/examples/ExpandableArrays.txt
works as expected.

However, if you split the example macro into separate functions and macros as in the example I give below, ExpandableArrays no longer works. Can anyone enlighten me why the below macro code does not work as expected?

The macro must be installed and called from the menu since it contains several macros.
The macro is initialized by AutoRun, setting the ExpandableArrays flag to true, and then the user calls the macro "Show Arrays" from the menu. An error message with index out of bounds appears; so ExpandableArrays does in this case not persist from the initialization, as one would expect.

Moving PopulateArrays() into the AutoRun macro works in this case, but that is not what I want, as the whole point with expandable arrays is to be able to expand them within separate parts of the workflow.

--------------------------------------------------
var cars = newArray;

function Initialize() {
                requires("1.45s");
                setOption("ExpandableArrays", true);
}

function PopulateArray() {
                cars[0] = "Saab";
                cars[1] = "Volvo";
                cars[2] = "BMW";
                cars[4] = "Ford";
}

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

macro "AutoRun" {
                Initialize();
                showMessage("Macro was initialized");
}

macro "Create and Show Array" {
                PopulateArray();
                PrintArray();
}
--------------------------------------------------


Stein

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Expandable arrays

Michael Schmid-3
Hi Stein,

My guess:
setOption("ExpandableArrays", true) is valid only as long as a macro is
active. It is lost after the AutoRun macro terminates (after ImageJ
startup).
Please add the setOption to the "Create and Show Array" macro.


Michael
________________________________________________________________
On 17.04.19 00:23, Stein Rørvik wrote:

> The ExpandableArrays option is useful when the number of elements to work with is unknown beforehand.
>
> The example in the documentation
> https://imagej.nih.gov/ij/macros/examples/ExpandableArrays.txt
> works as expected.
>
> However, if you split the example macro into separate functions and macros as in the example I give below, ExpandableArrays no longer works. Can anyone enlighten me why the below macro code does not work as expected?
>
> The macro must be installed and called from the menu since it contains several macros.
> The macro is initialized by AutoRun, setting the ExpandableArrays flag to true, and then the user calls the macro "Show Arrays" from the menu. An error message with index out of bounds appears; so ExpandableArrays does in this case not persist from the initialization, as one would expect.
>
> Moving PopulateArrays() into the AutoRun macro works in this case, but that is not what I want, as the whole point with expandable arrays is to be able to expand them within separate parts of the workflow.
>
> --------------------------------------------------
> var cars = newArray;
>
> function Initialize() {
>                  requires("1.45s");
>                  setOption("ExpandableArrays", true);
> }
>
> function PopulateArray() {
>                  cars[0] = "Saab";
>                  cars[1] = "Volvo";
>                  cars[2] = "BMW";
>                  cars[4] = "Ford";
> }
>
> function PrintArray() {
>                  for (i=0; i<cars.length; i++)
>                                 print(cars[i]);
> }
>
> macro "AutoRun" {
>                  Initialize();
>                  showMessage("Macro was initialized");
> }
>
> macro "Create and Show Array" {
>                  PopulateArray();
>                  PrintArray();
> }
> --------------------------------------------------
>
>
> Stein
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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