Question

I use matlab mcc to create a standalone application exe file, then I use php to call the exe file. but I can't get the function return value,it's always empty!! here is my test example in m file

function result=mysum(in)
if nargin<1
    in=[1,2,3];
else
    in=str2num(in); 
end
result=sum(in);
end

then I use the command mcc -m mysum.m to create exe file(I have already configured the matlab compiler).

here is the php file

    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    <?php  
    exec('F:\myevm\apache\htdocs\shs.exe [2,2,3,3,3] [4,4,4,4,4] 356 1567 1678',$ars);
    echo '<br>';
    echo $ars[0];

    ?>

    </body>
   </script>
   </html> 

however ,the $ars[0] is always empty!! I tried to find answer by myself or through the Internet,but failed . give me a help, thanks.

Was it helpful?

Solution

Note two things:

  1. You have your function set up to accept a single input argument.
  2. When you run an application from the Windows command line, arguments are passed in as strings.

So if you type mysum 1 (either in MATLAB on the uncompiled program, and I would guess also if you do this from the Windows command line on the compiled program, although I haven't tested this) it will work, giving the answer 1, and if you type mysum [1,2] it will work, giving the answer 3. Note that mysum [1,2] is different from mysum([1,2]), as it is being passed the string '[1,2]', not the array of doubles [1,2].

But if you type mysum 1 2 it will fail, as you are now passing two string input arguments in, and your function is set up to only accept one.

Rewrite your function so that it accepts a variable number of input arguments (take a look at varargin to achieve that), applies str2num in turn to each of the inputs (which will be varargin{1} to varargin{n} if you've used varargin), and then sums them individually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top