如何使用php执行Octave文件 [英] How to execute an Octave file using php

查看:117
本文介绍了如何使用php执行Octave文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Octave文件 sr.m

I have an Octave file sr.m

sqrt(3^2 + 4^2)

我必须使用 PHP 执行此文件,并在浏览器中显示输出. sr.m 文件位于Desktopoctavepgm文件夹中.

I have to execute this file using PHP and display output in browser. The sr.m file is resides in octavepgm folder in Desktop.

我的 php 代码是

$cmd = "octave3.6.4 -qf C:\\Users\\deepu_000\\Desktop\\octavepgm\\sr.m";
$ex = passthru($cmd, $output);
var_dump($output);

这将给出int 1作为输出.

This gives int 1 as output.

如何使它正常工作.请帮助我.

How can I make this working..please help me..

预期输出为5.

推荐答案

我会在下面进一步解释几个问题,但是您可以通过以下方法来解决问题:

There's several issues which I explain further down but here's how you do it:

$ php -a
Interactive mode enabled

php > $cmd = "octave -qf --eval 'printf (\"%f\", sqrt (3^2 + 4^2));'";
php > $output = exec ($cmd);
php > var_dump ($output);
string(8) "5.000000"

问题1:

您正在使用passthru(),但是如果要捕获八度的输出,则应该使用exec().这在php手册中进行了说明(强调):

You are using passthru() but if you want to capture the output of octave, you should be using exec(). This is explained on the php manual (emphasis added):

passthru()函数类似于exec()函数,因为它 执行命令.该函数应代替exec()或 当Unix命令的输出是二进制数据时,使用system() 需要直接传递回浏览器.

The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.

问题2:

如果只希望从Octave获得结果,则应使用printf(),否则将显示变量,包括ans =部分.另外,使用printf(),您可以更好地控制输出的格式.

If you only want the result from Octave, you should use printf() otherwise you get the display of a variable, including the ans = part. Also, with printf() you can better control the format of the output.

问题3:

对于这种简单情况,请使用Octave的--eval选项.

For such simple cases, use Octave's --eval option.

这篇关于如何使用php执行Octave文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