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 CenterFile Exchange에서 Workspace 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