MATLAB Scripting

Revision as of 12:35, 6 August 2014 by Hinerm (talk | contribs) (Initial version)
MATLAB Scripting (ImageJ)
Author Mark Hiner
Maintainer Mark Hiner
Curtis Rueden
File scripting-matlab.jar
Source Git
Development status stable


Template:Scripting

MATLAB tutorial for ImageJ

Prerequisites

  • You will need to install your own licensed copy of MATLAB. All that is provided with Fiji are adapters for evaluating scripts written in Fiji to MATLAB, and converters between ImageJ and MATLAB data structures.
  • If you've new to MATLAB, first check out Mathworks' getting started guide.
  • If you're familiar with the MATLAB language but haven't written .m scripts before, look through the script examples.

Creating MATLAB scripts inside Fiji

Using the Script Editor you will be able to select MATLAB from the language menu. You can also install and run .m scripts via the standard script plugin infrastructure.

Actually running a MATLAB script from Fiji is effectively like calling eval on the script's contents. The script will be evaluated as such in a remote MATLAB instance (which will be launched automatically, if needed). Options for controlling the startup of MATLAB, or killing existing MATLAB processes (e.g. if hidden) can be accessed via Edit > Options > MATLAB...

NB: because the script is being passed from Fiji to a remote MATLAB, MATLAB will not have access to Fiji's classpath. Objects can be passed as variables to MATLAB (e.g. by using @ annotation) but only if they are valid MATLAB classes or specially handled classes.

For example, by default all MatlabNumericArrays will be converted to matrices within MATLAB. We also support auto-conversion of ImageJ Datasets out of the box, which can be read in by scripts using "@matrix" parameters:

% @matrix data
% @OUTPUT double[] matrixSum
sum(data)
sum(ans)
matrixSum = sum(ans)

This script will take the active Dataset, set it as an array variable named "data" in MATLAB, and set the matrixSum output value to the sum of the first three dimensions of the dataset. Scripts requiring Fiji/ImageJ classes without auto-conversion support should launch Fiji from within MATLAB.

Running Fiji within MATLAB

The Miji project provides a MATLAB .m script that will start up a Fiji instance inside a running MATLAB application. The startup process automatically injects the full Fiji classpath into the MATLAB classpath, merging the two. At this point, you'll have a working Fiji and can now run MATLAB scripts as normal with access to the full unified classpath.

Additionally, the Scripting-MATLAB library also includes an extensible MATLAB command framework. This allows for the creation of utility classes that will be automatically populated into MATLAB variables for easy access. For example, you could use Fiji to open a dataset and perform thresholding (or any other processing steps), then in MATLAB use the IJM.importDataset command to set the active dataset as a MATLAB matrix variable, for further analysis.

Importing classes

To reference Java classes from MATLAB you will need to import them.


When running Fiji externally, MATLAB will not have Fiji/ImageJ classes in its classpath - so they can not simply be imported. Although MATLAB does support editing its classpath this is NOT recommended, as the classes loaded by MATLAB will not be the same as those loaded in Fiji.

Instead, you can launch Fiji inside MATLAB and have it take care of managing the class loading for you. MATLAB then supports the use of import statements to simplify class names.