MATLAB Answers

0

How to access data that are cells in cells?

Asked by Kelly Kyriakou on 23 Nov 2015
Latest activity Commented on by 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

  0 Comments

Sign in to comment.

2 Answers

Answer by 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

Show 1 older comment
So, each cell of the cell array is itself a cell array. Assuming that each cell of the cell array is a 1x1 cell array of a 1x30 vector:
cc = vertcat(c{:});
m = vertcat(cc{:});
If not, what is
class(c{1}{1})
size(c{1}{1})
It works but it shows again only the data of first row. class(c{1}{1}) = double size(c{1}{1}) = 1 30
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.


Answer by 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

Show 1 older comment
If you want to put your cell array into a table, also consider the cell2table function. I don’t know if it would work with your data, but I’d give it a go.
Also, you need to subscript your ‘output’ variable or it will only contain the last values in your cell array.
Consider:
for i= 1:numel(myData{row}),
output(i) = getmyDatacolumns{i}{i};
end
You will have to experiment with variations on this idea with your cell array (you may need to add an inner nested loop and a second index to ‘output’), since you have it and we don’t.
If I use output(i) = getmyDatacolumns{i}{i} I get this error "Subscripted assignment dimension mismatch."
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.