# Create dataset from multiple Cell arrays

21 views (last 30 days)
Anne-Laure GUINET on 22 Jul 2019
Commented: Anne-Laure GUINET on 24 Jul 2019
Hi everybody,
I have a problem to better structure my data...
My results are located in a 47×1 cell array. In fact, each rows is one patient's results. Each patient have nine colums of results, also located in a cell arrays. So, I have 47 patients. For each of them, 9 cell arrays of results.
I would like to create an unique dataset that contains the 3, 4, 5th cell arrays of each patient. After I would like to export this dataset to csv file.
I'm sorry I can't share my code because it's patient's data, but I attach captures of the cell arrays.
Hope you can help me.
Thanks.
Guillaume on 23 Jul 2019
" If you have an idea to make that in a loop..."
It's possible that a loop is not even needed and that it can be done easily and efficiently in just a few lines of code.
But to answer you, I need you to answer my questions. So, again:
Is each subcell (corresponding to a patient) always a vector of 13 elements or does the number of elements vary per patient?
Another way to answer that is to give us the output of:
pl = cellfun(@(c) numel(c{1}), Results);
min(pl)
max(pl)
Also, what do the 9 elements correspond to? The easiest way to export your data to csv would be to convert your data to a table, which will require names for the 9 columns, so what should they be named?
A table would also make it much easier for you to analyse the data than cell arrays of cell arrays.

Guillaume on 23 Jul 2019
Edited: Guillaume on 23 Jul 2019
For lack of answers to my questions, here is a more efficient method than the accepted answer:
allpatients = vertcat(Results{:}); %convert in a 47x9 cell array
desireddata = cell2mat(allpatients(:, [3, 4, 5]));
A better way (for further processing) would have been to convert to a table but answers neeeded....
Anne-Laure GUINET on 24 Jul 2019
It work perfectly !
Thank you very much for your help :)

Shubham Gupta on 23 Jul 2019
I think you can simply achieve this by a for loop. Try this :
AL_SL = [];
AM1_SL = [];
AM2_SL = [];
for i = 1:47
AL_SL = [AL_SL;Results{i,1}{1,3}];
AM1_SL = [AM1_SL;Results{i,1}{1,4}];
AM2_SL = [AM2_SL;Results{i,1}{1,4}];
end
Comp_SL=[AL_SL AM1_SL AM2_SL];
I hope this helps !
Anne-Laure GUINET on 24 Jul 2019
Exept for cell 2 which is always a scalar.
The cell 1 is a time vector.
The cell 2 is a percentage.
The cells 3, 4 , 5 correspond to step length given by different method of calcul.
The cells 6, 7, 8, 9 correspond to time for column 1 and step length for column 2, calculated with 4 different method.
So I have to create a name vector ?
Name=('time', 'percentage'...) % etc for the 9 cells