How can I calculate the difference in time (seconds) with datetime?

I have to read a csv file. A column of this file has the next format:
2014/01/16 13:12:12.
Thanks for the help of Mischa Kim I have achieved to read in this format, so this not a problem.
So that my goal is to represent in X axis the time in seconds and Y axis the RSSI related to each second because as you can said me the problem if I represented the TIME_STAMP in the format 13:13:12 there're too many labels If I print all of them.
The First value of TIME_STAMP is 13:12:12 that corresponds to second 1. The Second Value of TIME_STAMP is 13:12:13 that corresponds to second 2. Until the last value...
If you don't understand my question tell me and I'll try to explain better!
Dou you Know a function or a way to calculate the TIME_STAMP in seconds with the aim to represent in x axis the time in seconds with each value.
I attach the csv file.
I would be grateful If you Know how can I do this! Thanks Mischa for your time! Greetings.

 채택된 답변

Mischa Kim
Mischa Kim 2014년 2월 13일
If you are starting with date strings, use datenum to convert dates into number (= days) format. As an example:
d2s = 24*3600; % convert from days to seconds
d1 = d2s*datenum('13:12:12');
d2 = d2s*datenum('13:12:16');
display(d2-d1)
So simply loop through the date strings and subtract the first value.
Of course, if you already have dates in double format you would just do subtraction without conversion. It gets even simpler if you know that you have consistent 1 sec spacing between data points. You then just simply
tspan = 1:1:num_datapoints

댓글 수: 5

In the file that I have attached I have consistent 1 sec spacing between data points but in other files it's not necessary!
I'll try to do this! Thanks!
I don't Know how to pass the string for doing a loop since datenum doesn't accept a string of characters Mischa. I have all the values of the TIME_STAMP in T0Str (because only I need the hh:mm:ss and not the date) but it is a 100x8 char.
OK, try this:
T0Str = ['13:12:12';...
'13:12:13';...
'13:12:14'];
d2s = 24*3600; % convert from days to seconds
tspan = zeros(length(T0Str(:,1)),1);
for ii = 1:length(T0Str(:,1))
tspan(ii) = round(d2s*(datenum(T0Str(ii,:))-datenum(T0Str(1,:))));
end
data = rand(3,1); % just same sample data
plot(tspan,data)
set(gca,'XTick',tspan) % remove ticks between data points
Diego
Diego 2014년 2월 13일
편집: Diego 2014년 2월 14일
It works perfect Mischa! Thanks again!
Diego
Diego 2014년 2월 14일
편집: Diego 2014년 2월 14일
One problem that I have is for example when you read increasely the time it works perfect but if an hour is before than the actual hour, the time (in seconds) starts again. For example in this case:
2014/02/10 12:58:20 2014/02/10 12:58:21 2014/02/10 12:57:35

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jos (10584)
Jos (10584) 2014년 2월 17일
You can also use the dedicated function ETIME (with DATEVEC)
date1 = '2014/01/16 13:12:12'
date2 = '2014/01/16 13:12:13' % one second later
etime(datevec(date2),datevec(date1))
If you have a list of date/times
DT = {'2014/01/16 13:12:13' '2014/01/16 13:12:15' '2014/01/16 13:13:13' '2014/01/17 13:12:13' }
reftime = DT{1} ;
etime(datevec(DT), repmat(datevec(reftime),numel(DT),1))

댓글 수: 5

Hi Jos! Thanks for your help!
I don't Know how to pass the string of the time to DT, since I have proved to passed it but it doesn't accept a string of characters. I don't have to write the values manuallity, the code has to put all the values automatically. It is a 100x8 char.
You can convert a N-by-M char array to a N-by-1 cell array of strings of length M, using CELLSTR
A = ['abcd' ; '1234' ; 'xxxx'] % a 3-by-4 char array
B = cellstr(A) % a 3-by-1 cell array of strings
% B{k} equals A(k,:)
It's nice Jos! Thanks!!!
It worked for me, but I am losing my miliseconds information :(, does anybody knows how to fix that?
In the years since the original answer for this question we introduced the datetime array that you should use instead of converting to datenum values.
dt = datetime('now')
dt = datetime
13-Apr-2021 02:19:12
dt2 = dt + seconds(1.2345)
dt2 = datetime
13-Apr-2021 02:19:13
s = seconds(dt2-dt)
s = 1.2345

댓글을 달려면 로그인하십시오.

sihem chaib
sihem chaib 2014년 2월 14일

1 개 추천

please how to declare increment beam length in matlab progamme

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2014년 2월 13일

댓글:

2021년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by