日期:2014-05-17  浏览次数:20774 次

使用php在网页执行matlab

通过php调用linux命令,可以实现远程调用matlab.

试验环境:Fedora16, matlab2012b linux version, Google chrome 18.0.1025.108 beta

以下代码实现了在网页输入matlab命令并显示执行结果。注意:如果不加-nodisplay选项,会出现" no display specified"的警告,当然可以在执行matlab命令之前unset DISPLAY也可以直接在terminal执行matlab。

如果你仅仅想远程调用matlab函数,可以参考mathworks的php webservice相关文档,此处也可以参考官方关于python调用的文档。

以下代码对于plot()等绘图命令会出现{Warning: Objects of graph2d.lineseries class exist - not clearing this class or any of its super-classes}错误,目前未解决(也就是无法绘图)。但是通过ssh远程登陆服务器调用相同命令可以plot并save图像,不解。

<html>
    <body>
        <form action="" method="post">
            输入命令: <input type="text" name="cmd">
                  <input type="submit" /><br />
        </form>
    </body>
</html>

<?php
phpinfo();
if(isset($_POST['cmd']))
{
$cmd = $_POST['cmd'];
$command="/usr/local/MATLAB/R2012a/bin/matlab -nodisplay -nosplash -r "."\"".$cmd."\"";
//echo $command."<br/>";
//$command="/usr/local/MATLAB/R2012a/bin/matlab -r "."\"".$cmd."\"";
shell_exec("unset DISPLAY");//删除DISPLAY环境变量
$out = shell_exec($command);
$str="< M A T L A B (R) >
                  Copyright 1984-2012 The MathWorks, Inc.
                    R2012a (7.14.0.739) 32-bit (glnx86)
                              February 9, 2012

 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.";
$out=ltrim($out,$str);
$out=rtrim($out,"
>>");
echo $out;
}
?>

相关截图:                                                                                                                                                                                                                                                                                            

输入sin(1:10)进行简单计算