Cannot run any matlab function when using php shell_exec matlab

2 views (last 30 days)
I am using php to build a website that can calculate some convex programming. However, when I tried to connect php and matlab, I can execute matlab and some of its command, but I cannot run any matlab functions.
I am using SSH, Windows 7, Matlab R2012a. The following are some of my observation:
I cannot execute any matlab function(function in .m file) via php, but I can run simple command such as cd, 3*3 by matlab via our php code. I also try to run matlab in UNIX using SSH. I can successfully run both matlab function and command. One more thing is that when I cd in php calling matlab, it shows a directory (/misc/uac_std/fyp/y13/fyp_dt/public_html/) which is different from the normal directory (/uac/std/fyp/y13/fyp_dt/public_html)
In php, every time I run matlab, I have this warning message - Check directory permissions.Warning: failed to create prefere nce directory /root/.matlab/R2012a. I have searched in google for it and try to make directory manually to create .matlab/R2012a in the home directory (cd~), but it did not help.
When running matlab in both SSH and php, I found that we run different version of matlab. When I run it in SSH, the version is R2009a. When php, the version is R2012a. I do not know whether it is related to our problem or not, but it's just strange. And also, the matlab I use in my college is R2011b...
Here is my php code:
if(isset($_POST['filepath'])) {
$filename = $_POST['filepath'];
$command="/usr/local/bin/matlab -nodisplay -nosplash -r \"test()\"";
$output=shell_exec($command);
print "$output\n"; } Can someone help me?

Answers (1)

Jason Ross
Jason Ross on 22 Jan 2013
Edited: Jason Ross on 22 Jan 2013
There are mutiple things going on here.
  • Make sure the startup options are appropriate. There are some examples here: http://www.mathworks.com/help/matlab/matlab_env/startup-options.html
  • When you are using ssh to connect and using PHP code, it seems things are happening under different user contexts. It also looks like the PHP code is being executed under the context of a web server, which is going to have a different root than the ssh context. The path for these contexts is different (one points to one install of MATLAB, the other points to another)
  • It seems that things are being executed under the user context of the "root" user on the machine(!) when you are starting MATLAB in the php context, that's why you get the message about trying to create the preference files -- /root is root's home directory.
I'd suggest stepping back and running commands like "whoami" (to determine what user context is where), "env" ("to determine what system path you are dealing with), and "which matlab" (to see where the MATLAB you are getting is coming from), and go from there. You also likely need to investigate the MATLAB path, as well as make sure that the files you are trying to execute are on a directory path you have access to, and that if they rely on other files, that the MATLAB path is appropriately configured to find them.
  5 Comments
Jason Ross
Jason Ross on 23 Jan 2013
Since you are starting out with MATLAB, PHP, and by extension, UNIX/Linux, I'd really suggest you seek out someone with adminstrative privelages on the server you are trying to use and show/tell them what you are trying to do. Since the server is shared with other users, it's likley they need to make changes for everyone to make sure that PHP is configured properly. I'd hope they will thank you for finding what could be a misconfiguration that allows PHP code to (possibly) run as root.

Sign in to comment.

Categories

Find more on Manage Products in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!