How to extract data from a vector

2 ビュー (過去 30 日間)
Dubstep Dublin
Dubstep Dublin 2015 年 3 月 19 日
コメント済み: per isakson 2015 年 3 月 20 日
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

回答 (2 件)

per isakson
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 件のコメント
Dubstep Dublin
Dubstep Dublin 2015 年 3 月 19 日
Thanks a lot. What changes do I need to make if my cell is not uniform..for example
AB_00_CD_10
AB_01_CD_20
AB_02_CD_30_EF_120_GH_200
AB_03_CD_40_EF_140_GH_300
I would to populate the nonuniform columns with 00
per isakson
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
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'

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by