changing the date form in a cell matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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] 'S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MA'
[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 commentaires
Thomas
le 26 Juil 2012
is MA March April? MJ -May June? and so on... do you have any data that says 'Jan 2009' ...?
Réponse acceptée
Thomas
le 26 Juil 2012
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 commentaires
Plus de réponses (1)
Davide Ferraro
le 26 Juil 2012
Modifié(e) : Davide Ferraro
le 26 Juil 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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!