next2u next2u - 2 years ago 131
PHP Question

Unexpected output while calling octave script from php

I am calling Octave script from PHP and passing parameters to it. But I get the unexpected output.

The PHP code I'm using to pass arguments and call the Octave script is:

$cmd = "C:\Octave\Octave4\bin\octave-cli C:\wamp\www\dspace\add.m $a $b";
$ex = passthru($cmd, $op);

My Octave script:

arglist = argv();
a = arglist{1};
b = arglist{2};

function f (a,b)
a + b


The output get is:

ans = 107

Expected output is:


How can I fix this?

Answer Source

You are adding the ASCII codes for the character "8" which is 56 and "3" which is 51 so the result is 107. Convert the strings to numbers:

arglist = argv();

a = str2num (arglist{1});
b = str2num (arglist{2});

function ret = f (a,b)
  ret = a + b;

printf("%i", f(a,b));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download