Importing formated time data into matlab
9 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
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.
More Answers (2)
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');
% 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!
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!