How do I import a timestamp from an ASCII File?

3 views (last 30 days)
Good Evening,
I am trying to calculate the difference in phase% (one of the columns in the documents that I attached) between two sensors -- Sensor 1 and Sensor 2. The main problem is that each sensor begins recording at a different time point. Luckily, they have timestamps. I am interested in utilizing these timestamps to "line up" the data to subtract the correct number from Sensor 2 at the same specific point in time from Sensor 1. Eg. (12:30:00 Sensor 1 Phase% Value) - (12:30:00 Sensor 2 Phase% Value).
I have been very successful using the Import Wizard and I can manipulate the data but the time column is converted into a string leaving the same, very small decimal in each cell. How do I indicate to the Import Wizard that the timestamp should be a different format or is there a simple way to manipulate the code to import the data as a time (hh:mm:ss)? I have attached .txt files instead of the original ASCII files and I have also attached the .m file that the import wizard has generated.
Thanks! Brittany

Accepted Answer

dpb
dpb on 6 Jul 2014
I don't use importdata; I'd recommend just reading the data files. Then convert the datestrings to internal *datenum*s and then you can do any selection/alignment/whatever using the datenum values (as well as plotting versus actual time).
fid=fopen('sensor1.txt');
data=textscan(fid,fmt,'headerlines',39,'collectoutput',true,'delimiter',';');
dn=datenum([char(data{1,1}(1:10,1)) char(data{1,1}(1:10,2))],'mm/dd/yyyyHH:MM:SS AM');
fid=fclose(fid);
NB: the concatenation of the cell strings in the first cell which contains a 2-column cell array of the date and time. Change the (1:10, ) reference to just show the first 10 rows to (:, ) with the first/second column as the second subscript as shown to get the full data array.
>> datestr(dn)
ans =
09-Apr-2014 17:36:34
09-Apr-2014 17:36:35
09-Apr-2014 17:36:36
09-Apr-2014 17:36:37
09-Apr-2014 17:36:38
09-Apr-2014 17:36:39
09-Apr-2014 17:36:40
09-Apr-2014 17:36:41
09-Apr-2014 17:36:42
09-Apr-2014 17:36:43
doc datenum % and friends
doc datetick % when you get ready for plotting...

More Answers (0)

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!