Passing output from Matlab to ImageJ macro via script parameters

Using the ImageJ-MATLAB plugin, I am starting MATLAB-plugins from normal IJ1 Macro Language scripts.I don’t understand how/whether it is possible to pass variables from MATLAB to ImageJ and then to continue working with them.

Example:
MATLAB-Script Matlab_ParameterTest2 gives variable a as an output:

% @OUTPUT double[] a
 a=1.5;

In the IJ-Macro I run the MATLAB-plugin, then after I would like to work with variable a.

//var a;
macro ParameterTest {
	run("Matlab ParameterTest2");
	//a = run("Matlab ParameterTest");
	
}

I can see variable a printed out, but I don’t know how to grab it.
image

Thank you for any help.

1 Like

I fear this is currently not possible from the IJ1 macro language. The run() macro function doesn’t have any return value (and anyhow, how should it deal with scripts that declare multiple outputs? or output types that the macro language doesn’t support?).

In any full-fledged script language, it should be possible to collect the outputs by using ScriptService to run the script, e.g. using this Groovy script:

#@ ScriptService scripts

m = scripts.run(new File("/path/to/your/Matlab_Script.m"), false).get()
a = m.getOutput("a")

println a
3 Likes

Good day Jan,

probably not for the case in question (mathlab) but we should remember calling macros and plugins with parameter transfer:

runMacro(name, arg)
Runs the specified macro or script, which is assumed to be in the macros folder, or use a full file path. The string argument ‘arg’ can be retrieved by the macro or script using the getArgument() function. Returns the string argument returned by the macro or the last expression evaluated in the script. See also: getArgument.

getArgument()
Returns the string argument passed to macros called by runMacro(macro, arg), eval(macro), IJ.runMacro(macro, arg) or IJ.runMacroFile(path, arg).

Just for the record

Herbie

3 Likes

Thank you for the reply! I guess I need to switch language then.

dear @aklemm,
I did something similar passing argument from Matlab to ImageJ and get argument back from ImageJ to Matlab.
So to be clearer, I have a Matlab GUI that launch a ijm macro script in and then I get back some parameters from ImageJ and store/use them in Matlab.
I didn’t ImageJ-MATLAB plugin but I used the Miji http://imagej.net/Miji, http://bigwww.epfl.ch/sage/soft/mij/, https://it.mathworks.com/matlabcentral/fileexchange/47545-mij--running-imagej-and-fiji-within-matlab

You can read there how to implement Miji and use it in Matlab.

Trying to give some help to your question:

  1. To pass argument to Fiji from Matlab:
    I’ve created a .ijm script (.txt it’s also ok, but in IJ1 Macro language) and I getArguments in that script and IndexOf to get more than one argument.
    Something like that:
args=getArgument();
image_path=substring(args, (indexOf(args,"image_path")+11), indexOf(args,"saving_path")-1);
//print(image_path);
saving_path=substring(args, (indexOf(args,"saving_path")+12), indexOf(args,"R_median")-1);
//print(saving_path);
R_median=parseFloat(substring(args, (indexOf(args,"R_median")+9), indexOf(args,"R_variance")-1));
//print(R_median);
R_variance=parseFloat(substring(args, (indexOf(args,"R_variance")+11),indexOf(args,"thr_method")-1));
//print(R_variance);
thr_method=substring(args, (indexOf(args,"thr_method")+11));

// **doSomethingWithTheseArguments**

Then I launch this script from Matlab with as @anon96376101 suggested with the runMacroFile

IJ=ij.IJ();
args=...
        strcat('image_path=',image_path,' saving_path=',saving_path,' R_median=',R_median,...
        ' R_variance=',R_variance,' thr_method=',thr_method);
IJ.runMacroFile(java.lang.String(fullfile(macro_path,'WH_Miji_macro_RoiExtraction_setThr_auto.ijm'))...
    ,java.lang.String(args));

pay attention to have Miji working and have your IJM script in your macro path (1st argument of the runMacroFile function) and generally use java.lang.String to pass strings to it.
That way you can pass arguments to Fiji/ImageJ.

  1. get outputs from Fiji/ImageJ to Matlab
    To do it I use the log (where I’ve printed out something in a previous ImageJ macro launched in the way above mentioned) of ImageJ and get it using MIJ.getLog() and split more than arguments with spaces and some string functions.
    and then launch another IJMacro1 macro in fiji to close the log.
// Matlab script to get the log and get the two thresholds used to do a segmentation
thresh=(char(MIJ.getLog()));
spaces = strfind(thresh,sprintf('\n'));
spaces = spaces(end-1:end);
thresh_low=str2double(thresh(1:spaces(1)-1));
thresh_upp=str2double(thresh(spaces(1):spaces(2)-1));
// close th Log
IJ.runMacroFile(java.lang.String(fullfile(macro_path,'IJ_closeLog.txt'))...
    ,java.lang.String(args));

and the IJ_closeLog.txt script to close the log for ImageJ

if (isOpen("Log")) {
     selectWindow("Log");
     run("Close");
 }

I don’t know if it’s the most elegant way, but I did it years ago… maybe now I would use a full-fledged script languange as @imagejan pointed out.

Hoping to being heplful,
have a nice day
Emanuele Martini

3 Likes

Thanks a lot @emartini ! I haven’t tried Miji yet, but I will keep it in mind. In this case I am working on making accessible a MATLAB-code to users that don’t have any experience with MATLAB. For them the Imagej-MATLAB solution is nice, because Fiji is opening MATLAB and MATLAB is giving a plot and some numbers. That way they don’t have to get used to a new interface.

1 Like