extract and split data from cell into multiple cells.
22 views (last 30 days)
Show older comments
So what I want to achieve is this:
I have a 1x3 cell of the form (date , hour, name):
so myCell has 3 100x1 cells;
ex : myCell (100x1 cell, 100x1 cell,100x1 cell):
07.03.2019 20:30 a
07.03.2019 20:31 b
07.03.2019 20:32 c
07.03.2019 20:33 a
07.03.2019 20:33 b
07.03.2019 20:34 c
07.03.2019 20:34 b
07.03.2019 20:35 c
07.03.2019 20:35 a
07.03.2019 20:36 c
I want to separate all my data from myCell into 3 diffrent cells each containing a or b or c;
ex:
myNewCellA:
07.03.2019 20:30 a
07.03.2019 20:33 a
07.03.2019 20:35 a
...
... myNewCellB with b, and myNewCellC with c;
Thank you in advance.
0 Comments
Accepted Answer
Star Strider
on 8 Mar 2019
Edited: Star Strider
on 8 Mar 2019
One approach:
C = {'07.03.2019 20:30' 'a'
'07.03.2019 20:31' 'b'
'07.03.2019 20:32' 'c'
'07.03.2019 20:33' 'a'
'07.03.2019 20:33' 'b'
'07.03.2019 20:34' 'c'
'07.03.2019 20:34' 'b'
'07.03.2019 20:35' 'c'
'07.03.2019 20:35' 'a'
'07.03.2019 20:36' 'c'};
[U2,~,ix] = unique(C(:,2),'stable');
myNewCell = splitapply(@(x){x}, C, ix);
myNewCellA = myNewCell{1} % Assign The Others Similarly, If You Want To
producing:
myNewCellA =
3×2 cell array
{'07.03.2019 20:30'} {'a'}
{'07.03.2019 20:33'} {'a'}
{'07.03.2019 20:35'} {'a'}
You can always assign them as ‘myNewCellA’ and similarly for the rest, however that is ineffecient and makes it difficult to iterate through them. I would just refer to them as ‘myNewCell{1}’, ‘myNewCell{2}’, ...
2 Comments
Star Strider
on 8 Mar 2019
My pleasure.
You have a cell array of cell arrays. You didn’t say that, and you didn’t attach your data, so I did the best I could under those circumstances. The images are new as well, so I didn’t know how to create my ‘C’ cell array.
With those problems solved, and if I guess the structure of your cell array correctly, this works:
C = {{'07.03.2019 20:30 a'
'07.03.2019 20:31 b'
'07.03.2019 20:32 c'
'07.03.2019 20:33 a'
'07.03.2019 20:33 b'
'07.03.2019 20:34 c'
'07.03.2019 20:34 b'
'07.03.2019 20:35 c'
'07.03.2019 20:35 a'
'07.03.2019 20:36 c'}
{'07.03.2019 20:30 a'
'07.03.2019 20:31 b'
'07.03.2019 20:32 c'
'07.03.2019 20:33 a'
'07.03.2019 20:33 b'
'07.03.2019 20:34 c'
'07.03.2019 20:34 b'
'07.03.2019 20:35 c'
'07.03.2019 20:35 a'
'07.03.2019 20:36 c'}};
Cs = split(C{1}, ' ');
DT = join([Cs(:,1),Cs(:,2)]);
DTN = [DT Cs(:,3)];
[U2,~,ix] = unique(DTN(:,2),'stable');
myNewCell = splitapply(@(x){x}, DTN, ix);
myNewCellA = myNewCell{1} % Assign The Others Similarly, If You Want To
The results are as in my original Answer, so I will not repeat them here.
I duplicated the original cell array and reformatted them to create ‘C’ as a (2 x 1) cell array of (10 x 1) cell arrays (again guessing its structure) to be as sure as I can be that I am simulating your data correctly.
Note that the split and join functions were introduced in R2016b, so if you have that or a later release, this will work.
You will need to iterate through the cells arrays in your ‘c’ cell array to use my code with each one. A for loop is best for that.
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!