Create a function to find elapsed time with string of date and times through samples?
Show older comments
Hi all.
I have a string array of dates in the form: '2016/10/05 1:06:58 PM' or '2016/10/06 3:01:46 AM'.
I am trying to write a function to find the time elapsed between each event be individual sample where each column (of 27) is an individual sample.
I have this:
function [timeBetween] = GetTimeBetweenFills(dateString)
timeBetween = zeros(length(dateString),1);
timeBetween(1) = 0;
for i=2:length(dateString)-1
time1 = datevec(dateString(i),'yyyy/mm/dd HH:MM:SS AM');
time2 = datevec(dateString(i+1),'yyyy/mm/dd HH:MM:SS AM');
timeBetween(i) = etime(time2,time1);
end
end
And I realized that AM and PM changes through the sample, which I don't know how to handle, and I'm having issues running it through this loop to go through each sample:
S = size(SiteDates);
strArray = table2array(SiteDates);
timeBetween = zeros(S(1),S(2));
for j = 1:27
SiDa = strArray(:,j);
timeBetween(:,j) = GetTimeBetweenFills(SiDa);
end
SiteDates is a table and I tried to convert it to an array and have a string array now but MATLAB doesn't like what I've done.
I get the error:
Error using dtstr2dtvecmx
Failed to convert from text to date number.
Error in datevec (line 122)
y = dtstr2dtvecmx(t,icu_dtformat);
Error in GetTimeBetweenFills (line 9)
time1 = datevec(dateString(i),'yyyy/mm/dd HH:MM:SS AM');
Any help would be wonderful. This is my first time working with time in MATLAB.
Thanks!
Accepted Answer
More Answers (0)
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!