Denise Vlachou and Gurdaman Singh Interfacing Programs with MATLAB What is MATLAB? It is a numerical computing environment that utilises its own programming language to allow matrix manipulations, plotting of functions and data implementation of algorithms, creation of user interfaces and interfacing with programs written in other languages. Interfacing with other language MATLAB is an object oriented programming language. This means that it represents concepts as objects that have data fields and its associated procedures. It initially in-­‐cooperated the language of C to develop its own language that would be fast and reliable. This means it is able to read the languages of C/C++, fortran or java. It is able to interface well with java because java has a ‘write once, run anywhere’ basis. It creates a virtual environment so can run on any operating system. Java also derives much of its syntax from C and C++ therefore it is compatible with MATLAB. MEX files A MEX file is a ‘MATLAB executable-­‐file’ that is edited to be able to run in MATLAB. It provides an interface between MATLAB and subroutines written in language of C or C++. This relies on the use of ‘mex’ function that compiles the routine into a format that MATLAB can read. To compile it, the following steps are taken: 1. Enter the command: *mex -­‐setup* (Without the “*”). This allows you to choose which compiler to use. 2. Then open the code from another language in the editor. 3. Edit the code so it starts with the line *#include “mex.h”* 4. Once chosen, run the command “mex filename.extensiontype” (the extension may differ). 5. This will convert the ‘.extensiontype’ into a .mex file. This can now be executed in MATLAB and it will take an input and produce an output. For more details on how to convert large more complex functions, of some which may take varied inputs: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf Excel Codes • • • • • num = xlsread(filename)example – reads data from worksheet num = xlsread(filename,sheet) – reads specified worksheet num = xlsread(filename,xlRange)example – reads from specified range, x1Range num = xlsread(filename,sheet,xlRange) – reads from specified sheet and range Further Excel functions: http://www.mathworks.co.uk/help/matlab/ref/xlsread.html#bthf_dr A guide to learn how MATLAB interacts with Java -­‐ http://undocumentedmatlab.com/blog/jmi-­‐java-­‐to-­‐matlab-­‐interface/ References Getreuer, P. (2010). Writing MATLAB C/MEX Code. p1-­‐28. Introducing MEX-­‐Files – MATLAB Mathworks.co.uk. 1994. Introducing MEX-­‐Files -­‐ MATLAB. [online] Available at: http://www.mathworks.co.uk/help/matlab/matlab_external/introducing-­‐mex-­‐files.html. Denise Vlachou and Gurdaman Singh Image J and MATLAB ImageJ is very good at image handling and most importantly has user control functions such as drop down boxes, built-­‐in image processing functions, and plug-­‐ins. Plug-­‐ins such as macro recorder and ROI manager are particularly useful. However, MATLAB is very good at data processing and has some very powerful toolboxes. Both MATLAB and ImageJ can be used separately to produce similar outputs in many scenarios, but sometimes using them together is the most effective way of working, taking advantage of the strengths of both programs. An interface between the programs can be used using a type of “java language bridge” – a package called MIJI. Setup MIJI To set up this MIJI bridge, download 2 files found at the MIJI website http://bigwww.epfl.ch/sage/soft/mij/, called mij.jar and ij.jar and copy them into the java folder of the MATLAB application (File path Applications/MATLAB_R2013a/java) In the MATLAB command window the following commands should be made; >> addpath('/Applications/Fiji.app/scripts') %Let Matlab see this directory >> javaaddpath '/Applications/MATLAB_R2013a.app/java/mij.jar'%Give matlab this java functionality >> javaaddpath '/Applications/MATLAB_R2013a.app/java/ij.jar'% As above >> Miji %Open ImageJ within MATLAB Importing images from ImageJ MATLAB can now “see” ImageJ files along with the sample files that ImageJ contains. For example, the familiar Fly Brain stack can be opened within ImageJ with the command; MIJ.run('Fly Brain (1MB)') For MATLAB to store this image in its own format use I=MIJ.getCurrentImage %MATLAB will store the ImageJ image as an array I. Lots of the time there is an error message saying there isn’t enough memory left to do this, or it works but you cannot view I as an array because it’s too large. This is an example of times when MATLAB is not appropriate to use, as MATLAB doesn’t handle stacks as well as ImageJ does. MATLAB uses ‘double’ type precision and ImageJ uses ‘float’ so sometimes exchanging between the two can lose prescion. Simple commands of the interface MATLAB is very good for analyzing data if it’s not too big. These are very useful commands when using MATLAB and ImageJ in parallel; Image(M) % turns matrix M into an image with specified or default colormap Imread(picture) % turns a MATLAB image into an array MIJ.createImage('result',M,true) %open M as an image called ‘result’ in ImageJ. I=MIJ.getCurrentImage %import the image open in ImageJ into array I in MATLAB MIJ.run('Open...', 'path') %open an image file in ImageJ