Need to import CSV file with date and time
조회 수: 20 (최근 30일)
이전 댓글 표시
Dear all, I am a newbie to MATLAB frequently these days. I need to read time from the attached csv file and convert it into minutes. I had looked at many other questions similar to me but seems i am stuck!!
Scan, Time, Temp(C), Alarm 101 1, 01/09/2014 15:13:47:338, 25.408, 0 2, 01/09/2014 15:13:48:322, 25.373, 0 3, 01/09/2014 15:13:49:322, 25.372, 0 4, 01/09/2014 15:13:50:322, 25.373, 0 5, 01/09/2014 15:13:51:322, 25.368, 0
The time column need to be converted to mins and initial time instant need to be referenced. So i need to know the temperature change in mins. Thanks everybody!!
댓글 수: 4
Peter Perkins
2019년 2월 20일
Adding a new question as a comment to a four-year-old thread is considered bad form. You should start a new thread.
In any case, this file should be easy for readtable in recent versions of MATLAB. At worst, you may need to use detectimportoptions. In that new thread, say what version of MATLAB, exactly what you have done so far, and exactly what result you got.
채택된 답변
Geoff Hayes
2014년 9월 3일
fid = fopen('Data_LDTT.csv');
if fid>0
% note how we skip the header lines and use the delimiter
data = textscan(fid,'%d %s %f %d','Delimiter',',','HeaderLines',10);
% close the file
fclose(fid);
% do stuff
end
data will be a cell array, with its second column containing all of the date strings of the format
'01/09/2014 15:27:18:322'
You may then want to convert each string into a serial date number using datenum and then subtract from that serial date number, the equivalent for the reference time. For example,
refTime = datenum('01/09/2014 15.13.48','mm/dd/yyyy HH.MM.SS');
curTime = datenum('01/09/2014 15:27:18:322','mm/dd/yyyy HH:MM:SS:FFF');
timeInMins = (curTime-refTime)*24*60
timeInMins =
13.5053667984903
The above assumes that the reference time is from the line
Acquisition Date:,01/09/2014 15.13.48
Since the serial date number is the whole and fractional number of days from a fixed, preset date, then to convert to minutes, we must multiply by 24 (the number of hours in a day) and by 60 (the number of minutes in an hour). The 13.5 minute answer (above) seems reasonable given the two time strings.
댓글 수: 0
추가 답변 (2개)
VISWANATH
2014년 9월 8일
댓글 수: 1
Geoff Hayes
2014년 9월 8일
You may not need a for loop. You've changed the input file so I can't test it out like I did before, but since the second column of data corresponds to the date and time information, then try the following
fid = fopen('Data_LDTT.csv');
if fid>0
% note how we skip the header lines and use the delimiter
data = textscan(fid,'%d %s %f %d','Delimiter',',','HeaderLines',10);
% close the file
fclose(fid);
% grab the date and time column
dateAndTimeData = data{2};
% convert to serial
dateAndTimeSerial = datenum(dateAndTimeData(:,1),...
'mm/dd/yyyy HH:MM:SS:FFF');
% set the reference time
refTimeSerial = datenum('01/09/2014 15.13.48','mm/dd/yyyy HH.MM.SS');
% compute the time in minutes from reference
timeInMinsFromRef = (dateAndTimeSerial-refTimeSerial)*24*60
end
Try out the above - it should give the desired result without having to do a loop.
Jayant Singh
2015년 9월 4일
Dear friends, This seems to be a very helpful place for clarifications.
I have a question regarding textscan. When we use textscan, how can I get all the data in rectangular format? I have tried it. I only get series of columns data{1}, data{2}, and so on. Is it possible to get everything back in rectangular format? Any response is greatly appreciated.
thanks
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!