MATLAB Answers

Importing formated time data into matlab

16 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

  0 Comments

Sign in to comment.

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

Show 1 older comment
Kevin Holst
Kevin Holst on 14 Feb 2012
Could you provide me a sample couple of lines of the data you're attempting to import?
Karen Hornsby
Karen Hornsby on 14 Feb 2012
sure
Date/Time Start: Thursday, February 09, 2012 11:54:22 AM
Title: Outdoor air test 2/9/12 compare to original FMPS
Comments:
Instrument Label: TSI Model 3091,Serial Number 71105053, Firmware Version MCU:3.11,DSP:3.02
Instrument Status: Normal Status.
Instrument Errors: No Errors Detected
Dilution Factor: 1.000
Concentration [#/cmウ]
Channel Size [nm]: 6.04 6.98 8.06 9.31 10.8 12.4 14.3 16.5 19.1 22.1
Time
11:54:23 AM 150.692 160.423 313.266 609.222 762.272 772.416 779.164 782.516 722.794 600
11:54:24 AM 198.732 212.745 366.206 659.115 792.899 767.558 765.893 787.907 733.418 602.428
11:54:25 AM 190.677 201.242 363.418 677.203 834.797 836.2 833.33 826.187 747.499 597.266
11:54:26 AM 187.069 200.672 360.485 666.509 823.894 832.639 833.132 825.373 740.968 579.916
11:54:27 AM 170.977 171.95 331.503 649.637 820.455 843.956 833.809 790.015 695.145 549.201
11:54:28 AM 223.65 225.432 371.054 660.518 805.386 805.657 797.943 782.244 701.252 554.967
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 17 Feb 2012
Hi Kevin,
sorry it has taken me so long to look at this, one of the instruments has been playing up.
I have managed to pull out all the meta data in to variables, but the main body is proving tricky.
So I can separate the time from the rest of the data but the AM onwards all comes out together. I think this is because the values are comma serperated rather than space delimited. is there a way to change the delimiter part way through a row of data do you know?
Kevin Holst
Kevin Holst on 21 Feb 2012
Karen,
Sorry for the delay, I was on vaca :) Unfortunately I'm away from my computer with Matlab and am unable to try this suggestion, but have you tried something like:
textscan(fid,'%s %s %f %f %f %f %f %f %f %f %f %f','Delimiter',{' ',','})
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!

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by