필터 지우기
필터 지우기

Load date/time data and being recognized for further time related subtraction/addtions

조회 수: 1 (최근 30일)
I have a text data file, which includes timestamp like 20150727084239.533 which represent 2015-07-27 08:42:39.533. And I have two questions when dealing with these. 1. how can I use textscan and have it recognized as time? I have the current code shown below, and it only load into the data array as numbers.
Load Vehicle A_0727_L2 Timestamp
filename='Vehicle A_0727_1200.txt';
fid=fopen(filename,'r');
for k=1:2;
tline=fgetl(fid);
end
timestamp1200=zeros(1,7931);
t=1;
while tline~=-1
datestr=regexp(tline,'(?<=STime="20150727).*(?=" ETime)','match');
tempdata2=textscan(datestr{1},'%f');
timestamp1200(:,t)=tempdata2{1};
t=t+1;
tline = fgetl(fid);
end
fclose all;
When I use %{HH:mm:ss.FFF}D to replace %f, system returns the following errors:
Error using textscan
Unable to read the DATETIME data with the format 'HH:mm:ss.FFF'. If the
data is not a time, use %q to get string data.
Error in C_July27_L2_Data (line 39)
tempdata2=textscan(datestr{1},'%{HH:mm:ss.FFF}D');
2. How do I use the load time to perform subtraction like 463 seconds from the loaded data, etc.
thanks!

채택된 답변

Derick Yang
Derick Yang 2017년 8월 8일
Try using the datetime function instead of textscan. In your case, you can use the following format:
tempdata2 = datetime(datestr{1}, 'InputFormat', 'yyyyMMddHHmmss.SSS')
Note that this will give you a datetime object. With a datetime object you can perform date arithmetic. To subtract from your date object:
tempdata2 - seconds(463).
  댓글 수: 3
Derick Yang
Derick Yang 2017년 8월 8일
This error means you need to reference datestr as a matrix rather than a cell array, using parentheses. Instead of datestr{1}, use datestr(1)
Ivy Chen
Ivy Chen 2017년 8월 8일
I replace the {} with parentheses, as following
Load Vehicle A_0727_L2 Timestamp
filename='Vehicle A_0727_1200.txt';
fid=fopen(filename,'r');
for k=1:2;
tline=fgetl(fid);
timedata=zeros(1,7931);
t=1;
while tline~=-1
datestr=regexp(tline,'(?<=STime=").*(?=" ETime)','match');
tempdata2=datetime(datestr(1), 'InputFormat', 'yyyyMMddHHmmss.SSS');
timedata(:,t)=tempdata2(1);
t=t+1;
tline = fgetl(fid);
end
But it now show a different error message as following:
The following error occurred converting from datetime to double:
Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric,
first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or
YEARS functions.
Error in Timestamp_Data (line 13)
timedata(:,t)=tempdata2(1);

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

추가 답변 (1개)

KL
KL 2017년 8월 8일
편집: KL 2017년 8월 8일
You could do it just by reading it as a string at first and then convert that string using datestr,
>> s = '20150727084239.533';
>> date = datestr([s(1:4),'-',s(5:6),'-',s(7:8),' ',s(9:10),':',s(11:12),':',s(13:end)])
date =
'27-Jul-2015 08:42:39'

카테고리

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