PHP and Matlab

The following example describes how to use PHP to run MATLAB commands.With there being so many different possible system configurations,it is likely that you may have to modify these steps slightly for your own use. I am using:

  • MATLAB R2009a
  • Windows Vista
  • Apache2.2.11

In this example, we will create a MATLAB function that generates a text file of random numbers. The location of this file is user defined from an HTML form. We use PHP to link the two together.

Edit: I’m getting a few messages about this solution. Please note that it was written in 2010 and a lot has changed in the last 4 years. I do plan to come back and update this article eventually, time permitting. But the solution may not work on latest software and your mileage may vary.

The MATLAB file

First,we need create a very basic MATLAB function that creates a plaintext file. The only input is a filename. Obviously, you’d want some slightly more robust checks normally. Using any plain text editor,copy and paste the following code and save as phpcreatefile.m (make a note of the file path as you’ll need it later):

function phpcreatefile(filepath)
% Open the file
fid = fopen(filepath, 'wt');
for i = 1:100
    % Create random number
    randNumber = [num2str(rand(1)) '\n'];
    % Write number to file
    fprintf(fid, randNumber);
% Close file
quit force

Notice the last line of this file. The ‘Quit’ command is necessary to terminate MATLAB when we’re finished.

The PHP file

Next,we need to create a file that renders an HTML form that, when submitted, opens MATLAB and calls the function phpcreatefile with a filename taken from the form. copy and paste the following code into a php file:

        <form action="" method="post">
            Enter a filename <input type="text" name="filepath"><br />
            <input type="submit" /><br />

if(isset($_POST['filepath'])) {
    $filename  = $_POST['filepath'];
    $inputDir  = "C:\\output";
    $outputDir = "C:\\output";
    $command = "matlab -sd ".$inputDir." -r phpcreatefile('".$outputDir."\\".$filename."')";
    echo "The following command was run: ".$command."<br/>";
    echo $filename." was created in ".$outputDir."<br/>";

Theabove code first creates an extremely basic HTML form that asks the user for a filename. When submitted, the form submits to self,making the filename available as a $_POST variable. Next, a block of PHP code checks to see if the form is submitted. If it is, we startto try and build the appropriate command to send to MATLAB.

  1. $filename= the filename submitted by the user from our form
  2. $inputDir= the path to the folder that you saved phpcreatefile.m in (this is only really necessary if the function phpcreatefile is saved in a folder that is not included in PATH)
  3. $outpurDir= the path to the folder we are going to create our text file in
  4. $command= Here, we’re building a command line string required to run MATLAB. Lets break it down a bit further:
    • matlab= the command we pass to CMD in order to start MATLAB
    • -sd”startdir” specifies the startup directory for MATLAB:in this case we set it to match the location of our function phpcreatefile
    • -r”statement” starts MATLAB and executes the specified MATLAB statement.
  5. exec($command)executes our command line string as if you’d entered it in the command window
  6. Next are a couple of lines that report back to our user what MATLAB has just done

Thatsit. You just ran MATLAB using PHP

Found this useful? Do you have comments or possible improvements to this example? Please leave your comments at the bottom.


One thing that is immediately apparent is that at no time can MATLAB be seen running while the above code executes. This is because PHP runs as a module of an Apache service. Vista (and maybe other systems too) has a security layer that deals with how services interact with the desktop. The problem is better described here.