how to access data from cell array within another cell array

6 views (last 30 days)
I have a cell array e.g;
C:5x1 cell
[57x1 double]
[29x1 double]
[12x1 double]
.
.
and when i open it further e.g;1 cell as
C{1,1}<57x1 double>
i get values as
[3.23]
[2.43]
[4.51]
.
.
upto 57 row.
i need to ask how to access these values??
the size of C in my case is not as small as i have written in this example

Accepted Answer

Jos (10584)
Jos (10584) on 22 Mar 2015
I have trouble recreating your data:
C = {{1 2 3}{10 11}}
gives me a 1x2 cell array with cells as elements:
% C = {1x3 cell} {1x2 cell}
% C{1} → ans = [1] [2] [3]
Here are some suggestions how you can deal with such cell array of cell arrays with scalars:
x = [C{1}{:}] % convert a single cell to a double array
C2 = cellfun(@(x) [x{:}], C, 'un',0) % convert to cell array of double arrays

More Answers (2)

dpb
dpb on 22 Mar 2015
Depends on how you want to access, them...by cell by element is
C{indxCell}(indexInCell)
indexInCell is any range of valid indices but indxCell must but a single cell reference to use the nested addressing.
Examples:
>> x{1}=rand(3,1);x{2,1}=randi(4,2,1);
>> x{:} % each cell is an array
ans =
0.2769
0.0462
0.0971
ans =
4
3
>> x{1}([1 3]) % first cell, first and 3rd elements
ans =
0.2769
0.0971
>> x{:}(2) % try the 2nd element of all cells...no go
Bad cell reference operation.
>> y=cell2mat(x) % put all into a single array
y =
0.2769
0.0462
0.0971
4.0000
3.0000
>>
After the last, "ordinary" colon addressing for the full set is obvious but you've lost the ability to differentiate which came from which initial cell if that were to be important. So, upshot is, no unique way, it depends upon what you're after, more specifically.

Stephen23
Stephen23 on 23 Mar 2015
It seems you have a simple cell array of double arrays. It is easy to access the data in a cell array using cell indexing.
First we create some fake data:
>> C{1,1} = [3.23;2.43;4.51];
>> C{2,1} = (1:60).';
>> C{3,1} = rand(50,1);
>> C
C =
[ 3x1 double]
[60x1 double]
[50x1 double]
now we can use cell array indexing to extract any of the numeric arrays:
>> C{1}
ans =
3.23
2.43
4.51
And this can be immediately followed by some standard matrix indexing to select only the particular elements that you want to work with:
>> C{1}([1,3])
ans =
3.23
4.51

Community Treasure Hunt

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

Start Hunting!