Need to import CSV file with date and time
20 views (last 30 days)
Show older comments
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 Comments
Peter Perkins
on 20 Feb 2019
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.
Accepted Answer
Geoff Hayes
on 3 Sep 2014
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 Comments
More Answers (2)
VISWANATH
on 8 Sep 2014
1 Comment
Geoff Hayes
on 8 Sep 2014
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
on 4 Sep 2015
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 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!