Importing formated time data into matlab

29 views (last 30 days)
Hi, I'm new to MatLab, but not completely new to programing, and I am ooking for a way to import data with a time stamp which formatted as hh:mm:ss AM/PM. Using the import wizard it doesn't recognise it as data but text. Is there a way of doing this or do I need to change the input file? Cheers, Karen

Accepted Answer

Kevin Holst
Kevin Holst on 14 Feb 2012
It all depends on what you're wanting to do with this data. You may be able to use the time just fine as a string. I'd suggest looking into using either datenum or datestr once the data is imported into matlab.
  4 Comments
Karen Hornsby
Karen Hornsby on 14 Feb 2012
I was loooking for a way to attache a text file but one did not present itself!
I'd rather not have to clip the meta data out completely as I have 2 identical intruments to analysis and they will be measuring at 2 differnt highs so I need to be able to keep them striaght in my head.

Sign in to comment.

More Answers (2)

Kevin Holst
Kevin Holst on 14 Feb 2012
Ok, so it looks like you won't be able to use matlabs fancy, easy data importer with this file, but if you will always be getting data in this format, you can create your own import function.
My suggestion, and this won't be pretty but it'll get the job done, is to do something like this:
fid = fopen('data.txt');
dateTimeStart = textscan(fgetl(fid),'Date/Time Start:%s','Delimiter','\t');
title = textscan(fgetl(fid),'Title:%s','Delimiter','\t');
comments = textscan(fgetl(fid),'Comments:%s','Delimiter','\t');
instrumentLabel = textscan(fgetl(fid),'Instrument Label:%s','Delimiter','\t');
fgetl(fid)
% etc collecting the data you want to keep in variables and using plain fgetl(fid) commands to skip a blank line
% for the channel size line, I'd probably use fgetl to get the whole line
% as a string and then cut off the first x characters, leaving just the
% numbers, and then you could use str2num to get those values into an array
% then we get to the real data.
textscan(fid,'%s %s %f %f %f %f %f %f %f %f %f %f') % this scans in all of your data into a 1x12 cell array, the first two columns are the time and AM/PM, the rest are numbers.
hope that helps!
  3 Comments
Karen Hornsby
Karen Hornsby on 13 Apr 2012
Hi Kevin,
I did try this, but no joy. However I was contacted by Matlab to help in a servey of sorts and they produced the following anwser to my throny time stamp issue.
dateTimeStart = textscan(fgetl(fid),'Date/Time Start:%s','Delimiter','\t')
dateString = dateTimeStart{1,1};% pulls data out of cell
formatString = '"dddd, mmmm dd, yyyy HH:MM:SS AM"'; % tells the datenum command what format the time stamp is in
dateTimeStart_datenum = datenum(dateString, formatString);
this works perfectly.
Thanks for all the help though :)

Sign in to comment.


Karam Jaradat
Karam Jaradat on 8 Aug 2016
I suggest you to import the dates to excel first and change the format of the dates column to general, then copy the content and save it in txt file, then import this file to Matlab, it should work!

Community Treasure Hunt

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

Start Hunting!