MATLAB output to PHP code

2019-06-13 18:00发布

I want to pass MATLAB output to my php code.

My MATLAB code, I have:

function x = returnX()
    x = 100;
end


And my PHP code:

<?php
     $command = "matlab -nojvm -nodesktop -nodisplay -r \"x = returnX();\"";
     passthru($command, $output);
     echo($output)
?>

However, this prints 0, not 100.
When I type the command in my cmd, it shows 100. But when I try it through PHP code, it does not work. Can anyone help me how to set the output value of MATLAB to php variable? Thanks!

2条回答
forever°为你锁心
2楼-- · 2019-06-13 18:47

According to the documentation:

If the return_var argument is present, the return status of the Unix command will be placed here.

You are echoing the return value from the Matlab command, not standard output. Since the command executed correctly, a 0 is returned. passthru() will send the content from standard output "without any interference" to the client.

Also, make sure your hosting provider allows you to make system calls from within a PHP script. Many hosts disable executing server-side commands for security reasons. Check out the support of safe mode and disabled_functions in your php.ini.

查看更多
beautiful°
3楼-- · 2019-06-13 18:50

You should rather use exec, which return the standard output, rather than the exit code like passthru.

display the output in the matlab code:

function x = returnX()
    x = 100;
    display(x);
end

use exec in the php code:

<?php
     $command = "matlab -nojvm -nodesktop -nodisplay -r \"x = returnX();\"";
     $output=exec($command);
     echo($output)
?>
查看更多
登录 后发表回答