How to pass cell array as input to function

35 vues (au cours des 30 derniers jours)
Niharika  Nayak
Niharika Nayak le 19 Déc 2019
Modifié(e) : Adam Danz le 20 Déc 2019
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 commentaire
James Tursa
James Tursa le 19 Déc 2019
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?

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 19 Déc 2019
Modifié(e) : Adam Danz le 20 Déc 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 commentaires
Adam Danz
Adam Danz le 20 Déc 2019
Modifié(e) : Adam Danz le 20 Déc 2019
Ah... good.
Niharika  Nayak
Niharika Nayak le 20 Déc 2019
?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by