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.
- $filename = the filename submitted by the user from our form
- $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)
- $outpurDir = the path to the folder we are going to create our text file in
- $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.
- exec($command) executes our command line string as if you'd entered it in the command window
- 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.

I'd heard at one point that
I'd heard at one point that you should either not animadversion your code, or band the comments from the cipher afore production, as it saves the PHP engine from scanning abandoned lines HP0-Y20. Is this accustomed practice? I tend to animadversion heavily and am analytical just how abundant of a aberration the commenting makes at the end of the day JN0-342. Anybody accept activated affirmation one way or the other? I would brainstorm there is a difference, because as you say the PHP engine will accept to browse these animadversion lines JN0-400. But I'll bet the aberration is a amount of milliseconds, so you accept to ask yourself if those milliseconds you ability save are added important than the minutes/hours/days you ability decay aggravating to amount out what assorted blocks of uncommented cipher are there for.
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 642-453 exam solved for me :) thank you for this.. $inputDir = the path to the folder that you saved phpcreatefile.m 646-656 exam in (this is only really necessary if the function phpcreatefile is saved in a folder that 70-271 exam is not included in PATH)