How to extract data from a vector

2 views (last 30 days)
Dubstep Dublin
Dubstep Dublin on 19 Mar 2015
Commented: per isakson on 20 Mar 2015
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

Answers (2)

per isakson
per isakson on 19 Mar 2015
Edited: per isakson on 19 Mar 2015
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 Comments
Dubstep Dublin
Dubstep Dublin on 19 Mar 2015
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 on 20 Mar 2015
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

Sign in to comment.


XueJing Yu
XueJing Yu on 19 Mar 2015
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'

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!