Need to import CSV file with date and time

20 ビュー (過去 30 日間)
VISWANATH
VISWANATH 2014 年 9 月 3 日
コメント済み: Peter Perkins 2019 年 2 月 20 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 2 月 19 日
ashwani - what code have you written to read the data from the file?
Peter Perkins
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
Geoff Hayes 2014 年 9 月 3 日
Use textscan to grab all of the data from file
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.

その他の回答 (2 件)

VISWANATH
VISWANATH 2014 年 9 月 8 日
Thanks a lot Geoff. I need to implement a for loop to subtract each of the evolved time stamps from the reference.
  1 件のコメント
Geoff Hayes
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
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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by