How to pass cell array as input to function

조회 수: 36 (최근 30일)
Niharika  Nayak
Niharika Nayak 2019년 12월 19일
편집: Adam Danz 2019년 12월 20일
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
James Tursa
James Tursa 2019년 12월 19일
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?

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

채택된 답변

Adam Danz
Adam Danz 2019년 12월 19일
편집: Adam Danz 2019년 12월 20일
" 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
Adam Danz
Adam Danz 2019년 12월 20일
편집: Adam Danz 2019년 12월 20일
Ah... good.
Niharika  Nayak
Niharika Nayak 2019년 12월 20일
?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by