breaking a column of strings in two different columns

조회 수: 2(최근 30일)
Dear all,
Inside the workspace I have the following column in cell format
Column 1
AR021108'
'AW301108'
'EW281208'
'RT250109'
'RY220209'
I was trying to break this column in two other where the first column will contain the first two elements of each of these string variables and the second one will contain the dates as follows
Column1 column 2
AR 02/11/08
AW 30/11/08
EW 28/12/08
RT 25/01/09
RY 22/02/09
Any suggestions?
  댓글 수: 1
antonet
antonet 2012년 6월 19일
I used strtok(jji,'0 1 2 3 6 7 8 9'); wherre jji is the name of the column and i managed to create the first colum. The second column has the form
021108
301108
281208
250109
220209
but my goal is to have
02/11/08
30/11/08
28/12/08
and so forh. Thank you

댓글을 달려면 로그인하십시오.

채택된 답변

Kevin Holst
Kevin Holst 2012년 6월 19일
It appears that you can't really do matrix operations on cell arrays like:
mycell{:,2} = mycell{:,1}(3:end);
So here's what I've got for you. It's probably not the most elegant solution, but it works...
for i = 1:length(mycell)
mycell{i,2} = [mycell{i,1}(3:4) '/' mycell{i,1}(5:6) '/' mycell{i,1}(7:8)];
mycell{i,1} = mycell{i,1}(1:2);
end
  댓글 수: 1
antonet
antonet 2012년 6월 19일
you are the best! It works!

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Thomas
Thomas 2012년 6월 19일
a={'AR021108'
'AW301108'
'EW281208'
'RT250109'
'RY220209'};
ii=cellfun(@(x) x(1:2),a, 'UniformOutput', false);
jj=cellfun(@(x) [x(3:4) '/' x(5:6) '/' x(7:8)], a, 'UniformOutput', false);
out=[ii jj]
another way without loops..
  댓글 수: 1
antonet
antonet 2012년 6월 20일
thank you very much Thomas!

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by