Error using dtstr2dtvecmx. Failed on converting date string to date number.

4 views (last 30 days)
Here is the script:
clear all; close all
clc
%%%%Directories
direc1 = '/Users/Degausseridae/Documents/Research/EggLoggerData/sas_00_createmetadata_Needs/';
direc2 = '/Users/Degausseridae/Documents/Research/EggLoggerData/sas_00_createmetadata_Produces/';
%%%%File to be read in
file2Load = 'BNSTdata.csv';
formatSpec1 = '%s %u %u %s %s %u %u %u %s %u %s %s %s %s %s %s %s %s %f %f %f %s %s %s %u %s';
formatIn = 'mm/dd/yyyy';
N = 26;
%%%%Import file
fileID = fopen([direc1,file2Load]);
C_data = textscan(fileID,formatSpec1,N,'Headerlines',1,'Delimiter',',');
fclose(fileID);
%%%%logger start date
sDate = datevec(C_data{1,13},formatIn);
Here is the error:
Error using dtstr2dtvecmx Failed on converting date string to date number.
Error in datevec (line 117) y = dtstr2dtvecmx(t,icu_dtformat);
Error in sas_00_createmetadata_GT (line 42)
sDate = datevec(C_data{1,13},formatIn);

Answers (1)

Walter Roberson
Walter Roberson on 8 May 2015
At least one of the entries does not have the expected date format 'mm/dd/yyyy'. If you want to debug it then you could use:
C_dates = C_data{1,13};
ndates = length(C_dates);
sDate = -1 * ones(ndates, 6); %using -1 to mark entries that did not convert
numwrong = 0;
maxwarnings = 10; %maximum times to warn so we don't go overboard
for K = 1 : ndates
try
thisvec = datevec(C_dates{K}, formatIn);
sDate(K,:) = thisvec;
catch:
numwrong = numwrong + 1;
if numwrong <= maxwarnings
fprintf(2, 'Warning: line #%d has date that is not mm/dd/yyyy, instead is |%s|\n', K, C_dates{K});
end
end
end
if numwrong > 0
fprintf(2, '\nWarning: there were %d lines with invalid dates\n', numwrong );
end
This will, of course, be much less efficient than doing them all at once.
It would, by the way, be possible to only use the slow method if an error was found, by doing a try/catch on the mass conversion and only going one by one if something gets caught.
I probably wouldn't do it this way myself: I would probably use regexp() to match the valid date patterns. For example, if the strings might not have leading 0's on the month or day, then:
C_dates = C_data{1,13};
datepattern = '\d{1,2}/\d{1,2}/\d{4}';
nondates = cellfun( @isempty, regexp(C_dates, datepattern) );
and the problem entries are
C_dates(nondates)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!