Hi all
I need to analyze a few measurements. The data files log the instrument's response vs. time, where time is the local date and time. I imported these data as timeseries which gives me the time in the date-time format.
Would you know a simple way to convert the time in the timeseries from the absolute format ( date and time) into a relative time (or duration) in seconds? For each data set, I'd like to define a reference time for this conversion. This reference time is different from the "start date" of the time series.
I don't really want to re-sample or modify the data. Just change the format in which the time is given (and for this specify a specific date-time that will denote 0 in the relative time axis.
Can this be done within the timesiries framework, Or do I need to write a scirpt to do this?
thanks thorsten
|
|
0
|
|
|
|
Reply
|
Thorsten
|
11/4/2010 3:43:03 PM |
|
Dear Thorsten,
Please show us the exact format.
Most likely this can be done easily with DATENUM. Take a look in the code of ETIME.
Kind regards, Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
11/4/2010 5:09:03 PM
|
|
Dear Jan
thanks for the etime hint. I came up with quite some lines of code. Could this be done shorter with a few timeseries commands?
For the following code, I import a data file. In that file the first two columns are date and time of each data point. Col 9 contains the data.
% convert date and time string into serial time
x_num = datenum([char(data{1}), char(data{2})], 'dd.mm.yyyyHH:MM:SS');
%convert time to string in a format timeseries can handle
x_str = cell(length(x_num)-1,1);
for i= 1:length(x_num)
x_str{i,1} = datestr(x_num(i), 'dd-mmm-yyyy HH:MM:SS');
end
y = data{:,9}; % CLD data
clear data fid i ans
%%
% create time series
% create timeseries of whole data range
ts = timeseries(y,x_str);
% add events: start time and end time of run of interest
ts = addevent(ts,{'START' 'STOP'},{'14-Jul-2008 15:14:04' ' 14-Jul-2008 15:51:07'});
% create new timeseries of single experiment
ts1 = gettsbetweenevents(ts,'START','STOP');
%%
% So far so good. Can the following be done directly within ts1, i.e. without the need to export data and time and create a new ts?
% convert time into relative time (0 is start of run)
% export time of ts1
tmp = getabstime(ts1);
% convert this to relative time
x_rel = zeros(length(tmp)-1,1);
for i= 1:length(tmp)
x_rel(i,1) = etime(datevec(datenum(tmp(i,1),'dd-mmm-yyyy HH:MM:SS')),...
datevec(datenum(tmp(1),'dd-mmm-yyyy HH:MM:SS')));
end
% make new timeseries with relative times
tmp2 = ts1.Data;
ts2 = timeseries(tmp2,x_rel);
clear i tmp* ts1
plot(ts2)
|
|
0
|
|
|
|
Reply
|
Thorsten
|
11/5/2010 9:14:04 AM
|
|
Dear Thorsten,
It would be very helpful, if you show us the value of your "data" cell.
> x_str = cell(length(x_num)-1,1);
> for i= 1:length(x_num)
> x_str{i,1} = datestr(x_num(i), 'dd-mmm-yyyy HH:MM:SS');
> end
Much faster:
x_str = datestr(x_num, 0);
> clear data fid i ans
Are you sure that this is really useful?
> x_rel = zeros(length(tmp)-1,1);
> for i= 1:length(tmp)
> x_rel(i,1) = etime(datevec(datenum(tmp(i,1),'dd-mmm-yyyy HH:MM:SS')),...
> datevec(datenum(tmp(1),'dd-mmm-yyyy HH:MM:SS')));
> end
Better convert all dates at once, such that you can call ETIME with a vector and a scalar.
But you can convert all dates to the DATENUM format also and then subtract the DOUBLE values. Multiply the results by 86400 to get the number of seconds.
Kind regards, Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
11/5/2010 5:14:04 PM
|
|
|
3 Replies
580 Views
(page loaded in 0.028 seconds)
|