How to extract data from a vector
2 次查看(过去 30 天)
显示 更早的评论
I have a 400x1 cell vector in the following format
AB_ww_CD_xx_EF_yy_GH_zz
where ww, xx, yy, zz are numbers
For example-
AB_00_CD_10_EF_80_GH_50
AB_01_CD_20_EF_100_GH_100
AB_02_CD_30_EF_120_GH_200
and so on...
How can I separate each element (AA is one element, xx is another, CD is another and so on) into 8 corresponding columns.
I am giving an example of 1st row below
AB_00_CD_10_EF_80_GH_50 --> Col 1=AB, Col 2=00, Col 3=CD, Col 4=10, Col 5=EF, Col 6=80, Col 7=GH, Col 8=50
Let me know if I have not made the question clear.
Thanks a bunch
0 个评论
回答(2 个)
per isakson
2015-3-19
编辑:per isakson
2015-3-19
This is close
cac = { 'AB_00_CD_10_EF_80_GH_50'
'AB_01_CD_20_EF_100_GH_100'
'AB_02_CD_30_EF_120_GH_200' };
COL = regexp( cac, '_', 'split' );
and
>> whos COL
Name Size Bytes Class Attributes
COL 3x1 3128 cell
and one more step
>> cat( 1, COL{:} )
ans =
'AB' '00' 'CD' '10' 'EF' '80' 'GH' '50'
'AB' '01' 'CD' '20' 'EF' '100' 'GH' '100'
'AB' '02' 'CD' '30' 'EF' '120' 'GH' '200'
>>
2 个评论
per isakson
2015-3-20
Replace
cat( 1, COL{:} )
by
out = repmat( {'00'}, [ size(cac,1), max(cellfun(@length,COL)) ] );
for jj = 1 : size(cac,1)
len = length( COL{jj} );
out( jj, 1:len ) = COL{jj};
end
XueJing Yu
2015-3-19
data={'AB_00_CD_10_EF_80_GH_50'; 'AB_01_CD_20_EF_100_GH_100'; 'AB_02_CD_30_EF_120_GH_200' };
customStrSplit = @(x) strsplit(x,'_');
output = cellfun(customStrSplit,data,'UniformOutput',0);
output{:}
%% this should output:
ans =
'AB' '00' 'CD' '10' 'EF' '80' 'GH' '50'
ans =
'AB' '01' 'CD' '20' 'EF' '100' 'GH' '100'
ans =
'AB' '02' 'CD' '30' 'EF' '120' 'GH' '200'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!