Separate one column of in a multi-column, (which is itself a cell array) into two columns in the existing cell array
1 回表示 (過去 30 日間)
古いコメントを表示
I have an application that reads data (from someone else's code), in a cell array with 8 columns. Here is a snipped of the first 9 rows:
9×8 cell array
Columns 1 through 6
{'E100M000.txt'} {'6109'} {'23-Sep-2020 14:…'} {[0.04]} {1×2 cell} {[4]}
{'E100M001.txt'} {'6109'} {'23-Sep-2020 14:…'} {[0.05]} {1×2 cell} {[4]}
{'E100M002.txt'} {'4781'} {'23-Sep-2020 15:…'} {[5.08]} {1×2 cell} {[0]}
{'E100M003.txt'} {'4717'} {'23-Sep-2020 15:…'} {[3.36]} {1×2 cell} {[0]}
{'E100M004.txt'} {'4781'} {'23-Sep-2020 15:…'} {[5.04]} {1×2 cell} {[0]}
{'E100M005.txt'} {'6051'} {'23-Sep-2020 16:…'} {[5.08]} {1×2 cell} {[0]}
{'E100M006.txt'} {'4167'} {'23-Sep-2020 15:…'} {[ 3.6]} {1×2 cell} {[0]}
{'E100M007.txt'} {'6022'} {'23-Sep-2020 15:…'} {[0.26]} {1×2 cell} {[0]}
{'E100M008.txt'} {'6002'} {'23-Sep-2020 16:…'} {[2.92]} {1×2 cell} {[0]}
Columns 7 through 8
{[2971.6]} {'Pleasant Valley…'}
{[3523.4]} {'Pleasant Valley…'}
{[ 0]} {'Peckham Propert…'}
{[ 0]} {'251 Creek Road' }
{[ 0]} {'Peckham Propert…'}
{[ 0]} {'23 Plateau Road' }
{[ 0]} {'Peckham Propert…'}
{[ 0]} {'Peckham Propert…'}
Column 5 is itself a cell array, and I would like to replace this with two columns in a new cell array of everything. Column 5 has a letter in the first column of the array, and a number in the second. Simple to split this? reshape? split? ???
Wish I could get it in the right format in the first place, no such luck.
Thanks!
Doug Anderson
1 件のコメント
Arif Hoq
2022 年 3 月 23 日
as you did not attach your data, you can try this
% A is your cell array
col5=A{:,5}
out=horzcat(A(:,1),A(:,2),A(:,3),A(:,4),col5,A(:,6),A(:,7),A(:,8),A(:,9))
採用された回答
Voss
2022 年 3 月 24 日
C = { ...
'E100M000.txt' '6109' '23-Sep-2020 14:…' 0.04 {'A' 1} 4 2971.6 'Pleasant Valley…'; ...
'E100M001.txt' '6109' '23-Sep-2020 14:…' 0.05 {'B' 2} 4 3523.4 'Pleasant Valley…'; ...
'E100M002.txt' '4781' '23-Sep-2020 15:…' 5.08 {'C' 3} 0 0 'Peckham Propert…'}
C_new = [C(:,1:4) vertcat(C{:,5}) C(:,6:end)]
5 件のコメント
Voss
2022 年 3 月 24 日
編集済み: Voss
2022 年 3 月 24 日
Actually, this is not correct:
col5=A{:,5}
That assigns only A{1,5} to col5 (because col5 is only one variable). Observe:
A = { ...
'E100M000.txt' '6109' '23-Sep-2020 14:…' 0.04 {'A' 1} 4 2971.6 'Pleasant Valley…'; ...
'E100M001.txt' '6109' '23-Sep-2020 14:…' 0.05 {'B' 2} 4 3523.4 'Pleasant Valley…'; ...
'E100M002.txt' '4781' '23-Sep-2020 15:…' 5.08 {'C' 3} 0 0 'Peckham Propert…'};
col5 = A{:,5}
% try assigning to 2 output variables:
[col51,col52] = A{:,5}
So you would need to call vertcat() at that point as well:
col5 = vertcat(A{:,5})
Then, the explicit call to horzcat():
out = horzcat(A(:,1),A(:,2),A(:,3),A(:,4),col5,A(:,6),A(:,7),A(:,8))
is the same as using [ ] to do the horizontal concatenation:
out = [A(:,1),A(:,2),A(:,3),A(:,4),col5,A(:,6),A(:,7),A(:,8)] % same
And you don't need to take each individual column separately; you can take the first 4 together and the last 3 together:
out = [A(:,1:4),col5,A(:,6:end)] % same
So the idea is essentially the same, yes; I just did it without the temporary variable col5. (And I don't think anything is transposed or needs to be transposed - you do vertcat on the elements of column 5, then horzcat (maybe using [ ]) that result with all the other columns (in the right order of course).)
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!