How to pass cell array as input to function

I have a cell array which contains 10 {1x1 cell} cells now I want to pass this cell array as input to a function and access the element inside the {1x1 cell} how should I pass the cell array?

1 Comment

You pass it just like you would pass any other variable. And extract the contents using the curly braces { } inside your function. Can you show us the code that is giving you problems?

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 19 Dec 2019
Edited: Adam Danz on 20 Dec 2019
" I want to pass this cell array as input to a function and access the element inside"
There are (at least) three interpretations of that.
Pass each element in as a single input
C = arrayfun(@(x){x},1:10); % demo data
z = myFun(C{:});
function z = myFun(a,b,c,d,e,f,g,h,j,k)
z = a+b+c+d+e+f+g+h+j+k;
end
Condense the inputs into a vector and pass as 1 input
C = arrayfun(@(x){x},1:10); % demo data
y = myFun2([C{:}]);
function z = myFun2(n)
z = sum(n);
end
Pass the entire cell array
C = {1,2}; % demo data
y = myFun2(C);
function z = myFun2(n)
z = C{1} + c{2};
% or
% z = sum([C{:}]);
end

3 Comments

thanks for the solution Pass the entire cell is the solution of my problem. It works
Adam Danz
Adam Danz on 20 Dec 2019
Edited: Adam Danz on 20 Dec 2019
Ah... good.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Dec 2019

Edited:

on 20 Dec 2019

Community Treasure Hunt

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

Start Hunting!