How can I solve a problem with the wrong format of date
3 views (last 30 days)
Show older comments
Hello, I am trying to analyse some data based on webinar example. However, if I run below codes,
formatout = 'yyyy-mmm';
fprintf('Estimation period %s to %s with %d assets ...\n', ...
datestr(Date(startindex),formatout), datestr(Date(endindex),formatout), numel(A));
my result shows this:
Estimation period 0551-Mar to 0551-Dec with 7 assets ...
my date format is as:
200102 200103 200104 ... 201303
help me through to solve this problem.
Many thanks. SH
1 Comment
Stephen23
on 7 Aug 2017
Edited: Stephen23
on 7 Aug 2017
Storing the year and month like that is the problem. Store the data in a MATLAB variable designed for storing dates (e.g. serial date number, datetime, etc) and you will not have any problems.
How do you get those numbers (i.e. 200102, 200103, 200104, etc) into the MATLAB workspace in the first place? The correct and best solution would be to fix the problem at the source, and not try to patch it later as you are trying to do. Import/generate those date values in a way that you are not storing them in this awful way in one integer, and instead import them simply as strings, serial date numbers, or datetime values.
Accepted Answer
dpb
on 7 Aug 2017
Edited: dpb
on 8 Aug 2017
If you're going to use numeric variable to store the dates, then use them that way --
>> d=[200102 200103 200104 201303];
>> startindex=1; endindex=length(d); A=rand(5,1); % dummy values
>> fprintf('Estimation period %d to %d with %d assets ...\n', ...
d(startindex),d(endindex),numel(A))
Estimation period 200102 to 201303 with 5 assets ...
>>
As Stephen suggests, however, you'll have a lot more flexibility and success if convert to datenum
>> dn=datetime(fix(d/100),d-fix(d/100)*100,0)
dn =
31-Jan-2001 28-Feb-2001 31-Mar-2001 28-Feb-2013
>>
ADDENDUM
I was rushed for time was going to show in first example a way to get the desired format rather than just the numeric but had to go before got it done...
fmt=['Estimation period %d-%s to %d-%s with %d assets ...\n']; % format string
y=@(d) fix(d/100); m=@(d) d-y(d)*100; % anon function to get year, month
mnstr=month(datetime(2010,[1:12].',1),'shortname'); % the month names
Example useage:
>> fprintf(fmt,y(d(startindex)),mnstr{m(d(startindex))}, ...
y(d(endindex)), mnstr{m(d(endindex))},numel(A))
Estimation period 2001-Feb to 2013-Mar with 5 assets ...
>>
All in all, converting to using datetime from the git-go is quite a lot simpler... :)
>> fmt=['Estimation period %s to %s with %d assets ...\n'];
>> fprintf(fmt,datestr(dn(startindex),'yyyy-mmm'), ...
datestr(dn(endindex),'yyyy-mmm'),numel(A))
Estimation period 2001-Jan to 2013-Feb with 32 assets ...
>>
The "gotcha" there is that afaik datestr is the only way to get the string form output to use for printing and it does not
- honor the format set for the particular datetime object passed, nor
- use the same formatting strings as datetime but the old datenum
Time/date functions are still a "work in progress"...much needs yet to be done to make them really fully functional...
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!