how to access data from cell array within another cell array
Show older comments
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
More Answers (2)
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.
1 Comment
Hina Iqbal
on 23 Mar 2015
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
1 Comment
Hina Iqbal
on 23 Mar 2015
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!