How to pass arguments within a function in imageJ macro?

I want to pass in foldername into the run() function and the save() function but they are within quotes and I don't know how to pass the variable names.

I generated the run() and save() function using the record command in imageJ.

The aim of this program to to automate the process of running the plugin "T2 analysis mouse" on multiple folders. Any other tweets or help will be appreciated.

I found this online source https://imagej.nih.gov/ij/macros/ArgumentPassingDemo.txt but it does not work.

I tried multiple ways inclusing /&foldername/3 T2 &filename/3 T2 /&foldername 3 T2``/&foldername3 T2 /&filename 3 T2``/&filename3 T2 Here is the code I wrote:

function makeT2(foldername)
{
    filename = foldername;
    print(filename);
    run("T2 Analysis mouse", "select=[/Users/Vineeth/Desktop/New stuff Feb 5/&filename 3 T2] 
        image=[32-bit Signed] width=256 height=128 offset=0 number=902 gap=0 little-endian");
    run("Save", "save=[/Users/Vineeth/Desktop/New stuff Feb 5/&foldername3 T2/T2map.tif]");
    close();
}

folders = getFileList("/Users/Vineeth/Desktop/New stuff Feb 5/");

for( i = 0; i < folders.length; i++)
{
  print(folders[i]);
  file = getFileList("/Users/Vineeth/Desktop/New stuff Feb 5/" + folders[i]);
  for( j = 0; j < file.length; j++)
  {
    if(file[j] == "3 T2/")
    {
      print("made t2 for  " + folders[i] + file[j]);
      makeT2(folders[i]);

    }
  }
}

The various print() functions are for testing the code and the code mostly works expect for the makeT2 function.

This is the error:

java.lang.NullPointerException
at T2_Analysis_mouse.getImage(T2_Analysis_mouse.java:183)
at T2_Analysis_mouse.run(T2_Analysis_mouse.java:56)
at ij.IJ.runUserPlugIn(IJ.java:183)
at ij.IJ.runPlugIn(IJ.java:150)
at ij.Executer.runCommand(Executer.java:124)
at ij.Executer.run(Executer.java:61)
at ij.IJ.run(IJ.java:249)
at ij.macro.Functions.doRun(Functions.java:561)
at ij.macro.Functions.doFunction(Functions.java:79)
at ij.macro.Interpreter.doStatement(Interpreter.java:203)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.runUserFunction(Interpreter.java:278)
at ij.macro.Interpreter.doStatement(Interpreter.java:206)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doIf(Interpreter.java:852)
at ij.macro.Interpreter.doStatement(Interpreter.java:215)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doFor(Interpreter.java:464)
at ij.macro.Interpreter.doStatement(Interpreter.java:221)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doFor(Interpreter.java:464)
at ij.macro.Interpreter.doStatement(Interpreter.java:221)
at ij.macro.Interpreter.doStatements(Interpreter.java:191)
at ij.macro.Interpreter.run(Interpreter.java:102)
at ij.macro.Interpreter.run(Interpreter.java:72)
at ij.macro.MacroRunner.run(MacroRunner.java:124)
at java.lang.Thread.run(Thread.java:695)

and this is the log after running the progran:

LeeMouseOld_30Jan18.LH1/
made t2 for  LeeMouseOld_30Jan18.LH1/3 T2/
LeeMouseOld_30Jan18.LH1/
Please choose a directory where the T1 or T2 folder is the last item in the path (i.e. 5_t1 or 6_t2)
Directory return for General T folder: /Users/Vineeth/Desktop/New stuff Feb 5/&filename 3 T2/
Please adjust import settings to:
Image type: 32-bit signed
Width: 128 pixels
Height: 256 pixels
Number of images: 50 images
Check Little-endian byte order

As you can see &filename remains the same but I want the data within the variable filename to be in its place.

1 answer

  • answered 2018-02-13 01:18 Jan Eglinger

    Use string concatenation:

    run("T2 Analysis mouse", "select=[/Users/Vineeth/Desktop/New stuff Feb 5/" + filename + " 3 T2] 
            image=[32-bit Signed] width=256 height=128 offset=0 number=902 gap=0 little-endian");
    run("Save", "save=[/Users/Vineeth/Desktop/New stuff Feb 5/" + foldername + "3 T2/T2map.tif]");
    

    See the macro function documentation (emphasis added):

    run("command"[, "options"])

    Executes an ImageJ menu command. The optional second argument contains values that are automatically entered into dialog boxes (must be GenericDialog or OpenDialog). Use the Command Recorder (Plugins>Macros>Record) to generate run() function calls. Use string concatentation to pass a variable as an argument. With ImageJ 1.43 and later, variables can be passed without using string concatenation by adding "&" to the variable name. For examples, see the ArgumentPassingDemo macro.

    The mentioned shortcut syntax with & does not work between square brackets [] in the option string.

    A number of questions on the ImageJ forum also discuss this.