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.

3 Kommentare

Guillaume
Guillaume am 22 Jul. 2019
Is the data for each patient always 13 rows (ignoring the 2nd cell), or does that vary per patient?
How is a patient currently identified, is it just the index (1 to 47) of the main cell array?
In fact I have solved the problem "manually" but it was very long ! If you have an idea to make that in a loop...
I would like to have :
R1 = {[Results{1,1}{1,3} Results{1,1}{1,4} Results{1,1}{1,5}]};
R2 = {[Results{2,1}{1,3} Results{2,1}{1,4} Results{2,1}{1,5}]};
R3 = {[Results{3,1}{1,3} Results{3,1}{1,4} Results{3,1}{1,5}]};
R4 = {[Results{4,1}{1,3} Results{4,1}{1,4} Results{4,1}{1,5}]};
...
R47
47 is a length of "List" which is a cell array with the name of the patients.
And :
AL_SL=cat(1,R1{1,1}(:,1),R2{1,1}(:,1),R3{1,1}(:,1),R4{1,1}(:,1),R5{1,1}(:,1),R6{1,1}(:,1),R7{1,1}(:,1),R8{1,1}(:,1),R9{1,1}(:,1),R10{1,1}(:,1),R11{1,1}(:,1),R12{1,1}(:,1),R13{1,1}(:,1),R14{1,1}(:,1),R15{1,1}(:,1),R16{1,1}(:,1),R17{1,1}(:,1),R18{1,1}(:,1),R19{1,1}(:,1),R20{1,1}(:,1),R21{1,1}(:,1),R22{1,1}(:,1),R23{1,1}(:,1),R24{1,1}(:,1),R25{1,1}(:,1),R26{1,1}(:,1),R27{1,1}(:,1),R28{1,1}(:,1),R29{1,1}(:,1),R30{1,1}(:,1),R31{1,1}(:,1),R32{1,1}(:,1),R33{1,1}(:,1),R34{1,1}(:,1));
AM1_SL=cat(1,R1{1,1}(:,2),R2{1,1}(:,2),R3{1,1}(:,2),R4{1,1}(:,2),R5{1,1}(:,2),R6{1,1}(:,2),R7{1,1}(:,2),R8{1,1}(:,2),R9{1,1}(:,2),R10{1,1}(:,2),R11{1,1}(:,2),R12{1,1}(:,2),R13{1,1}(:,2),R14{1,1}(:,2),R15{1,1}(:,2),R16{1,1}(:,2),R17{1,1}(:,2),R18{1,1}(:,2),R19{1,1}(:,2),R20{1,1}(:,2),R21{1,1}(:,2),R22{1,1}(:,2),R23{1,1}(:,2),R24{1,1}(:,2),R25{1,1}(:,2),R26{1,1}(:,2),R27{1,1}(:,2),R28{1,1}(:,2),R29{1,1}(:,2),R30{1,1}(:,2),R31{1,1}(:,2),R32{1,1}(:,2),R33{1,1}(:,2),R34{1,1}(:,2));
AM2_SL=cat(1,R1{1,1}(:,3),R2{1,1}(:,3),R3{1,1}(:,3),R4{1,1}(:,3),R5{1,1}(:,3),R6{1,1}(:,3),R7{1,1}(:,3),R8{1,1}(:,3),R9{1,1}(:,3),R10{1,1}(:,3),R11{1,1}(:,3),R12{1,1}(:,3),R13{1,1}(:,3),R14{1,1}(:,3),R15{1,1}(:,3),R16{1,1}(:,3),R17{1,1}(:,3),R18{1,1}(:,3),R19{1,1}(:,3),R20{1,1}(:,3),R21{1,1}(:,3),R22{1,1}(:,3),R23{1,1}(:,3),R24{1,1}(:,3),R25{1,1}(:,3),R26{1,1}(:,3),R27{1,1}(:,3),R28{1,1}(:,3),R29{1,1}(:,3),R30{1,1}(:,3),R31{1,1}(:,3),R32{1,1}(:,3),R33{1,1}(:,3),R34{1,1}(:,3));
Comp_SL=[AL_SL AM1_SL AM2_SL];
" 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Guillaume
Guillaume am 23 Jul. 2019
Bearbeitet: Guillaume am 23 Jul. 2019

2 Stimmen

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....

1 Kommentar

Anne-Laure GUINET
Anne-Laure GUINET am 24 Jul. 2019
It work perfectly !
Thank you very much for your help :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shubham Gupta
Shubham Gupta am 23 Jul. 2019

0 Stimmen

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 !

6 Kommentare

Guillaume
Guillaume am 23 Jul. 2019
Anne-Laure GUINET comment mistakenly posted as an answer moved here:
Thank you very much for your answers !!!
@Shubham : your solution works perfectly !
Guillaume
Guillaume am 23 Jul. 2019
No offence meant, but this answer is not very good.
  • Hardcoded sizes which means it will only work on the current dataset
  • Arrays growing in a loop which means that the performance gets worse and worse as the number of iteration increases
  • Numbered variables
Anne-Laure GUINET
Anne-Laure GUINET am 23 Jul. 2019
I agree with you Guillaume. My dataset is short so it's ok. But I know it's not perfect...
To respond you :
Is each subcell (corresponding to a patient) always a vector of 13 elements or does the number of elements vary per patient?
My results are located in a cell array named 'Results' where each row contains all the result of one patient. So, 'Results' is a 34*1 cell. (34 patients now).
Each row of 'Results' is a 1*9 cell.
So for example, Results{1,1} is 1 row and 9 colums. The colums that are interested : 3, 4, 5. But the size of that colums could vary per patients. Format x*1 double.
The problem is to extract only the 2nd of the cell array, also cointained in a cell array...
Guillaume
Guillaume am 23 Jul. 2019
Bearbeitet: Guillaume am 23 Jul. 2019
Yes, I've understood the structure of your data. You have a Nx1 cell array of patients. You're saying that this cell array now has 34 patients, so the code you've accepted no longer work since it's hardcoded to 47 patients (the one I've given works regardless of the number of patients). Each cell of this cell array is itself of 1x9 cell array.
One of my question was what do these 9 cells correspond to. If we're converting the data into a table, they need names (e.g. Name, Age, Stimuli, Response, whatever you want it to be).
Each of these 9 cells contain what appear to be a 13xN matrix (except cell 2 which is scalar). However, since you've only shown that for one patient, it's possible that for another patient, it's a different number of rows. Hence, why I asked, is it always 13 rows in each matrices.
Anne-Laure GUINET
Anne-Laure GUINET am 24 Jul. 2019
Bearbeitet: Anne-Laure GUINET am 24 Jul. 2019
Thanks Guillaume. It works also for 47 patients but I have to erase some data because of my algo doesn't work for them.
No, the number of rows is different for each patient.
Anne-Laure GUINET
Anne-Laure GUINET am 24 Jul. 2019
Bearbeitet: Anne-Laure GUINET am 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by