Issue with loading datetime from table

12 ビュー (過去 30 日間)
Sue MM
Sue MM 2019 年 7 月 23 日
回答済み: Eric Sofen 2019 年 7 月 29 日
Hi, I have a simple 3 column CSV file with column 1 = names, column 2= address column = dates. I load the CSV using readtable()
Data= readtable(FileMaker.csv);
C = table2cell(data)
Dates = C(:,3)
DateId = datetime(dates)
However I keep getting the error using datetime(639) input data must be numeric array, string array, cell array containing character vectors, or char matrix.
The cell for dates is showing up as a datetime when I look at the workspace but I’m not sure how to overcome the error message. Any help would be greatly appreciated!
  2 件のコメント
madhan ravi
madhan ravi 2019 年 7 月 23 日
FYI , the below is not a valid MATLAB syntax
Dates = C(:,)
Sue MM
Sue MM 2019 年 7 月 23 日
Typo in the question meant C(:,3)

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

回答 (2 件)

Eric Sofen
Eric Sofen 2019 年 7 月 29 日
There's no reason to go through cell to pass the datenums into datetime:
Data = readtable(FileMaker.csv);
Dates = datetime(Data.Var3, 'ConvertFrom', 'datenum') % assuming default variable names in the table.

Joel Handy
Joel Handy 2019 年 7 月 23 日
編集済み: Joel Handy 2019 年 7 月 23 日
There appears to be two issues with your code. The first is that datetime doesn't take a cell array of numbers so you need to convert the cell array containing datetimes to a numeric array. The second issue is that datetime needs you to explicitly tell it that you are giving it datenums. I think the following code will work for you.
Dates = [C{:,3}];
% OR
Dates = cell2mat(C(:,3));
DateId = datetime(Dates, 'ConvertFrom', 'datenum')

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by