# Matlab Integration and SciPy

2013-02-20This post contains informal notes collected while researching options and techniques for using Matlab to do complex numerical programming work while integrating with existing code bases. Also includes notes and links on alternatives. I used a trial version of MATLAB R2012B to build a proof-of-concept against an existing Java based project.

### Using Java from within Matlab

The java integration with Matlab is straightforward and works well. (The same seems to be true for .NET integration, although I didn’t test it.) No additional toolboxes or addons are required, beyond the core Matlab product, in order to call java classes from within Matlab.

However, there is one significant caveat; the java classpath of any custom code must share the classpath with matlab, and matlab includes a number of well-known libraries. My proof-of-concept project also used a lot of libraries and so there were a number of conflicts that made it difficult to reuse my code in Matlab directly. I almost got it to work eventually and probably could’ve if I continued, but I ended up taking a different approach using a remote network interface rather than direct Java library calls. The network based approach is much more flexible and doesn’t suffer from the classpath conflict issues

- http://www.mathworks.com/help/matlab/using-java-libraries-in-matlab.html
- http://www.mathworks.com/matlabcentral/answers/5892-use-java-classes-in-matlab
- http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/

In my proof-of-concept I published an RMI service as an interface in to my existing code base (which required minimal amount of code & setup since I was already using Spring Framework). I then built a small JAR library for use by Matlab that provided an RMI client proxy to the remote services. I used RMI, but could easily have used any other remoting protocol as well, such as Spring’s HTTP remoting. This setup worked well, and is flexible because it provides a dedicated area where matlab specific conversions and handling can be done with impact on existing code. It is also negated issues with classpath conflicts. Yet another benefit was that the Matlab client could be running on a different machine than the server code base.

### Using .NET from within Matlab

Similar to Java integration, no additional toolboxes or addons are required in order to call .NET assemblies from within Matlab.

### Calling Matlab from Java/.NET

Calling in to matlab from Java/.NET requires addons. The official approach to is to purchase the matlab compiler and then a standalone toolbox specific to each platform. This combination lets you compile standalone executables (and libraries?).

#### Pricing

I’m not sure what the rules are around publishing pricing information, but suffice to say, the official Matlab approach requires a relatively expensive initial investment. Required components include:

- Matlab Core
- Matlab Compiler
- Matlab Builder for Deployment (per platform)

The initial pricing I received would’ve required >$10k to get started. There are no runtime distribution costs though.

#### Alternative Unofficial Approaches

- https://code.google.com/p/matlabcontrol/
- http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html
- http://www.cs.utoronto.ca/~bowen/code/code.html#matjav
- http://jamal.khadkevich.org / https://github.com/hutm/JAMAL

### Additional Research

The remaining part of this post is primarily a collection of links and notes I generated while researching.

I don’t have much experience using external math engines in recent projects, however, based on what I’ve researched, I would not lean towards using Matlab for my own projects these days. It might be different if I had extensive knowledge of Matlab already, or lots of code that depended on it.

#### Matlab Alternatives

- SciPy: http://en.wikipedia.org/wiki/SciPy
- GNU Octave : http://en.wikipedia.org/wiki/GNU_Octave
- Scilab: http://www.scilab.org/
- Sage: http://www.sagemath.org/ (more symbolic math oriented? e.g. More like Mathematica vs. Matlab)

#### NumPy/SciPy

- ttp://www.scipy.org/more_about_SciPy
- http://docs.scipy.org/doc/
- http://stackoverflow.com/questions/6200910/relationship-between-scipy-and-numpy

#### Matlab to NumPy

### NumPy/SciPy Development

#### Dedicated SciPy IDEs

- http://enthought.com/products/epd.php
- http://code.google.com/p/spyderlib/
- http://code.google.com/p/pythonxy/

#### Generic Python IDEs

#### Plotting

- http://matplotlib.org/
- http://www.packtpub.com/matplotlib-python-development/book?utm_source=matplotlib.sourceforge.net&utm_medium=link&utm_content=pod&utm_campaign=mdb_002124
- http://code.enthought.com/projects/mayavi/

### Misc Links & Articles

#### Matlab vs PySci

- http://stevetjoa.com/305/ , good info including the comments
- http://vnoel.wordpress.com/2008/05/03/bye-matlab-hello-python-thanks-sage/

#### Antecdotal

- http://news.ycombinator.com/item?id=363096
- http://stackoverflow.com/a/11144102/123378
- http://stackoverflow.com/questions/5063037/should-i-switch-to-python
- http://www.reddit.com/r/math/comments/tl77g/which_one_of_these_gpl_gnu_general_public_license/

#### Mathematica vs Matlab vs Sage vs Octave

- http://math.stackexchange.com/questions/153/what-should-i-learn-first-mathematica-or-matlab
- http://sagemath.blogspot.com/2007/12/why-isnt-sage-just-part-of-octave.html

#### Python & Qt, PySide/PyQt

- http://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project
- http://www.pyinstaller.org/
- http://www.stat.washington.edu/~hoytak/blog/bestlibraries.html
- http://code.google.com/p/guiqwt/
- http://www.pyqtgraph.org/
- http://pythonhosted.org/PyQt-Fit/

#### Matlab & Python

- http://stackoverflow.com/questions/1707780/call-python-function-from-matlab
- http://www.mathworks.com/matlabcentral/newsreader/view_thread/290116

### Matlab, Misc

- http://stackoverflow.com/questions/2326609/how-to-organize-matlab-code
- http://www.datatool.com/prod02.htm (matlab programmingstyle guidelines)

- Important for Regression

- Undocumented Matlab

- Especially interesting: