Read a time column in a csv (convert 'char' to date) with readtable

I am a new user of Matlab and I can't find an easy answer to a simple question: how can I use readtable for csv and recognize the first column as a date?
Here is my file Cp.csv (separator ";") :
date ; Cp
10/03/2005 12:30:00;1.07266
10/03/2005 13:00:00;4.70812
10/03/2005 13:30:00;3.91713
10/03/2005 14:00:00; 1.01443
10/03/2005 14:30:00; 1.20241
10/03/2005 15:00:00; 4.78315
10/03/2005 15:30:00;1.55072
I tried the following code:
t=readtable('Cp.csv','Format',%{dd/MM/yyyy HH:mm:ss}D%f')
I keep on getting the following error:
Undefined function or method 'readtable' for input arguments of type 'char'
Thanks in advance for helping

 Accepted Answer

The problem is nothing to do with the syntax of your call (which is correct, but probably you don't even need the Format option). It's all to do that you're using a very ancient version before readtable existed. readtable (and tables) requires at least version R2013b.
As it is you'd have to use textscan to parse your file. datetime didn't exist either in R2010b, so you'll have to parse the date as a datenum or datestring.
edit: Actually, looking at the document of textscan (login required) in R2010b, it did not support parsing of dates at all. You'll have to read the date as strings then later on do the conversion. Possibly, something like:
fid = fopen('CP.csv', 'rt');
data = textscan(fid, '%s%f', 'Delimiter', ';');
fclose(fid);
data{1} = datenum(data{1}); %if you want datenum instead of date strings

3 Comments

The datetime type was introduced in R2014b.
I tried this and got fail: I seond HOURS on date formatting. Do you have a section on reading dates from Excel:
The data:
% final.Date = datetime(final.Date,'ConvertFrom','Excel')
% File data
%1/1/1959 16.042
%2/1/1959 16.057
%3/1/1959 16.068
% Readtable output
%1959-01-01 16.042
%1959-02-01 16.057
%1959-03-01 16.068
Suggested answer:
fid = fopen('C:/Users/Owner/Documents/Research/Data/FRED/Macro/PCEPI.csv', 'rt');
data = textscan(fid, '%s%f', 'Delimiter', ';');
fclose(fid);
mdate{1} = datenum(data{1},'MM/DD/YYYY');
Error using datenum (line 201)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.

Sign in to comment.

More Answers (1)

Thanks madhan ravi, but I have first tried this, but I had the same error message, that is why I added the Format option, but it failed too

2 Comments

Madhan has deleted his answer. Most likely, because he missed that you were using R2010b which does not have readtable. As I said in my answer. readtable('Cp.csv') would have worked on its own if you were using a version that has readtable.
Yes Guillaume thanks! Your code makes me improving....but I still have a problem indicated with this error message: DATENUM failed ... caused by " error in using ==> datavec at 219 Failed to lookup month of year"

Sign in to comment.

Products

Release

R2010b

Asked:

on 12 Dec 2018

Commented:

on 3 Nov 2021

Community Treasure Hunt

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

Start Hunting!