Home > Mobile >  Matlab batch not passing arguments to function
Matlab batch not passing arguments to function

Time:09-17

Thank you in advance for any help you can give. I am relatively new to Matlab and am stuck with the use of the batch command for passing arguments to the function. I have tried to simplify the example as much as possible.

Following the documentation, I can get it to work with a built-in function:

>> 
>> j = batch(@zeros,1,{10,1});
>> a=fetchOutputs(j)

a =

  1×1 cell array

    {10×1 double}

>> a{1}

ans =

     0
     0
     0
     0
     0
     0
     0
     0
     0
     0

I then created a simple stand-along function runTest.m:

function result = runTest(n1,n2) 
result = zeros(n1,n2);
end

It works with a direct call:

>> a=runTest(5,1)

a =

     0
     0
     0
     0
     0

But, it doesn't work using batch:

>> j = batch(runTest,1,{5,1});
Not enough input arguments.

Error in runTest (line 2)
result = zeros(n1,n2);
 

Any suggestions? Thank you.

CodePudding user response:

When you do

j = batch(runTest,1,{5,1});

then the part of that statement that reads runTest tries to call the function runTest with no input arguments. This is where the error happens. It never gets to call batch.

Like in just about every other programming language, the arguments to a function call are evaluated before the function is called, evaluating runTest is the same as evaluating runTest(), MATLAB makes no distinction between the two.

What you intended to write was

j = batch(@runTest,1,{5,1});
  • Related