24

A friend asked me about creating a small web interface that accepts some inputs, sends them to MATLAB for number crunching and outputs the results. I'm a Python/Django developer by trade, so I can handle the web interface, but I am clueless when it comes to MATLAB. Specifically:

  • I'd really like to avoid hosting this on a Windows server. Any issues getting MATLAB running in Linux with scripts created on Windows?
  • Should I be looking into shelling out commands or compiling it to C and using ctypes to interact with it?
  • If compiling is the way to go, is there anything I should know about getting it compiled and working in Python? (It's been a long time since I've compiled or worked with C)

Any suggestions, tips, or tricks on how to pull this off?

Pete
  • 2,503
  • 4
  • 21
  • 20
  • 7
    Note that this would be in violation of MATLAB license. Perhaps if you are at a University you would get away with it, but for MATLAB you need a license for each _end user_ - ie everyone who is going be using your web interface. I was trying to setup some computational web services and we were told of this requirement - in the end we had to use Matlab compiler to create a standalone executable which doesn't have this restriction (or switch to Python completely - Numpy/Scipy/Matplotlib provide a pretty comprehensive replacement). – robince Feb 13 '10 at 10:57

4 Answers4

18

There is a python-matlab bridge which is unique in the sense that Matlab runs in the background as a server so you don't have the startup cost each time you call a Matlab function.

it's as easy as downloading and the following code:

from pymatbridge import Matlab
mlab = Matlab(matlab='/Applications/MATLAB_R2011a.app/bin/matlab')
mlab.start()
res = mlab.run('path/to/yourfunc.m', {'arg1': 3, 'arg2': 5})
print res['result']

where the contents of yourfunc.m would be something like this:

%% MATLAB
function lol = yourfunc(args)
    arg1 = args.arg1;
    arg2 = args.arg2;
    lol = arg1 + arg2;
end
ifixthat
  • 6,137
  • 5
  • 25
  • 42
Max Jaderberg
  • 562
  • 5
  • 10
13

Take a look at mlabwrap which allows you to call Matlab via a python API

betabandido
  • 18,946
  • 11
  • 62
  • 76
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
  • 1
    I second mlabwrap. Though, I would prefer a pure Python solution like numpy, I'm currently using mlabwrap to read some data from file, preprocess it in Python send to Matlab and then get the results. You may have overhead problems with starting the Matlab environment everytime a request comes. – Ruggiero Spearman Feb 13 '10 at 10:57
  • 2
    I also agree that mlabwrap is insanely easy to use. However, as Amac said, the overhead is quite significant. To put some numbers to this, a computation I'm using (function optimization.lsqlin) takes 0.01 seconds to finish when calling it from matlab, but when calling it from python with the same parameters using mlabwrap it takes 0.30 seconds to finish. – Spike Feb 16 '10 at 00:28
6

As an alternative, since Matlab R2014b, a Python library is provided to call Matlab from Python using the MATLAB Engine API for Python.

gaborous
  • 15,832
  • 10
  • 83
  • 102
1

Regarding OS compatibility, if you use the matlab version for Linux, the scripts written in windows should work without any changes. If possible, you may also consider the possibility of doing everything with python. Scipy/numpy with Matplotlib provide a complete Matlab replacement.

Raja Selvaraj
  • 7,178
  • 3
  • 20
  • 13