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!