Indexing in complex cell arrays
3 views (last 30 days)
Show older comments
I have a 3275x1 cell array that looks like the following (see attatched images). I'm trying to extract only the values on the first column when deep into the cell. For instance, I tried the following code that didn't work:
x = a{:,1}{:,1}{1,1}
This is me trying to isolate only the values in the first column of the part of the cell you can see in image a{1,1}{1,1}. In the code above, I tried to tell matlab that I want all rows, but just for that single column, to be isolated.
To clarify, in image a{1,1}{1,1}, the value in the first column is an x-value, the value in the second column is a y-value, and the value in the third column is a t-value. I'm trying to put all the x-values into one cell array, all the y-values in another cell array, and all the t-values in another cell array.
To give more examples:
a{1,1}{1,1}{1,1} would be an x-value.
a{1,1}{1,1}{1,2} would be a y-value
a{1,1}{1,1}{1,3} would be a t-value
a{1,1}{2,1}{1,1} would be an x-value
a{1,1}{2,1}{1,2} would be a y-value
a{1,1}{2,1}{1,3} would be a t-value
(etc).
I'm fairly new to matlab, so i'd be greatly appreciative of anyone who could point me in the direction of some resources to help me learn how to analyse this data (or, of course, any code which can perform the task I want.).
Thanks so much!
0 Comments
Accepted Answer
MarKf
on 1 Feb 2023
To simplify, since you are working with cells that contain only single colums or rows (and not multiple like in a matrix), a{1} would be the same as a{1,1}.
x = a{:,1}{:,1}{1,1} would then be (in this case when you have only 1 column or row) the same as x = a{:}{:}{1} which anyway still does not work, it shouldn't, since cells can have different sizes (or data types) within and cannot be simply extracted to vectors (or matrices).
What do you want your variable x above to be? I assume a column vector with all the x-values stacked vertically. That is the 312 x-values contained in a{1}{1}, a{1}{2} ... a{1}{312} followed by the 245 ones in a{2}{1}, a{2}{2} ... a{2}{245} and so forth.
with loops:
x = []; for i1=1:numel(a), for i2=1:numel(a{i1}), if isnumeric(a{i1}{i2}{1}), x= [x; a{i1}{i2}{1}]; end; end; end
which can give you some control on what to do in case there is missing data (ignore it in this case or else x=[x; NaN];). Remove the ; after the x to have it as a row vector with all the x-values stacked horizontally. You could do the same with cellfun, vertcat or cell2mat but it's not better if you are just starting
0 Comments
More Answers (1)
KSSV
on 1 Feb 2023
% Make dummy data for demo
a = cell(10,1) ;
for i = 1:10
m = randsample(5:10,1) ;
b = cell(m,1) ;
for j = 1:m
b{j} = {rand(1,3)} ;
end
a{i} = b ;
end
% convert
iwant = vertcat(a{:}) ;
iwant = cell2mat(vertcat(iwant{:})) ;
x = iwant(:,1) ;
y = iwant(:,2) ;
t = iwant(:,3) ;
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!