Reputation: 43
I am trying to plot some data using the datetick function by first using datenum to parse some data. Here is some of the data I am trying to read:
0, 6/23/2015 12:21:590 PM, 93.161, 95.911,94.515,95.917, -5511.105,94.324,-1415.849,2.376,2.479
1, 6/23/2015 12:22:02 PM, 97.514, 96.068,94.727,96.138,-12500.000,94.540,-8094.912,2.386,2.479
I try the following code:
fileID = fopen('070915.csv');
C = textscan(fileID,'%f %s %f %f %f %f %f %f %f %f','Delimiter',',','headerLines', 9);
fclose(fileID);
formatIn = 'mm/dd/yyyy HH:MM:SS.FFF PM';
m = datenum(C{2},formatIn)
figure('Position',[0,0,1000,1000])
h1 = plot(m,C{5},'b');
datetick (formatIn);
and I get the following error:
error: datevec: DATE not parsed correctly with given format
error: called from
datevec at line 147 column 11
datenum at line 104 column 40
plotwithdate at line 18 column 3
I can get datenum to partially work by seperating the month/day/year from the time and the AM/PM by using the follwing:
fileID = fopen('070915.csv');
C = textscan(fileID,'%f %s %s %s %f %f %f %f %f %f %f %f','Delimiter',', ','headerLines', 9);
fclose(fileID);
m = datenum(C{2},'mm/dd/yyyy')
n = datenum(C{3},'HH:MM:SS.FFF')
o = datenum(C{4},'AM')
which gives me a 7xxxxx number for each row for m, n, and o. So the syntax looks ok until I try them all together.
Upvotes: 3
Views: 1871
Reputation: 1610
Two issues with the code.
First, your format string doesn't match the number of columns. You need an extra %f
in there. As it stands, your code produces:
C{2}
ans =
{
[1,1] = 6/23/2015 12:21:590 PM
[2,1] = 1
[3,1] = 2.479
}
So, first fix is to add an extra %f
to the textread format string:
C = textscan(fileID,'%f %s %f %f %f %f %f %f %f %f %f','Delimiter',',','headerLines', 9);
Which produces:
C{2}
ans =
{
[1,1] = 6/23/2015 12:21:590 PM
[2,1] = 6/23/2015 12:22:02 PM
}
Now, the next thing you'll notice is that the 12:21:590
is erroneous. I'll assume that was just a typo in your example. But setting it to 12:21:59
still leaves problems.
You've specified a format with fractional seconds, but your data doesn't include fractional seconds. Octave will throw an error because of that. If you specify .FFF
, your data needs to include it even if it's just as a .0
.
Finally, Octave will still throw an error, even if you add the fractional seconds to your data. This is not your fault, however. It appears that there is a bug in Octave whenever both the .FFF
and PM
notation are used together. E.g.:
>> datevec('06/01/2015 3:07:12 PM','mm/dd/yyyy HH:MM:SS PM')
ans =
2015 6 1 15 7 12
>> datevec('06/01/2015 3:07:12.123','mm/dd/yyyy HH:MM:SS.FFF')
ans =
2015.0000 6.0000 1.0000 3.0000 7.0000 12.1230
>> datevec('06/01/2015 3:07:12.123 PM','mm/dd/yyyy HH:MM:SS.FFF PM')
error: datevec: DATE not parsed correctly with given format
error: called from
datevec at line 147 column 11
This issue is covered in a bug report to Octave. There is currently a corrected version of the function datevec.m file attached to that bug report. According to the information on that page the corrected function will be part of Octave 4.0.1 when released. In the meantime you could download and use the corrected version to correctly process your data.
Alternatively, if you have control over your source data format, you can either drop the .FFF
designator (looks like your data may not need it anyway), or drop the PM
, and it would work.
Update: the above mentioned bug was patched and closed. Octave versions later than 4.0.1 should no longer have this problem.
Upvotes: 2