# Difference between revisions of "MATLAB Scripting"

(→MATLAB tutorial for ImageJ) |
(Notifies about MATLAB R2017b shipping Java 8.) |
||

Line 1: | Line 1: | ||

− | {{Warning|MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows Windows], [http://www.mathworks.com/matlabcentral/answers/103056-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-mac-os macOS] or [http://www.mathworks.com/matlabcentral/answers/130360-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-linux-os Linux].}} | + | {{Warning|Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: [http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows Windows], [http://www.mathworks.com/matlabcentral/answers/103056-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-mac-os macOS] or [http://www.mathworks.com/matlabcentral/answers/130360-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-linux-os Linux]. |

+ | |||

+ | If you run MATLAB R2017b and later, you don't need to do this, as it ships and uses Java 8 already. | ||

+ | }} | ||

{{Learn | languages}}ImageJ-MATLAB is an extension which: | {{Learn | languages}}ImageJ-MATLAB is an extension which: | ||

* Translates data between ImageJ images and MATLAB matrices. | * Translates data between ImageJ images and MATLAB matrices. |

## Revision as of 02:36, 14 December 2017

ImageJ-MATLAB is an extension which:

- Translates data between ImageJ images and MATLAB matrices.
- Enables execution of MATLAB scripts from inside ImageJ's Script Editor.
- Lets you launch ImageJ and interact with it from inside MATLAB.
- Allows developers to write additional plugins which extend these capabilities in new directions.

## Contents

# MATLAB tutorial for ImageJ

## Prerequisites

- Add the ImageJ-MATLAB update site.
- You will need to install your own licensed copy of MATLAB. All that is provided with ImageJ are adapters for evaluating scripts written in ImageJ to MATLAB, and converters between ImageJ and MATLAB data structures.
- MATLAB ships with Java 7, but ImageJ requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: Windows, macOS or Linux.
- If you're 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 ImageJ

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 ImageJ 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). Note that only scripts, not functions, can be evaluated in this way. See the MATLAB documentation for an explanation of these concepts.

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 ImageJ to a remote MATLAB, MATLAB will not have access to ImageJ'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 net.imagej.Dataset rval % @OUTPUT net.imagej.Dataset mask % Performs dilation with a 3x3 square, % operating on the active dataset % Outputs the dilated mask and the original image % with the mask applied. rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset

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 ImageJ classes without auto-conversion support should launch ImageJ from within MATLAB.

### Global state

MATLAB retains state (e.g. declared variables) as commands are executed, and ImageJ makes no special effort to clean up after a script. So whether running internally or communicating externally with MATLAB, state will be available to and persist after script execution. Thus one can, for example, write scripts in ImageJ referencing variables declared in MATLAB, without actually initializing them in the script.

### Passing Objects

The caveat to global state is that, when running ImageJ externally, ImageJ and MATLAB run in separate JVMs. As a result, most objects can not be passed between the two. This makes Datasets (and arrays) the currency that is passed between these applications.

### Return values

Most of the ImageJ scripting languages have implicit return values. As mentioned above, ImageJ will only execute true scripts, which do not have return values (in the MATLAB functional sense). There is a similar concept in the `ans`

variable, which automatically gets the return value of executed statements if they are not explicitly assigned. However, due to the global nature of the ImageJ-MATLAB script language, it is not necessarily clear if `ans`

was set by the script or a pre-existing command. Thus the decision was made that ImageJ-MATLAB scripts will **never** implicitly return a value. Instead, the @OUTPUT annotation should always be used - even for `ans`

, as shown here:

% @OUTPUT double[] ans % This trivial script demonstrates the use of % the "ans" variable in the SciJava-MATLAB % script language. % Unassigned statements in MATLAB are automatically % assigned to "ans". However, these scripts will not % return "ans" unless it is explicitly requested as % an output parameter. 0

### Importing classes

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

When running ImageJ externally, MATLAB will not have 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 ImageJ.

Instead, you can launch ImageJ 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.

## Running ImageJ within MATLAB

The ImageJ update site provides an `ImageJ.m`

script that will start up an ImageJ instance inside a running MATLAB application. Launching the script is the same as for Miji:

addpath('/Applications/Fiji.app/scripts') % Update for your ImageJ installation as appropriate ImageJ;

The startup process automatically injects the ImageJ classpath into the MATLAB classpath, merging the two. At this point, you'll have a working ImageJ 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 ImageJ 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.

For example, instead of using a script as described above, we could achieve the same result by executing the following commands in the MATLAB prompt:

IJM.getDatasetAs('data') % import the image as a MATLAB matrix rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset IJM.show('rval') % display the rval array in ImageJ IJM.show('mask') % display the mask array in ImageJ

# Source

- Scripting-MATLAB provides the script engine plugins.
- ImageJ-MATLAB defines the ImageJ-specific translators.

# Video presentation

# Publication

- Hiner, M. C.; Rueden, C. R. & Eliceiri, K. W. (2016), "ImageJ-MATLAB: a bidirectional framework for scientific image analysis interoperability",
*Bioinformatics*, PMID 27797782, doi:10.1093/bioinformatics/btw681, <http://bioinformatics.oxfordjournals.org/content/early/2016/10/25/bioinformatics.btw681>.