How do I manipulate data from a csv datafile containing date, time and number data?
Show older comments
This sounds intuitively easy but it has me puzzled. Columns 1 and 2 contain datetime formatted data, with column 3 double precision numerical. I can import the data using READTABLE with no apparent problem but subsequent manipulation continues to fall over. Is it best to split this into three separate (n x 1) arrays, or can I work with the data as one (n x 3) matrix? My routine is to read in the data, extract portions of the data (e.g. 600 rows) and then statistically analyse the values of the third column. The routine was working fine until I exceeded 24 hours of data, when it then ran into ambiguity due to ambiguous data type. I continue to run into a problem during subsequent code using DATEVEC, which produces an error message regarding data type, and using TABLE2ARRAY which also produces a data-type error. I am using R2016b. I am pretty new to Matlab and have attached a small sample input data. Cheers Bob
Accepted Answer
More Answers (1)
Peter Perkins
on 20 Jun 2017
1 vote
Your file has date and time in two separate fields. Depending on what version of MATLAB you're using, the table that readtable returns may already have datetimes in it, or it might have text. It sounds like the former. If that's correct, what will happen is that readtable will read the second column of your spreadsheet as datetimes, filling in "today" as the date portion.
You say,
"The routine was working fine until I exceeded 24 hours of data, when it then ran into ambiguity due to ambiguous data type."
but your sample file doesn't have an example of that, it's all on one day, all on the same hour. And it's not at all clear what you mean by ambiguous. I can only guess.
My guess is that you mean that while your date stamps run over several days, your time-only stamps seem not to. That's because of what I've described above. Try this: call timeofday on the second variable in the table that readtable returns. That creates a duration. Mow add that duration to the first var in the table. You now have the datetimestamps that I think you want. You may have to adjust the display format to show the time portion, but I think that will happen automatically.
Hope this helps.
1 Comment
Robert Fitzell
on 21 Jun 2017
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!