How do I import a timestamp from Excel/csv?

I have a .csv file with 6 columns of data and 14000 or so rows. I attached a file with a screenshot of the spreadsheet. I have tried
data=load('My_Spreadsheet_Name');
timestamp=data(:,1);
and since learned why that won't work. I then tried
xlsread('My_Spreadsheet_Name');
and that worked for everything that wasn't a timestamped piece of data. The 5 columns on the right imported into Matlab just fine but Matlab skipped over the 1st set of data that I needed.
This Link had a similar question but I'm either too dumb to figure out the formatting properly or I'm trying to import differently formatted data.
I'm hoping someone smarter than me will be able to figure out what I'm doing incorrectly.

Respuestas (2)

C.J. Harris
C.J. Harris el 6 de Jul. de 2017
Try this:
[NUMERIC, TXT, RAW] = xlsread('My_Spreadsheet_Name');
You'll find the timestamps in both the TXT and RAW outputs. If you call xlsread the way you are, then you'll just get the numeric data.

2 comentarios

Dan Getz
Dan Getz el 6 de Jul. de 2017
Editada: Dan Getz el 6 de Jul. de 2017
Thank you so much. That worked for me. Is there a way to import the data as being double? I'm trying to interpolated based on 3 hr data and I can't interpolate with cell data.
time_col = raw(:,1)
num_cols = cell2mat( raw(:,2:end) );
Now you can convert time_col with datenum() or (better) datetime()

Iniciar sesión para comentar.

Peter Perkins
Peter Perkins el 7 de Jul. de 2017

1 voto

The best way to do this in a recent version of MATLAB is to use readtable, and create datetimes. This will free you from all the issues of converting excel serial dates.

4 comentarios

Dan Getz
Dan Getz el 7 de Jul. de 2017
So I used readtable and I successfully pulled all of the data from the table. Then I used table2array to convert the table to datetime values. The datetime values are correct except for the year. Instead of 2011, the year is 0011. Any ideas how to track that down? I'm guessing it's something where excel's data stored 2011 as just 11.
Walter Roberson
Walter Roberson el 7 de Jul. de 2017
Editada: Walter Roberson el 7 de Jul. de 2017
readtable() does not use excel to load data from .csv files.
The easiest way is to use readtable with the 'DateTimeType', 'text' option and then convert the times afterwards. That would be done with
datetime(TheTimeCells, 'InputFormat', 'M/d/y h:mm a')
Peter Perkins
Peter Perkins el 11 de Jul. de 2017
For a CSV, you can also specify the 'Format' parameter in the call to readtable, with something like %{'M/d/yy h:mm a'}D for that field.
Or quick and dirty: t.Date.Year = t.Date.Year + 2000
Walter Roberson
Walter Roberson el 11 de Jul. de 2017
readtable is documented to interpret format strings the same way as textscan, and unless it has recently changed, textscan %D format specifiers cannot handle embedded spaces.
Ah.... testing now, I see that a '%{M/d/yy hh:mm aa}D' format spec can work for textscan, but only if whitespace has been set to exclude spaces.

Iniciar sesión para comentar.

Preguntada:

el 6 de Jul. de 2017

Comentada:

el 11 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by