Strrep for multiple rows?
Show older comments
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
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
Chameleon17
on 3 Sep 2015
Edited: per isakson
on 3 Sep 2015
per isakson
on 3 Sep 2015
Edited: per isakson
on 3 Sep 2015
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
on 3 Sep 2015
Edited: Chameleon17
on 3 Sep 2015
per isakson
on 3 Sep 2015
Edited: per isakson
on 3 Sep 2015
- See datenum, Convert date and time to serial date number.
- Convert to serial date numbers and subtract.
dpb
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.
Chameleon17
on 3 Sep 2015
dpb
on 3 Sep 2015
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.
Categories
Find more on Dates and Time 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!