Strrep for multiple rows?

I have two vectors of dates, some of the zero dates are 30-Nov-0000 and some are 00-Jan-0000.
I want to convert the 30-Nov-0000 to 00-Jan-0000, but string replace will not work on multiple rows it says.
Is there another way to do this?
As an add on, sorry I didn't want to ask a completely new question, is there any way to find the actual number of days between two dates? I found the daysact command and it is exactly what I want, but you need the financial tool box apparently. Is there any longer way around this?

Answers (1)

dpb
dpb on 2 Sep 2015
Case where cell strings win!!!! :)
Given a variable character array of ds for date(string),
>> ds=['30-Nov-0000';'30-Nov-0000';'00-Jan-0000']; % sample short vector of date strings
>> ds=char(strrep(cellstr(ds),'30-Nov-0000','00-Jan-0000'))
ds =
00-Jan-0000
00-Jan-0000
00-Jan-0000
>>
Works via converting to cellstring array then back to character--or, of course, it may be just as well to leave as cellstring depending on end result wanted.
As for the second, you can simply subtract date numbers; days are represented by whole numbers so the difference in days is the whole number of the result (with, perhaps a +1 if want inclusive or not).
There's a new date-time class in last releases; it may have some additional builtin functionality as well that's worth pursuing. Otherwise, as above convert to date numbers and just do arithmetic. You will need, I think a non-arbitrary '0000' year, though, particularly if you're concerned about leap years, etc., in the computations being handled correctly and automagically.

7 Comments

Hi! Thanks very much for that answer, it helped!
Except, why when:
DateP1 = char(strrep(cellstr(DateP),'30-Nov-0000','00-000-0000'))
DateP2 = datevec(DateP1)
DateP1 will replace '30-Nov-0000' with the '00-000-0000'. When I try to switch back to datevec it reverts all the '00-000-0000' back to '30-Nov-0000'?
That's weird.
>> datevec( '00-000-0000', 'dd-mmm-yyyy' )
ans =
2 11 30 0 0 0
>> datevec( '00-00-0000', 'dd-mm-yyyy' )
ans =
2 11 30 0 0 0
>> datevec( '17-07-2015', 'dd-mm-yyyy' )
ans =
2015 7 17 0 0 0
>> datevec( '01-01-0000', 'dd-mm-yyyy' )
ans =
0 1 1 0 0 0
>> version
ans =
8.1.0.604 (R2013a)
'00-000-0000' is not a legal string value for a date. Three character month should be letters. Furthermore, there is no day 00 nor a month 00
IMO: Matlab should have thrown an error or at least a warning.
Chameleon17
Chameleon17 on 3 Sep 2015
Edited: Chameleon17 on 3 Sep 2015
If I have two lists of dates and I want to compare the second set of dates to the first, find out how many days before or after the first date the second one occurred, is this possible? I know how to do it if I just want to subtract the dates but if I do this and they treat the day and month columns separately sometimes it doesn't work properly. Is it possible for matlab to just say this many days +/- the key date?
per isakson
per isakson on 3 Sep 2015
Edited: per isakson on 3 Sep 2015
Like Per, I fail to see why you're using zeros for date fields at all; they're not valid other than as additions/subtractions from some real date.
I think the reason datevec doesn't error is that the date number functions are designed to let one build date vectors by "ordinary" math vector operations so that they internally wrap fields from one lower level of granularity to the next higher accounting for days in month, leap years, etc., etc. Consequently, they do consider a zero field but wrap it to the next; what confused datevec thoroughly here appears to be that there's multiple zeros and a bad format so a warning on that case could be expected I agree. But, adding even more complexity would further slow down already complex functions.
Thank you for that.
I was using zeros because there are no values for those fields, no dates, but I cannot shorten the date columns as they are in a specific order to be paired with another column of dates. It appears that one of my columns does not have a year attached to it either, I'm trying to go back to my original code where I processed this and I can't see where I lost the year information. Not that it is important as everything is in the same year. Is there a way to add in a year at this stage? I'm reading the convert date and time info you posted which is helpful, but because I'm missing the year for one column the date numbers for that column are three numbers long while in the other column they are six.
It would be pretty simple one would think to add the year; precisely where and how would be dependent upon just what your starting point really is; we've never seen the actual data format in toto, only the minimal subset that describes the specific syntax issues you've run into.
As for how to handle the missing values cleanly, that also would be dependent upon information we don't have of again, what's the initial start point and where are you really heading as end result? I would likely start with a specific (but real) date that is not in the dataset so that all the conversion functions operate as expected but you can then in the resulting date number column do a global substitution on that value to NaN or other missing-value indicator.
In looking the new date-time class doesn't seem to have a builtin missing value, either, so the above or a variation of the above is probably the best ploy. NaN works well with plotting as plot and friends simply do not display those points and introduce breaks in line plots so one can also see there are breaks instead of presuming data between all points if remove the observations entirely. There are also the nanXXX class of functions that do things mean, etc., treating the NaN as missing and not using those entries. You can do the above manually, of course, via the isnan and/or isfinite functions but the syntactic sugar is convenient for those operations with the specific functions available.

Sign in to comment.

Categories

Asked:

on 2 Sep 2015

Commented:

dpb
on 3 Sep 2015

Community Treasure Hunt

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

Start Hunting!