Convert array to argument list?

Here's my situation:
I have a function (A) that receives another function name (B) and an array that contains all of the arguments to that function. I need A to call B with the arguments that B needs, but those arguments are in the array. B cannot be changed, and depending on the particular B chosen, the number of arguments vary. What is the easiest way to address this situation. I would've thought there would be some command to convert an array to an argument list, but I can't find one. I think I could construct the argument list on the fly, but I'm not sure how to do that, either.
Thanks for your help

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 26일

7 개 추천

B = [77 34 56]
B = mat2cell(B,1,ones(1,numel(B)))
func(B{:})
If your B has a different shape, you will have to adjust this...

댓글 수: 3

Jack
Jack 2011년 5월 26일
Thanks. I didn't know a cell array would be interpreted as individual arguments like that.
Jack
Jack 2011년 5월 26일
It turns out num2cell places each array entry into a separate cell without the extra arguments needed by mat2cell.
Matt Fig
Matt Fig 2011년 5월 26일
True that! I just gave you the general idea, in case you needed to break the array up in a more complicated way as you didn't specify what the array looked like exactly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2011년 5월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by