MATLAB Answers

salva
0

changing the date form in a cell matrix

Asked by salva
on 26 Jul 2012
Dear all,
i have the following cell matrix
A = {
[1] 'SLO' ' ' '' ''
[1] 'SLO' ' ' '' ''
[1] 'SLO' ' ' '' ''
[1] 'SLO' 'M 2009' 'SLO' 'MA'
[1] 'SLO' 'A 2009' 'SLO' 'MA'
[1] 'SLO' 'M 2009' 'SLO' 'MA'
[1] 'SLO' 'J 2009' 'SLO' 'MA'
[1] 'SLO' 'J 2009' 'SLO' 'MA'
[1] 'SLO' 'A 2009' 'SLO' 'MA'
[1] 'SLO' 'S 2009' 'SLO' 'MA'
[1] 'SLO' 'O 2009' 'SLO' 'MA'
[1] 'SLO' 'N 2009' 'SLO' 'MA'
[1] 'SLO' 'D 2009' 'SLO' 'MA'
[1] 'SLO' 'J 2010' 'SLO' 'MA'
[1] 'SLO' 'F 2010' 'SLO' 'MA'
[1] 'SLO' 'M 2010' 'SLO' 'MA'
[1] 'SLO' 'A 2010' 'SLO' 'MA'
[1] 'SLO' 'M 2010' 'SLO' 'MA'
[1] 'SLO' 'J 2010' 'SLO' 'MA'
[1] 'SLO' 'J 2010' 'SLO' 'MA'
[1] 'SLO' 'A 2010' 'SLO' 'MA'
[1] 'SLO' 'S 2010' 'SLO' 'MA'
[1] 'SLO' 'O 2010' 'SLO' 'MA'
[1] 'SLO' 'N 2010' 'SLO' 'MA'
[1] 'SLO' 'D 2010' 'SLO' 'MA'
[1] 'SLO' 'J 2011' 'SLO' 'MA'
[1] 'SLO' 'F 2011' 'SLO' 'MA'
[1] 'SLO' 'M 2011' 'SLO' 'MA'
[1] 'SLO' 'A 2011' 'SLO' 'MA'
[1] 'SLO' 'M 2011' 'SLO' 'MA'
[1] 'SLO' 'J 2011' 'SLO' 'MA'
[1] 'SLO' 'J 2011' 'SLO' 'MA'
[1] 'SLO' 'A 2011' 'SLO' 'MA'
[2] 'KKK' ' ' ' ' ''
[2] 'SLO' ' ' ' ' ''
[2] 'SLO' ' ' '' ''
[2] 'SL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A'
[3] 'SLO' 'A 2009' 'SLO' 'MA'
[3] 'SLO' 'S 2009' 'SLO' 'MA'
[3] 'SLO' 'O 2009' 'SLO' 'MA'
[3] 'SLO' 'N 2009' 'SLO' 'MA'
[3] 'SLO' 'D 2009' 'SLO' 'MA'
[3] 'SLO' 'J 2010' 'SLO' 'MA'
[3] 'SLO' 'F 2010' 'SLO' 'MA'
[3] 'RRR' 'M 2010' 'SLO' 'MA'
[3] 'RRR' 'A 2010' 'SLO' 'MA'
[3] 'SLO' 'M 2010' 'SLO' 'MA'
[3] 'SLO' 'J 2010' 'SLO' 'MA'
[3] 'SLO' 'J 2010' 'SLO' 'MA'
[3] 'SLO' 'A 2010' 'SLO' 'MA'
[3] 'SLO' 'S 2010' 'SLO' 'MA'
[3] 'SLO' 'O 2010' 'SLO' 'MA'
[3] 'SLO' 'N 2010' 'SLO' 'MA'
[3] 'SLO' 'D 2010' 'SLO' 'MA'
[3] 'SLO' 'J 2011' 'SLO' 'MA'
[3] 'SLO' 'F 2011' 'SLO' 'MA'
[3] 'SLO' 'M 2011' 'SLO' 'MA'
[3] 'SLO' 'A 2011' 'SLO' 'MA'
[3] 'SLO' 'M 2011' 'SLO' 'MA'
[3] 'SLO' 'J 2011' 'SLO' 'MA'
[3] 'SLO' 'J 2011' 'SLO' 'll'
[3] 'SLO' 'A 2011' 'SLO' 'll'
}
My goal is to obtain this one
A = {
[1] 'SLO' ' ' '' ''
[1] 'SLO' ' ' '' ''
[1] 'SLO' ' ' '' ''
[1] 'SLO' '3/2009' 'SLO' 'MA'
[1] 'SLO' '4/2009' 'SLO' 'MA'
[1] 'SLO' '5/2009' 'SLO' 'MA'
[1] 'SLO' '6/2009' 'SLO' 'MA'
[1] 'SLO' '7/2009' 'SLO' 'MA'
[1] 'SLO' '8/2009' 'SLO' 'MA'
[1] 'SLO' '9/2009' 'SLO' 'MA'
[1] 'SLO' '10/2009' 'SLO' 'MA'
[1] 'SLO' '11/2009' 'SLO' 'MA'
[1] 'SLO' '12/2009' 'SLO' 'MA'
[1] 'SLO' '1/20010' 'SLO' 'MA'
[1] 'SLO' '2/2010' 'SLO' 'MA'
[1] 'SLO' '3/2010' 'SLO' 'MA'
[1] 'SLO' '4/2010' 'SLO' 'MA'
[1] 'SLO' '5/2010' 'SLO' 'MA'
[1] 'SLO' '6/2010' 'SLO' 'MA'
[1] 'SLO' '7/2010' 'SLO' 'MA'
[1] 'SLO' '8/2010' 'SLO' 'MA'
[1] 'SLO' '9/2010' 'SLO' 'MA'
[1] 'SLO' '10/2010' 'SLO' 'MA'
[1] 'SLO' '11/2010' 'SLO' 'MA'
[1] 'SLO' '12/2010' 'SLO' 'MA'
[1] 'SLO' '1/2011' 'SLO' 'MA'
[1] 'SLO' '2/2011' 'SLO' 'MA'
[1] 'SLO' '3/2011' 'SLO' 'MA'
[1] 'SLO' '4/2011' 'SLO' 'MA'
[1] 'SLO' '5/2011' 'SLO' 'MA'
[1] 'SLO' '6/2011' 'SLO' 'MA'
[1] 'SLO' '7/2011' 'SLO' 'MA'
[1] 'SLO' '8/2011' 'SLO' 'MA'
}
and so forth for the rest of the individuals
where as you can see in the third column I have replace for example the month 'M 2009' with '3/2009'.
In my case i have 30000 individuals
Is there any code for achieving this?
Thanks

  3 Comments

