Strrep for multiple rows?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposte (1)
dpb
il 2 Set 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 Commenti
dpb
il 3 Set 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.
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!