Problem converting a cell array of integers to datetime

Hi, I have an array if integers (100x1 cell), like 736696, 736697, ....
I try to use b = datetime(a, 'ConvertFrom', 'datenum'), but keep getting error "Input data must be a numeric or a cell array or char matrix containing date/time strings." Why?
Thanks

 採用された回答

Jan
Jan 2017 年 2 月 1 日
編集済み: Jan 2017 年 2 月 1 日

0 投票

If your data is really stored in a cell:
a = {736696, 736697};
v = [a{:}]; % Converted to vector
b = datetime(a, 'ConvertFrom', 'datenum')
Many functions of Matlab expect numeric values in a numeric array and not as a cell. Cells are useful to store elements of different size or type.

4 件のコメント

JFz
JFz 2017 年 2 月 1 日
Thanks. This works. What if a is a column in a table?
Steven Lord
Steven Lord 2017 年 2 月 1 日
% Generate some sample data
theNextWeek = datetime('today') + days((0:6).')
N = datenum(theNextWeek)
% Use that sample data to build a table
T = table(N, 'VariableNames', {'Dates'})
% Extract the variable from the table and convert it
DT = datetime(T.Dates, 'ConvertFrom', 'datenum')
Compare theNextWeek and DT.
JFz
JFz 2017 年 2 月 1 日
Thanks. When I ran the above sample code, there is no problem. But when I ran my small table, with it, I keep getting error: Input data must be a numeric or a cell array or char matrix containing date/time strings.
If I ran my code as below it works: out = datetime(myT{1}, 'ConvertFrom', 'datenum') but if I ran my code as: out = datetime(myT, 'ConvertFrom', 'datenum') It gives me the error message. I am quite puzzled.
JFz
JFz 2017 年 2 月 1 日
OK. Found my problem: I have to use a cell2mat to convert my dates first! Thanks for all the help.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

タグ

質問済み:

JFz
2017 年 2 月 1 日

コメント済み:

JFz
2017 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by