Missing 2000 years on date from csv file.
7 ビュー (過去 30 日間)
古いコメントを表示
Hi,
When I read date column from a csv file, Matlab lacks 2000 years on every single date row. For example on the csv file it is 01/01/2018, Matlab reads it 01/01/0018. I use the function 'readtable'. Do you have any explanation to this, or any idea on how to fix this error? It is important for me to store the loaded data in a table format as I use functions like 'timetable' and 'retime' afterwards. Thank you
2 件のコメント
採用された回答
madhan ravi
2019 年 1 月 12 日
Perhaps?
T=readtable('Treasury.csv');
a=strrep(string(T.Date),'0018','2018');
T.Date=a
6 件のコメント
Stephen23
2019 年 1 月 12 日
"Let me know if you have a more efficient method. This one works, but I am sure there is better."
The better and more efficient method is to store the complete date in the CSV file, preferably in an ISO 8601 date format.
その他の回答 (2 件)
Corinne Beier
2019 年 4 月 24 日
I was able to fix my 0018 and 0019 datetime issue using this:
opts = detectImportOptions('/path/to/file.csv');
opts = setvaropts(opts,'myDateVariableName','InputFormat','MM/dd/y HH:mm');
data = readtimetable('/path/to/file.csv', opts);
The key for me was changing the date time format to include only 1 'y':
'MM/dd/y HH:mm'
Because I originally tried
'MM/dd/yyyy HH:mm' and 'MM/dd/uuuu HH:mm'
and they both did not work.
Also, I did have some trouble with my variable name. See below.
'myDateVariableName'
is the name of the column that has datetime values. Please note, if Matlab is changing your variable for you, use Matlab's version of the variable name. e.g. my data was actually labeled as "Channel Name:" and Matlab always changed the name to "ChannelName_" For my fix to work I had to use 'ChannelName_' as 'myDateVariableName'
Hope this helps.
0 件のコメント
Peter Perkins
2019 年 1 月 23 日
If all you have is a missing "20", the quick and dirty solution is to add calyears(2000) to each datetime in your table. But the right thing to do is to use a format when reading the data, preferably using detectimportoptions.
And of course Stephen is correct: the real solution is to try to get the data in the file to be in a less ambiguous form.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!