Collapsing a cell array of cell arrays and matrices into a cell array of mixed data types
2 views (last 30 days)
Show older comments
I am reading in a data file of mixed data types using textscan. The resulting cell array, called DataCols is 1x250 cells consisting of 189x1 cell, double, and uint arrays. The goal is to read this array into the "data" field of a structure consisting of other fields related to the data. The problem is that, because the DataCols cell array contains other arrays, instead of just data columns, using struct('data','DataCols') results in a struct of structs, one struct for each data column from the original file. Instead, I want the struct to return a single structure with a "data" field containing a single 189x250 cell array, the columns of the array correspond to the columns of the original data file.
Is there some way to modify the DataCols cell array to accomplish this before using struct('data','DataCols')? Some code to collapse a level of the DataCols array such that it is a 189x250 cell array of mixed data types, perhaps?
I have been looking at this for a while and have tried a number of different suggestions found in responses to other questions, but none seem to work for this particular problem. Any help would be much appreciated!
EDIT: To clarify, I have a 1x250 cell array "DataCols," where each cell contains a either a cell array, a double matrix, or a uint matrix, all having dimensions 189x1. The goal is to condense this cell array "DataCols" into a cell array of dimensions 189x250.
DataCols currently looks like (Forward slashes are included so the forum will not turn my less-than and greater-than symbols into links)
DataCols /<1x250>:
1 2 3 ...
1 /<189x1 cell> /<189x1 double> /<189x1 uint> ...
And I want it to look like
DataCols /<189x250>:
1 2 3 ...
1 'hello' 67.2342 8 ...
2 'hi' 35.7657 1 ...
3 'goodbye' 21.9872 4 ...
.
.
.
where column 1 corresponds to column 1 of the cell array in row1, column1 of the first version of DataCols, column 2 corresponds to column 1 of the double array in row1, column1 of the first version of DataCols, etc..
Please let me know if this is still unclear!
Thanks
Answers (1)
Mohammad Monfared
on 26 Oct 2013
Hi,
How about this simple code:
for i=1:250
for j=1:189
DataCols_new{j,i} = DataCols{1,i}(j,1) ;
end
end
4 Comments
Mohammad Monfared
on 28 Oct 2013
I don't know what is going wrong here, but I've run exactly the same code and had no problem. new 'DataCols_new' is attached here (as you know rename its extension to .mat). Are you doing something like pre-allocation of 'DataCols_new' cell or something else before these five code lines?
See Also
Categories
Find more on Cell Arrays 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!