extract and split data from cell into multiple cells.

23 ビュー (過去 30 日間)
rnd rnd
rnd rnd 2019 年 3 月 8 日
コメント済み: Star Strider 2019 年 3 月 8 日
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):
1112.png
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.

採用された回答

Star Strider
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 件のコメント
rnd rnd
rnd rnd 2019 年 3 月 8 日
Thank you for the solution,
The problem is I am getting an error:
Error using cell/unique (line 85)
Cell array input must be a cell array of character vectors.
Error in timeDifCalc (line 8)
[U2,~,ix] = unique(c(:,2),'stable');
I think the problem is I have a cell , not a cell array (see photo)
and I have 3 columns , date, hour, a string.
1112.png
And in the end I also want to have a cell not a cell array, if that is possible.
Star Strider
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 ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by