How to have multiple variable function work with vectors

88 views Asked by At

I'm having trouble dealing with scilab functions ; I have an output in the form of a 1x6 vector, and I'd like to have a simple way to make it work with a 6 variable function.

v = [1,2,3,4,5,6] ;
function z=f(a,b,c,d,e,f)
...
endfunction
f(v) //returns error

Thank you

1

There are 1 answers

0
AudioBubble On BEST ANSWER

Scilab doesn't have a direct analogue of Python's fcn(*v) function call that would interpret the entries of v as multiple arguments.

If you want to be able to call your function as either fcn(1,2,3,4,5,6) or as v = 1:6; fcn(v), then you'll need to add this clause to its beginning:

function z=fcn(a,b,c,d,e,f)
    if argn(2)==1 then
        [a,b,c,d,e,f] = (a(1),a(2),a(3),a(4),a(5),a(6))
    end
// rest of function
z = a+b+c+d+e+f
endfunction

Now v=1:6; fcn(v) returns 21, just like fcn(1,2,3,4,5,6) does.

The condition argn(2)==1 checks if the function received one parameter instead of expected 6; if this is the case, it assumes it to be a vector. If that vector doesn't have enough elements (6) for the tuple assignment, an error is thrown.