Using MATLAB with PHP

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
  • Apache 2.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.

The MATLAB file

First, we need create a very basic MATLAB function that creates a plain text 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);
end
% Close file
fclose(fid);
% Quit MATLAB
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:

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

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

The above 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 start to 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

Thats it. 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.

Notes

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.

The United States would only

The United States would only benefit from the reconnection with the Native Americans because there is far more to the world that could be better pass4sure 646-985 understood through their eyes rather than the shallow opinions of today's society. Native Americans have always been known to be earthy people who maintain a healthy relationship with the earth. If society would reconnect with the Indians it would help in better understanding the present because in order to understand the present fully pass4sure 352-001 one must know the past and understand it completely. The society of United States would greatly benefit from learning from the past mistakes done by either United States or Native Americans. Americans and Native Americans didn't get along very well in the past, but with a little more help and determination a bond may form through personal matters. Personal matters affect all people no matter what race they are. All humans pass4sure 646-671 are affected by their surroundings in some way and this is a way in which all humans relate. To reconnect with the Native Americans would mean that the United States would become closer to the environment and in a way more spiritual towards the world. Indians teaching Americans to be more spiritual isn't like a pass4sure 1Y0-A09 religious spiritual; it is a strong relationship that is constantly growing.

Native Ame

good coding

well i am pleased with your work ..its a nice effort b y you ..actually i was having some problem in this implementation that you have solved for me :) thank you for this.. $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)