how to format dates in textfiles to read in matlab

If the date in the textfile looks like 4/25/2012, should I put this as a string or number?
Also, I have a question regarding reading textfiles... So I have 78 variables (made to columns) in this text file, with about 500 rows of data. Might this be a problem for MATLAB in reading this txt file? Also, many rows have missing data - might this be a problem as well?
The way I have it right now is this:
file_name=strcat('2008_tobii.txt');
fid=fopen(file_name);
C=textscan(fid, '%*f %*f %*s %*s %*f %*f %*s %*f %*f %*f %*s %*s %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %s %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %f %f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %*f %f %*f %*f %*f','Delimiter','\t','HeaderLines',1)
fclose(fid)
The date is specified as %*f in one of these formats.
Thanks!!

Answers (1)

Read "4/25/2012" as a string and use 'mm/dd/yyyy' with datenum or datevec to parse the date strings.
Specify the date as '%*s' if you don't want to keep them.
Neither number of columns nor missing data should be a problem.
You might want to specify 'EmptyValue'
--- Cont. ---
Are there spaces together with the tabs (\t) in the text file? Whether or not 'Whitespace' takes care of them. Remove the spaces from the format string.
--- Alternative ---
Try this as proposed by Walter
frm = '%u/%u/%u%f%f';
str = '4/25/2012;17;18';
cac = textscan( str, frm, 'Delimiter', ';', 'CollectOutput', true )
which produce
cac =
[1x3 uint32] [1x2 double]
I use ";" because it is visible

1 Comment

If you read the date as a series of numbers then datenum([Y M D]) to get the serial date number.
Whether to read as a string or a number depends on what kind of processing you want to do with it, and upon whether the format is consistent or not.

Sign in to comment.

Categories

Asked:

on 17 Jun 2012

Community Treasure Hunt

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

Start Hunting!