Importing via csvread and having dates in 'm/d/yyyy h:mm' format.

3 views (last 30 days)
Hello, I'm importing some data files via csvread. I want to use csvread since I can specify to skip the first ~4 rows which just contain strings (header information). Unfortunately, all the dates are in the custom excel format m/d/yyyy h:mm which looks like this:
11/15/2012 11:05
In excel's general format this converts to 41228.46227 which is perfect for my analysis purposes since I need to break down the data by date and time. Manually converting it to the general format has been my work around, but isn't acceptable for the final script I want to write.
I suppose what I want to do is read in the date/time as a string and then perform the conversion in matlab.
The full error text I receive is:
"
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 2u) ==> /15/2012 11:05,0,0\n
Error in csvread (line 48) m=dlmread(filename, ',', r, c);
Error in testing (line 5) raw_data = csvread('FSA00147_Main_Cleaned.csv');
"
I'm not sure what's going on here, something about the date is throwing dlmread.
Any help is greatly appreicated! If you can suggest and alternate way to read in these .csv files, that would work too!

Accepted Answer

dpb
dpb on 27 Jan 2014
From help csvread --
"M = csvread('FILENAME') reads a comma separated value formatted file
FILENAME. The result is returned in M. The file can only contain
numeric values."
IOW, you can't use csvread for the purpose reading string data per the design limitations as documented.
Use textscan or similar instead.
  2 Comments
Alfonso
Alfonso on 27 Jan 2014
Crap, I overlooked that. I'll investigate a work around. Thanks.
dpb
dpb on 27 Jan 2014
textscan is the obvious choice albeit returns cell arrays (which is ok for the string data). Since you didn't give an example of the actual data file (I presume it contains something besides the date fields) can't give a specific format string but shouldn't be too tough.
Then,
doc datenum % to convert to datenums in Matlab form
NB: they're equivalent with a different origin to Excel but simpler to keep self-consistent inside Matlab to use.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!