Inconsistent Dates in Time Series
Show older comments
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.
Accepted Answer
More Answers (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 Comments
AU
on 30 May 2019
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
on 30 May 2019
Adam Danz
on 30 May 2019
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
on 30 May 2019
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
on 31 May 2019
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!