MATLAB Answers

How to access data that are cells in cells?

60 views (last 30 days)
Kelly Kyriakou
Kelly Kyriakou on 23 Nov 2015
Commented: Star Strider on 24 Nov 2015
Hello,
I have a variable that contains 931x1 cells and each cell contain 1x30 doubles. When I open the variable from workspace I see this: 1x1 cell 4x1 cell 4x1 cell and when I click on a cell I see 1x30 double How I can access these data? I tried with cat command but this create one cell array in which all cell are in one column and I want to a table 932x30. I tried also to access with {} but this take only the first cell and show it like [1x30 double]. How I could to acces all the cells?
Thank you very much for any help

Answers (2)

Guillaume
Guillaume on 23 Nov 2015
If you indeed have a 931x1 cell array and each cell contains a 1x30 vector, then
m = vertcat(c{:}) %where c is your cell array
will work. If it doesn't then you need to give us the output of
size(c)
class(c{1})
size(c{1})
  4 Comments
Guillaume
Guillaume on 23 Nov 2015
Sorry, made a typo, wrote a 1 instead of :. Now corrected.
Basically, because you've got cell arrays wrapped into a cell array you need to expand twice, c{:} each time.
Ideally, you'd correct the code that generated that cell array to remove the double wrapping.

Sign in to comment.


Star Strider
Star Strider on 23 Nov 2015
Without seeing your data, it’s not possible to give a definitive Answer.
However, you can do multiple subscript reference addressing with cells, for example:
D = A{1}{1}
D = A{1}(1)
You will have to experiment with your own data. The cell2mat function is also an option for you to consider.
  4 Comments
Star Strider
Star Strider on 24 Nov 2015
None of your data that I looked at in your (931x1) cell are numeric. The contents are all strings, and they appear to be data on different major highways in some nation (I don’t recognise the designations).
This code for instance:
D = load('Kelly Kyriakou matlab.mat');
Cell = D.myData;
VarNames = Cell{1}(1:5)
S2 = Cell{2}(1:5)
S3 = Cell{3}(1:5)
produces this output:
VarNames =
'name'
'link_id'
'link_key'
'speed_sensor_id'
'count_sensor_id'
S2 =
'A2'
'1'
'1_AB'
''
''
S3 =
'A2'
'1'
'1_BA'
''
''
If you want to create a table with them you could do something like this:
T = table(S2,S3,'RowNames',VarNames)
T =
S2 S3
______ ______
name 'A2' 'A2'
link_id '1' '1'
link_key '1_AB' '1_BA'
speed_sensor_id '' ''
count_sensor_id '' ''
The data in the individual subcells (that I called ‘S1’ and ‘S2’ here) are mostly different lengths, so I have no idea how to suggest you deal with them. The table function requires that they all be the same length.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!