Web Enabling Matlab Analytics
Over the past several months, I’ve been working on several projects at Toomre Capital Markets dealing with optimal ways of distributing results of calculations done in Matlab. In previous posts, I’ve written about accessing and storing information from Excel and SQL databases. In this post, I want to look at distributing some of the results via web pages.
One approach that we’ve used is having a Matlab routine run as a DLL called from an IIS web server. The webserver passes key parameters to the Matlab routine. The routine then retrieves information from an SQL database, runs a set of analytical routines and stores the results back in SQL. The IIS server retrieves the results from the database and builds the webpage for the end users.
There is a lot that can be done using this methodology, but it can be complicated making sure that all the variables are properly mapped between Matlab, SQL, and the webpages. If you have an IIS server and an SQL Server up and running and have plenty of time, you can build a very powerful application this way.
However, another project we worked on wanted something much simpler. Just run the analytics and display the results on the website, was the request.
In this case, the website was powered by a content management system written in PHP. It was possible to add PHP pages to the content management system, so we wrote some simple PHP pages that would display a form, save some of the data in temporary files, and pass the rest on to a standalone application written in Matlab.
We ran into some interesting issues along the way. For example, Matlab does not currently support cross compilation for standalone applications. So, we needed to install Matlab on a machine that uses the same architecture as our target production machine. In this case, it was a Linux box. To further complicate things, Linux 32 bit standalone Matlab applications do not run reliably under Linux 64 bit.
Our only 64 Bit test machine we could use was a headless Ubuntu machine. A headless machine is one that you can connect to via Telnet or SSH, but do not have any sort of windows access. The default installation routines for Matlab assume that you have some sort of Windows access, which was not the case for this machine, so we had to jump through a few hoops.
Installing Matlab on a Headless Linux Box
From a regular workstation, I downloaded the various files to install Matlab. They included an installation script, common Matlab code, Matlab code specific to the architecture, and in our case a bunch of toolbox files. I then copied all of these files over to the target machine and on the target machine unpacked the installer.
With all the files in place, the next problem came up. The installer also assumes you have a console you can work from. However, there is an option, -t to run with the installer that will run in a terminal window and this ran fine.
The next issue that I ran into was that Matlab would not start because it could not find the libXp.so.6 library. I poked around and found different ways of installing it depending on the distribution of Linux you are using. For Ubuntu and Debian, apt-get install libxp6 did the trick. Other systems may need yum install libXp or rpm –i xorg-x11-deprecated-libs*-rpm. With this in place, I could run Matlab on my headless server.
Compiling and Distributing Headless Matlab Standalone Applications
To compile the application, I copied the .m and .prj files over from the workstation where I had been doing the development and ran mcc for the project file there, e.g. mcc –F test.prj.
I installed the Matlab Compiler Runtime (MCR) locally and ran the standalone application successfully, with a caveat that I’ll note below. I then installed the Matlab Compiler Runtime on a different machines copied the compiled code over and ran it successfully there. It is worth noting that I needed to run with the option –console to get the MCR to install successfully on a headless machine.
All of my work up to this point had been on Ubuntu machines where I had root privileges. However, the target production machine is a Fedora based machine where I do not have root privileges. For some reason, the installer could not find the JVM. I went through several different approaches, installing the latest version of Java from Sun, running the installer with the -is:extract to get the files and then attempting to run java –jar setup.jar on the extracted files. None of this worked.
In the end I simply tarred up the MCR files from my Ubuntu machine, copied them to the Fedora machine and untarred them there. This worked fine with one other caveat I’ll go into below.
Running the Matlab Application from PHP
I took the shell scripts generated by the compilation and started modifying them for my specific users. For example, I hard coded the MCRROOT value, so it would not need to be passed as a parameter. I redirected the standard output and the standard error files to temporary files. The standard output file always starts with a warning about no display being specified. I didn’t want that information passed on to the websites. I then had the script display the results of the error log file as well as the results file.
The php file called the script, and displayed those results as well as added the code to display the various graphs that are produced by the Matlab analytics. Here is where I get to my next couple of caveats.
When you create a graph in Matlab from a headless machine, you need to turn of the visible display of the graph. To do this, add set(gcf,’Visible’,’off’);. You will also want to adjust the size of the graphic created, otherwise you’ll end up with a really large graphic for your webpage. I used set(gcf,’PaperUnits’,’points’, ‘PaperPosition’, [0 0 480 320]); to create a 480x320 pixel graph. It should be noted that PaperUnits seems to behave differently between Linux and Windows. On Windows a point appears to be about 2.5 pixels and on Linux it is 1 pixel.
With all of this in place I started to get my output. However, when I tried running it from the webpage, I received the final problem that I had to overcome. When Matlab runs a compiled program, it creates an mcrcache directory. Since I had been running this from the terminal, it was being created with my permissions. However, when it was run from the webpage, it used the webserver’s permissions, which did not allow proper access to the mcrcache. To get around this, I created a new mcrcache. I added the line export MCR_CACHE_ROOT=/home/user1/mcrcache to the script to use the new mcrcache.
After I ran the program from the terminal, I went to the mcrcache directory and recursively changed the permissions on all the files so that the webserver would also be able to modify these files. With that completed, my php program running on an Apache server could now successfully call the standalone application and display the results back on the webpage to the end user.
While it was a little complicated to get all the pieces in the right places, once you know the tricks, it becomes fairly easy to deploy Matlab routines onto simple websites. At Toomre Capital Markets, we’re continuing to explore new ways to integrate Matlab and other analytics into the complete user experiences. From time to time, we’ll share some of our tips on how to implement these ideas. If you’re working on interesting analytics integration and want to share ideas or need a little help, please contact us.
(Originally posted at Toomre Capital Markets.)