extract and split data from cell into multiple cells.
23 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2019-3-8
编辑:Star Strider
2019-3-8
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 个评论
Star Strider
2019-3-8
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!