Want to use a vector as parameter to a function, without having to separate its elements

前端 未结 7 742
猫巷女王i
猫巷女王i 2021-01-03 04:35

If I call a matlab function with: func(1,2,3,4,5) it works perfectly.

But if I do: a=[1,2,3,4,5] %(a[1;2;3;4;5

7条回答
  •  渐次进展
    2021-01-03 04:54

    Since arguments to functions in Matlab can themselves be vectoes (or even matrices) you cannot replace several arguments with a single vector.
    If func expects 5 arguments, you cannot pass a single vector and expect matlab to understand that all five arguments are elements in the vector. How can Matlab tell the difference between this case and a case where the first argument is a 5-vector?

    So, I would suggest this solution

    s.type = '()';
    s.subs = {1:5};
    func( subsref( num2cell( otherfunc(b) ), s ) )
    

    I'm not sure if this works (I don't have matlab here), but the rationale is to convert the 5-vector a (the output of otherfunc(b)) into a cell array and then expand it as 5 different arguments to func.
    Please not the difference between a{:} and a(:) in this subsref.

提交回复
热议问题