error using etime (datevec)

5 次查看(过去 30 天)
C.G.
C.G. 2022-2-16
I have a csv file with times for experiment start and end. I'm trying to use etime to work out the time difference but im getting the following error. Can anybody help?
fid = fopen('time.csv');
out = textscan(fid,'%s %f %f %s','delimiter',',','headerlines',1);
fclose(fid);
com = out{1};
com2 = out{2};
com3 = out{3};
start_t = out{4};
dt2 = start_t(1);
gap_sec = etime(datevec(dt2),datevec(dt1));
time = gap_sec + time;
Error using datevec (line 225)
Failed to lookup month of year.

回答(2 个)

Star Strider
Star Strider 2022-2-16
If R2014b or later is available, try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/896495/time.csv')
T1 = 2×3 table
Var1 Var2 Var3 _____________________________________________________________ ____________ ______________ {'START,0.000000000000000E0,1.644373511592961E9,"2022-02-08'} 20:25:11.593 {'UTC-06:00"'} {'PAUSE,1.800001135500000E4,1.644391511601876E9,"2022-02-09'} 01:25:11.602 {'UTC-06:00"'}
C = regexp(T1{:,1}, '\d*\.\d*E\d', 'match');
n = cell2mat(cellfun(@(x)str2double(x), C, 'Unif',0));
Dates = datetime(n(:,2), 'ConvertFrom','Posixtime');
ElapsedTime = diff(Dates);
ElapsedTime.Format = 'hh:mm:ss.SSS'
ElapsedTime = duration
05:00:00.008
The initial regexp call returns the POSIX numbers as strings. The ‘n’ and ‘Dates’ assignments extract the numerical data from the strings and convert them to datetime arrays. Using the diff funciton (to get the elapsed time) automatically returns the result as a duration array (here a scalar) and the last line sets the display 'Format' to show the milliseconds as well.
.

Seth Furman
Seth Furman 2022-2-17
编辑:Seth Furman 2022-2-17
We can import the table using import options to specify the delimiter and datetime format.
See also the following documentation pages.
fname = "https://www.mathworks.com/matlabcentral/answers/uploaded_files/896495/time.csv";
opts = detectImportOptions(fname,"Delimiter",",","TextType","string","VariableNamingRule","preserve")
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {','} Whitespace: '\b\t ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'event', 'experiment time', 'system time' ... and 1 more} VariableTypes: {'string', 'double', 'double' ... and 1 more} SelectedVariableNames: {'event', 'experiment time', 'system time' ... and 1 more} VariableOptions: Show all 4 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'preserve' Location Properties: DataLines: [2 Inf] VariableNamesLine: 1 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
opts.VariableTypes{4} = 'datetime';
inputFormat = "uuuu-MM-dd HH:mm:ss.SSS ZZZZ";
opts = setvaropts(opts,4,"InputFormat",inputFormat,"TimeZone","UTC");
t = readtable(fname,opts)
t = 2×4 table
event experiment time system time system time text _______ _______________ ___________ ___________________________ "START" 0 1.6444e+09 2022-02-09 02:25:11.593 UTC "PAUSE" 18000 1.6444e+09 2022-02-09 07:25:11.602 UTC
diff(t{:,4})
ans = duration
05:00:00

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by