找到你要的答案

Q:How to represent fractional part of second in matlab time-series object?

Q:如何在MATLAB中的时间序列对象代表二小数部分?

I want to represent time in time-series object in matlab in following format. dd-mmm-yyyy HH:MM:SS.FFF. I have converted my date into desired date string format but when I create time-series object then fraction value of the second is rounded off to the nearest integer, as a result I get redundant time value.

What I should do so get time series to the precision of millisecond?

I am using:

dateStr = datestr(dateNum, 'dd-ddd-yyyy HH:MM:SS.FFF')

to convert it into date string.

01-Mar-2008 18:28:51.810

But, when I use this in time-series, fractional part of the second is rounded off as shown below.

tsobject = timeseries( x, dateStr, 'name', 'X-ord')
01-Mar-2008 18:28:52

Actual date string is

01-Mar-2008 18:28:51.810
01-Mar-2008 18:29:05.646
01-Mar-2008 18:29:07.376
01-Mar-2008 18:29:09.105
01-Mar-2008 18:29:10.835

Using datenum instead of datestr

tsobject = timeseries( x, dateNum, 'name', 'X-ord')
tsobject.TimeInfo
tsdata.timemetadata
Package: tsdata

  Non-Uniform Time:
    Length       90419

  Time Range:
    Start        7.334688e+05 seconds
    End          7.336596e+05 seconds

  Common Properties:
     Units: 'seconds'
     Format: ''
     StartDate: ''

I want to represent time in time-series object in matlab in following format. dd-mmm-yyyy HH:MM:SS.FFF. I have converted my date into desired date string format but when I create time-series object then fraction value of the second is rounded off to the nearest integer, as a result I get redundant time value.

我应该做什么,使时间序列毫秒的精度?

我正在使用:

dateStr = datestr(dateNum, 'dd-ddd-yyyy HH:MM:SS.FFF')

将其转换为日期字符串。

01-Mar-2008 18:28:51.810

但是,当我在时间序列中使用这个时,第二部分的小数部分被舍入如下所示。

tsobject = timeseries( x, dateStr, 'name', 'X-ord')
01-Mar-2008 18:28:52

实际日期字符串

01-Mar-2008 18:28:51.810
01-Mar-2008 18:29:05.646
01-Mar-2008 18:29:07.376
01-Mar-2008 18:29:09.105
01-Mar-2008 18:29:10.835

使用datenum代替中

tsobject = timeseries( x, dateNum, 'name', 'X-ord')
tsobject.TimeInfo
tsdata.timemetadata
Package: tsdata

  Non-Uniform Time:
    Length       90419

  Time Range:
    Start        7.334688e+05 seconds
    End          7.336596e+05 seconds

  Common Properties:
     Units: 'seconds'
     Format: ''
     StartDate: ''
answer1: 回答1:

This should do the job (especially in R2014a and earlier):

str = '12-Apr-2015 11:22:23.123';
num = datenum(str,'dd-mmm-yyyy HH:MM:SS.FFF');  % string -> serial
str = datestr(num,'dd-mmm-yyyy HH:MM:SS.FFF');  % serial -> string

Starting from R2014b there is a better implementation for time/date-data. Therefore you can use a datetime-object.

str = '12-Apr-2015 11:22:23.123';
obj = datetime(str,'InputFormat','dd-MMM-yyyy hh:mm:ss.SSS');
obj.Format = 'dd-MMM-yyyy hh:mm:ss.SSS';

With timeseries-objects, you have to input the serialized date (in this case variable num), because there is no valid input format for strings with milliseconds for timeseries-objects.

% your strings
dateStr = ['01-Mar-2008 18:28:51.810';
           '01-Mar-2008 18:29:05.646';
           '01-Mar-2008 18:29:07.376';
           '01-Mar-2008 18:29:09.105';
           '01-Mar-2008 18:29:10.835'];

% string -> serial
dateNum = datenum(dateStr,'dd-mmm-yyyy HH:MM:SS.FFF');  % string -> serial

% serial -> string (uncomment to see if the serialized version is ok
%dateStr2 = datestr(dateNum,'dd-mmm-yyyy HH:MM:SS.FFF');  

% generate sample data
x = ones(length(dateNum),1);

% create timeseries-object
tsobject = timeseries(x, dateNum, 'name', 'X-ord');

% display time in timeseries-object with defined format
datestr(tsobject.Time(:),'dd-mmm-yyyy HH:MM:SS.FFF')

This returns:

ans =
01-Mar-2008 18:28:51.810
01-Mar-2008 18:29:05.646
01-Mar-2008 18:29:07.376
01-Mar-2008 18:29:09.105
01-Mar-2008 18:29:10.835

这要做的工作(尤其是在r2014a早):

str = '12-Apr-2015 11:22:23.123';
num = datenum(str,'dd-mmm-yyyy HH:MM:SS.FFF');  % string -> serial
str = datestr(num,'dd-mmm-yyyy HH:MM:SS.FFF');  % serial -> string

从r2014b有更好的执行时间/日期数据。因此,你可以使用一个DateTime对象。

str = '12-Apr-2015 11:22:23.123';
obj = datetime(str,'InputFormat','dd-MMM-yyyy hh:mm:ss.SSS');
obj.Format = 'dd-MMM-yyyy hh:mm:ss.SSS';

时间序列对象,输入你要序列化的日期(在这种情况下,变量num),因为这是对时间序列对象毫秒没有有效的输入格式字符串。

% your strings
dateStr = ['01-Mar-2008 18:28:51.810';
           '01-Mar-2008 18:29:05.646';
           '01-Mar-2008 18:29:07.376';
           '01-Mar-2008 18:29:09.105';
           '01-Mar-2008 18:29:10.835'];

% string -> serial
dateNum = datenum(dateStr,'dd-mmm-yyyy HH:MM:SS.FFF');  % string -> serial

% serial -> string (uncomment to see if the serialized version is ok
%dateStr2 = datestr(dateNum,'dd-mmm-yyyy HH:MM:SS.FFF');  

% generate sample data
x = ones(length(dateNum),1);

% create timeseries-object
tsobject = timeseries(x, dateNum, 'name', 'X-ord');

% display time in timeseries-object with defined format
datestr(tsobject.Time(:),'dd-mmm-yyyy HH:MM:SS.FFF')

这将返回:

ans =
01-Mar-2008 18:28:51.810
01-Mar-2008 18:29:05.646
01-Mar-2008 18:29:07.376
01-Mar-2008 18:29:09.105
01-Mar-2008 18:29:10.835
matlab  time-series