Strrep for multiple rows?

9 ビュー (過去 30 日間)
Chameleon17
Chameleon17 2015 年 9 月 2 日
コメント済み: dpb 2015 年 9 月 3 日
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?

回答 (1 件)

dpb
dpb 2015 年 9 月 2 日
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 件のコメント
Chameleon17
Chameleon17 2015 年 9 月 3 日
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.
dpb
dpb 2015 年 9 月 3 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by