Convert vector of datenum values to datetime values

3 ビュー (過去 30 日間)
Kyle Reagan
Kyle Reagan 2017 年 6 月 12 日
回答済み: Peter Perkins 2017 年 6 月 20 日
The first column of a 3x2 matrix is datenum values. How do I convert each element to datetime? It looks as follows:
b = [736848] 'words'
[736849] 'words'
[736852] 'words'
I want to convert b(:,1) to either datestr or datetime form such that each row looks like
b = 02-Jun-2017.
Can anybody help me do this? I get an error if I try doing
datestr((b:,1)) or datetime(b(:,1))

採用された回答

Stephen23
Stephen23 2017 年 6 月 12 日
編集済み: Stephen23 2017 年 6 月 12 日
It seems that b is a cell array, and each cell in the first column contains one scalar datenum. You can use cell2mat to convert the first column of cells into a numeric vector:
vec = cell2mat(b(:,1));
datestr(vec)
datetime(vec)
Or vertcat:
vec = vertcat(b{:,1});
  1 件のコメント
Kyle Reagan
Kyle Reagan 2017 年 6 月 12 日
Thanks, Stephen! It works just as anticipated.

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2017 年 6 月 20 日
There's actually a direct conversion, once you lose the cell array:
vec = cell2mat(b(:,1));
datetime(vec,'ConvertFrom','datenum')
This is more than a convenience, there is some numeric magic to try to mitigate the fact that datenum uses a somewhat awkward unit of time, i.e. 1 day.

カテゴリ

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