Sending comma seperated list of vectors to a variable input function

1 view (last 30 days)
Hello all,
I have a function F that can take a variable number of vectors as input. I.E. it can take F(1:4) or F(1:4, 1:3) or F(1:4, 1:3, 1:2) ... etc.
I would like to be able to send it a variable number of inputs from a script. That is, sometimes the script will call it with 3 vectors as input, or sometimes 10 vectors, etc.
I've tried to use mat2cell which I think would work, but I am having trouble formatting it properly, perhaps I do not understand the help page for it.
Say I have a vector B
>> B = [1:4; 1:4; 1:4]
B =
1 2 3 4
1 2 3 4
1 2 3 4
And I want to call F(1:4, 1:4, 1:4). How do I do this?
I've tried
X = mat2cell(X, 3, 4)
and called F(X{:}), but this does not provide the desired output.
If you can provide any help I thank you in advanced!

Accepted Answer

Jan
Jan on 15 Jul 2013
Edited: Jan on 15 Jul 2013
This seems to be a problem of your mat2cell call.
B = [1:4; 1:4; 1:4]
X = mat2cell(B, [1,1,1], 4);
Now X is {1:4, 1:4, 1:4} and this works:
F(X{:})
  1 Comment
Mark
Mark on 16 Jul 2013
Aha, so I did misunderstand the mat2cell call. Thank you this works exactly as I want it to.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 15 Jul 2013
Look up varargin. It's whole purpose in life is what you're describing.
  2 Comments
Jan
Jan on 15 Jul 2013
As far as I understand, Mark's problem does not concern getting the list of inputs inside the function, but providing a list of inputs from the caller.
Mark
Mark on 16 Jul 2013
Jan Simon's has the correct interpretation of my question. Thank you for your help though.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!