Inconsistent Dates in Time Series
古いコメントを表示
I have a time series of stock returns with inconsistent dates. Some are in the format dd-mmm-yy and others in the format dd.mm.yyyy. I tried to use the following code to adjust the dates and to get the same date format:
Dates = Dependent_v.Date(:,1);
F = regexp(Dates,'-');
G = cellfun(@isempty,F);
if G = 0
I = datenum(F, 'dd.MM.yyyy');
else
I = datenum(F, 'dd-MMM-yy');
However, I always get the error code: The expression to the left of the equals sign is not a valid target for an assignment.
採用された回答
その他の回答 (1 件)
% Create fake data
F = [cellstr(datestr(now-10:now, 'dd-mmm-yy')); cellstr(datestr(now-10:now, 'dd.mm.yyyy'))];
% Identify dates in 'dd-mmm-yy' format
idx = ~cellfun(@isempty,regexp(F, '\d{1,2}-[A-z]{3}-\d{2}'));
% do conversions
I = zeros(size(F));
I(idx) = datenum(F(idx), 'dd-mmm-yy');
I(~idx) = datenum(F(~idx),'dd.mm.yyyy');
7 件のコメント
AU
2019 年 5 月 30 日
Could you provide a sample of your data that is causing the error? You mentioned that it's exactly like mine but I just want to make triple-sure of that.
Also, what version of matlab are you using (execute ver() to find out). I'm using 2019a.
AU
2019 年 5 月 30 日
Adam Danz
2019 年 5 月 30 日
I just tested my answer in r2017b and there are no problems. How are you importing your data? Are your dates a cell array of strings that look similar to this:
F =
22×1 cell array
{'20-May-19' }
{'21-May-19' }
{'22-May-19' }
{'23-May-19' }
{'24-May-19' }...
AU
2019 年 5 月 30 日
I see what happened. Some of the dates that are in 'dd-mmm-yy' format only have a single digit for the day (example: 1-Dec-11 should be 01-Dec-11) .
The fix is to accept single-digit format within the regular expression:
idx = ~cellfun(@isempty,regexp(F, '\d{1,2}-[A-z]{3}-\d{2}'));
% ^^
I've updated my answer to include single-digit days in that format.
AU
2019 年 5 月 31 日
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!