How to extract year, month, day from character vectors?

4 ビュー (過去 30 日間)
Paul Barrette
Paul Barrette 2022 年 8 月 11 日
コメント済み: Paul Barrette 2022 年 8 月 14 日
I have a character vector matrix (I think that's what it's called - it was obtained with cell2mat command applied to a *.csv file) which shows this when I open it (one column):
var =
'1970/01/01'
'1970/01/02'
'1970/01/03'
'1970/01/04'
'1970/01/05'
'1970/01/06'
I would like to produce a numerical matrix X with as many rows but three columns (year, month, day, respectively). I've explored numerous options, to no avail. Any suggestions?

採用された回答

Kevin Holly
Kevin Holly 2022 年 8 月 11 日
編集済み: Kevin Holly 2022 年 8 月 11 日
var =['1970/01/01'
'1970/01/02'
'1970/01/03'
'1970/01/04'
'1970/01/05'
'1970/01/06']
var = 6×10 char array
'1970/01/01' '1970/01/02' '1970/01/03' '1970/01/04' '1970/01/05' '1970/01/06'
% I want to make you aware of Datetime arrays
datetime(var,'InputFormat','yyyy/MM/dd')
ans = 6×1 datetime array
01-Jan-1970 02-Jan-1970 03-Jan-1970 04-Jan-1970 05-Jan-1970 06-Jan-1970
% Here is how you can convert your data into a numerical matrix containing Year, Day, and Month columns
m = [];
for ii = 1:size(var,1)
m = [m;split(var(ii,:),"/")'];
end
X = str2double(m)
X = 6×3
1970 1 1 1970 1 2 1970 1 3 1970 1 4 1970 1 5 1970 1 6
  1 件のコメント
Paul Barrette
Paul Barrette 2022 年 8 月 14 日
This works. Thanks Kevin. Much appreciated.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by