Extract data from a cell array/struct

53 views (last 30 days)
Good afternoon,
I have been having this probelm all day and I cannot fix it. I am trying to extract the the 3rd column (WaterCumulativeMass) from each of the cell arrays. The problem I am encountering is that the second cell array only contains the headers and no values (which is correct), so to keep consistancy with the other arrays I blank it. This has the effect of turning the cell array into a Struct which I then convert back into a cell array.
The code which works partially (shown) extracts the data from the third column of cell array but combines it into a single column vector where I would like it as columns like it would normally do in a matrix for anything involving a loop function with counter. If I were to use anything with a counter i.e. ABC(i) = array{i}.data(:,3) it states that the left and right sides are not equal or the dot function is not applicable to this etc.
I have tried extracting the data from the struct format aswell but this again will not work
Can someone help please?
% Partially successful cell array method
CC = mydata_EL;
CC{2} = [];
for i = 3:numel(CC); % Needs to start at 3rd column because of the blanks
ABC = CC{i}.data(:,3)% Does extract 3rd columns. Creates a single vector
%ABC(i) = CC{i}.data(:,3) # Does not work with the counter
end
  3 Comments
Richard Rees
Richard Rees on 31 Jul 2019
Edited: Richard Rees on 31 Jul 2019
Apollogies, I didn't realise I saved all the work space variables. Attached is the correct variable (CC.mat)
What I want to do is to extract the third column of the data field from each struct within the cell array and complile it into a matrix, whereby each extraction has its own column.
At the moment I can extract the data but it arranges it into a single vector i.e. 623x1 whereas I need in this instance 191x6
Eva-Maria Weiss
Eva-Maria Weiss on 31 Jul 2019
But since the number of values of both shapes you've written here is not equal, something else might going wrong...

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 31 Jul 2019
What I want to do is to extract the third column of the data field from each struct within the cell array and complile it into a matrix, whereby each extraction has its own column [..] I need in this instance 191x6.
That's a lot clearer and easily done. Except for the 191 which I have no idea where it comes from since each data matrix is 636x3. The following creates a 636x6 matrix:
%concatenate the structure in each cell into a structure array:
data_struct = [mydata_EL{3:end}];
%concatenate the data field of each element of the array into a 3D matrix
data_mat = cat(3, data_struct.data);
%extract column 3 and reshape the Mx1xN array into a MxN array
data_col3 = squeeze(data_mat(:, 3, :));
The first two lines of code use what is known as expansion of cell arrays and structure arrays into comma-separated lists. For example,
[mydata_EL{2:end}]
is automatically expanded by matlab into
[my_data_EL{3}, my_data_EL{4}, .., my_data_EL{end}]
and
cat(3, data_struct.data)
automatically expanded into
cat(3, data_struct(1).data, data_struct(2).data, .., data_struct(end).data)

More Answers (0)

Categories

Find more on Structures 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!