Matlab via the WWW -- Mocha

Introduction

We have developed a procedure for using Matlab and a browser together as clients for graphical applications. The browser can This software provides a Matlab analogue to Java, allowing us to take advantage of all of Matlab's computational and graphical capability. It will work with Mosaic as well as Netscape, but does require a UNIX system. Following Tom's suggestion, we shall call the software Mocha.

However, the security of the software has not been thoroughly tested. (I have heard of security holes in Java, too, and people suggest you only look at Java applets from known sources.) Not many people know about Mocha at the moment, so that's some help. In addition, you could install it to run as "nobody" (as outlined in the installation notes) which stops a lot of potential problems, since the Matlab code will not have write permission on your own directory. Causes other problems, though - it's hard to get rid of processes if you forget to Quit, and, for some applications, one might want to run scripts as well (which requires real trust). Finally, the best procedure is probably to enable the software only when you plan to look at specific trusted material and disable it afterwards. The procedure is described below.

Principles

The Mocha software is a program, mq, which can accept information from the WWW in much the same way as Ghostscript, xv, or another "helper" external viewer. It works with the MIME type application/x-matlabqueue. The program recognizes various commands in the data stream and can save files in /tmp/mocha-$USER, begin a slave Matlab process, pass Matlab command lines to the process (using a named pipe), remove the files, and terminate the subprocess.

Installation

There are various approaches to installation: We shall discuss here the case when someone on your cluster already has the software installed. You need that user's name. On geosys, it's ecosys, while on the 14/15 floor machines, it's glenn and on the 16/17th floor it's lab. In the instructions below, replace owner by this user's name. If no one in your cluster has Mocha, you need to install it using the procedure found here.

Setup: (this is done once by each user - before starting a browser) Type
~owner/mocha/install
Enabling Mocha: (this can be done in another window while a browser is running) When you wish to view a Mocha procedure, type
~owner/mocha/enable
Disabling Mocha: (likewise) When you are done, type
~owner/mocha/disable

Note: Before disabling, you should have quit your Mocha process, so that the Matlab window has gone away; otherwise, you may have a stray process and the working area in /tmp hanging queue around. If you forgot, you can get rid of these by either
  1. Load the browser and page back in and hit the Stop Matlab button
  2. Or, if that does not work,
    rm -r /tmp/mocha-$USER
    
    Look for mq, matlabqueue or Matlab processes and kill them.

Example:

Once you've gone through the installation, you can test things out by enabling the software and clicking the following links in order:

which should download /tmp/test.m, /tmp/test2.m and start Matlab.

Enter a function in the box: f(x)=

Enter a range for the dependent variable:

and press to plot the graph.

should remove /tmp/test.m,/tmp/test2.m and stop Matlab.

A less trivial example can be found here.

Writing Mocha programs and serving them

Mocha programs consist of
1) Straight .m files which are downloaded at the beginning. Put these in your public Web page directory or one of its subdirectories. On geosys, that's ~/public_html while it's ~/web on 14/15. The hyperlink in the HTML text for beginning the Matlab process looks like

<a href="http://geosys.mit.edu/cgi-bin/mocha/matsetup/~xxx?file(s)_to_transfer[;file_to_execute]" > Begin </a>

where xxx is the combination of your username and the subdirectory (e.g., ~glenn/mocha). The file(s)_to_tranfer is a comma-separated list of files or shell-type specifications like *.m

2) Edited .m files which contain pages of Matlab code which are transferred for execution. Put these in the same directory. The hyperlink in the text looks like

<a href="http://geosys.mit.edu/cgi-bin/mocha/matexec/~xxx?file_to_execute" > Plot </a>

or
<form action="http://geosys.mit.edu/cgi-bin/mocha/matexec/~xxx?file_to_execute" method="POST">

These files are passed through an editor which replaces all ${variable_name} symbols with the value extracted from the field on an HTML form with name=variable_name, the same name. I usually use a suffix such as mm or mx for these files so that they are not copied over in the initialization (where I use *.m).

3) Closedown procedure. The hyperlink in the text looks like

<a href="http://geosys.mit.edu/cgi-bin/mocha/matquit" > Quit </a>

View the source of this page (particularly the example) to see how this works. Here are the .m files:

test.m
% set up the range
xvals=xr(1):(xr(2)-xr(1))/500:xr(2);

% generate the function - here we do it point by point so that the
% user need not know Matlab's .* notation, but that wouldn't be
% necessary if they were taught the notation.
fx=[];
for x=xvals
  fx=[fx,eval(f)];
end

% go to plotting
test2

test2.m
% make plot
plot(xvals,fx);
title(f);

testexec.m
% get values from HTML form
xr=[${xr}];
f='${f}';

% run program
test;


12.822 examples

(from MIT's course 12.822)

12.804 examples

(from MIT's course 12.804)

glenn@lake.mit.edu