フィルターのクリア

How to convert character cells to date?

12 ビュー (過去 30 日間)
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 22 日
コメント済み: Peter Perkins 2017 年 10 月 22 日
Hi! I am a newbie in matlab programming. I imported csv file which contain date, time and number in it. Unfortunately, date can not be imported if it is still in the date format, so I change it to character format. The dates are in Germany version. I attach the picture.
I want it to be in the date format so I try to convert it with various method available on internet. I have been trying to find how to convert it to date format since yesterday, but I found nothing. Here is my code :
finalCSVnew{:,1}=datetime(finalCSVnew{:,1},'InputFormat','dd.MM.yyyy');
I got an error "The following error occurred converting from datetime to cell: Conversion to cell from datetime is not possible."
Please help me :(. Thank you very much for your help.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 22 日
Please attach your data as mat-file.

サインインしてコメントする。

採用された回答

KL
KL 2017 年 10 月 22 日
編集済み: KL 2017 年 10 月 22 日
The image you have attached says it's a table. In that case, it's as simple as,
finalCSVnew.Date = datetime(finalCSVnew.Date,'InputFormat', 'dd.mm.yyyy');
But I also see 'Time' column on your table, I'd recommend to combine these two to form a proper datetime.
Even better idea is to change your table into a timetable. I have been mentioning this almost everyday.
Import your csv file as table and then create a timetable from it .Then you can do whatever you want to do with it, like monthly mean, yearly sum, etc. For example,
data_table = readtable('yourfile.csv');
data_timetable = table2timetable(data_table);
  1 件のコメント
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 22 日
Thank you. It really works for my code

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2017 年 10 月 22 日
Cells can be tricky to work with. The problem you encountered here is that you can't batch-process cells this way, you'll need to use cellfun to do this.
Hint: as function handle you can put @(x) datetime(x,'InputFormat','dd.MM.yyyy')
  3 件のコメント
Rik
Rik 2017 年 10 月 22 日
If you take a look at the documentation, you will notice that you need to provide a function. Because there isn't really a function that does what you need, you need to provide your own function. You can either make a separate m-file with that function, or you can supply an anonymous function.
anon_func=@(x) datetime(x,'InputFormat','dd.MM.yyyy');
output=cellfun(anon_func,...
Another hint (although Matlab will give a sufficiently clear error if you do it wrong): look at what value UniformOutput output should have in your case.
Peter Perkins
Peter Perkins 2017 年 10 月 22 日
I don't think you need to call cellfun. The datetime constructor accepts a cell array of date text.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by