How to pass a cell to a function?

4 views (last 30 days)
Negar
Negar on 21 Dec 2014
Commented: Image Analyst on 21 Dec 2014
Hello,
I have a cell array of size (1344X1). each cell element contains a matrix: first row: 22x40 double, second row 22X52 double,, third row 22X112 double, and so on (My data points are all 22-dimentional). Now I want to pass all elements of the cell array at the same time to a function. I tried the following, but it seems to pass the cell emoluments one by one.
[centers, membership_idx] = myKmeans(data(:,1));
  1 Comment
Guillaume
Guillaume on 21 Dec 2014
It's not very clear what you want to do. All the elements of the cell array is basically the cell array itself, so why can't you pass the cell array?
What does your function take as inputs?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 21 Dec 2014
Edited: Image Analyst on 21 Dec 2014
You do not have any 22 dimensional arrays. Your cell array is a 1-D array. Inside each cell is a 2-D array with 22 rows and some variable number of columns. You might find the FAQ useful.
Since your cell array has only 1 column and 1344 rows, data(:,1) would be the first column, but since it's a column vector it would pass all cells in your cell array, not just one cell. So what you said happens does not agree with what you described. It should be passing the whole cell array but that's not the way it's usually done. Usually to pass a whole array, you don't attach parentheses and indexes to it. You just use the name of the cell array all by itself.
Let's say your cell array is called "data" and you want to pass the entire cell array into some custom function you wrote called "myKmeans()". To do that you'd do:
[centers, membership_idx] = myKmeans(data);
  3 Comments
Negar
Negar on 21 Dec 2014
Oh I just realized that when I use cell2mat, the problem is solved:
%meanstore = cell2mat(data(:,1)');
%[centers, membership_idx] = myKmeansmain(meanstore);
But Im still wondering why it is not possible to pass the cell column, as it is, to the function?
Image Analyst
Image Analyst on 21 Dec 2014
You can pass the cell array to the function. The problem came inside the function when you tried to sum something. Evidently the sum() function does not like cell arrays and you need to extract the matrix with {} or use cell2mat().

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!