Thomas
on 26 Jul 2012
how do you intend to differentiate between J 2009 as June 2009 and July 2009 or January 2009 i.e.1/2009 6/2009 and 7/2009 ??
perhaps by going one step back: that is this is my very original matrix
A={
1 'SLO' ' ' '' ''
1 'SLO' ' ' '' ''
1 'SLO' ' ' '' ''
1 'SLO' 'MA 2009' 'SLO' 'MA'
1 'SLO' 'MJ 2009' 'SLO' 'MA'
1 'SLO' 'JA 2009' 'SLO' 'MA'
1 'SLO' 'SO 2009' 'SLO' 'MA'
1 'SLO' 'ND 2009' 'SLO' 'MA'
1 'SLO' 'JF 2010' 'SLO' 'MA'
1 'SLO' 'MA 2010' 'SLO' 'MA'
1 'SLO' 'MJ 2010' 'SLO' 'MA'
1 'SLO' 'JA 2010' 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'SLO' 'MA'
3 'SLO' 'MJ 2011' 'SLO' 'MA'
3 'SLO' 'JA 2011' 'SLO' 'll'
}
see also my previous question
Thomas
on 26 Jul 2012
is MA March April? MJ -May June? and so on... do you have any data that says 'Jan 2009' ...?

Sign in to comment.

Tags

2 Answers

Answer by Thomas
on 26 Jul 2012
 Accepted Answer

Using the first A cell MAtrix shown in the question:
for ii=1:length(A)
A(ii,3)=strrep(A(ii,3),'S','SEP');
A(ii,3)=strrep(A(ii,3),'O','OCT');
A(ii,3)=strrep(A(ii,3),'N','NOV');
A(ii,3)=strrep(A(ii,3),'D','DEC');
A(ii,3)=strrep(A(ii,3),'F','FEB');
if strcmp(A(ii,3),A(1,3))
ii=ii+1;
else
if (cell2mat((strfind(A(ii,3),'J')))==1)
if (cell2mat(strfind(A(ii-1,3),'M')))==1
A(ii,3)=strrep(A(ii,3),'J','JUN');
elseif (cell2mat(strfind(A(ii-1,3),'J')))==1
A(ii,3)=strrep(A(ii,3),'J','JUL');
else
A(ii,3)=strrep(A(ii,3),'J','JAN');
end
end
if (cell2mat((strfind(A(ii,3),'M')))==1)
if (cell2mat(strfind(A(ii-1,3),'F')))==1
A(ii,3)=strrep(A(ii,3),'M','MAR');
else
A(ii,3)=strrep(A(ii,3),'M','MAY');
end
end
if (cell2mat((strfind(A(ii,3),'A')))==1)
if (cell2mat(strfind(A(ii-1,3),'J')))==1
A(ii,3)=strrep(A(ii,3),'A','AUG');
else
A(ii,3)=strrep(A(ii,3),'A','APR');
end
end
end
end
A(:,3) % output data
% convert to datenum
for ii=1:length(A)
if strcmp(A(ii,3),A(1,3))
ii=ii+1;
else
date_vec(ii)=datenum(A(ii,3),'mmm yyyy');
end
end

  0 Comments

Sign in to comment.


Answer by Davide Ferraro on 26 Jul 2012
Edited by Davide Ferraro on 26 Jul 2012

The date has an ambiguous format cause "M 2009" can be march or may so a classic approac with DATENUM may not be appropriate. What I see is that you also have discontinuities.
A possible approach is to use a loop and at each step you check what letter you had the step before. I.e. if you find a M you check the previous line and if this an F you assume that M is March else it's May. A possible idea:
if A{i,3}(1) == 'M'
if A{i-1}(1) == 'F'
A{1,3} = ['3/' A{1,3}(2:end)];
else
A{1,3} = ['5/' A{1,3}(2:end)];
end
end
Surely you need to take care of all possible options and create a more complex IF-ELSE structure.

  2 Comments

It seems to me too complicated.
Any other suggestion?
thanks

Sign in to comment.