merge two columns into one

7 ビュー (過去 30 日間)
swetha S
swetha S 2019 年 10 月 18 日
回答済み: Steven Lord 2019 年 10 月 18 日
i have a date column (481*1) and time column (481 *1) separately. How do I merge it into one column in Matlab? Example,
date time
27/8/2019 00:48:20
I want the output as
datetime
27/8/2019 00:48:20
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 10 月 18 日
Attach your variable 'date' and 'time' as mat-file.

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

採用された回答

Steven Lord
Steven Lord 2019 年 10 月 18 日
I'm going to assume you have your data stored as text in a cell array.
dateAndTime = {'27/8/2019','00:48:20'}
Combine the two pieces of the date and time together.
dt = join(dateAndTime)
Looking at your data, I think the right format is to use d (for 1 or 2 digit day of month), M (for 1 or 2 digit month of year), yyyy (4 digit year), HH (24 hour time), mm (minutes), and ss (seconds).
fmt = 'd/M/yyyy HH:mm:ss';
Now convert dt into a datetime array.
dt2 = datetime(dt, 'InputFormat', fmt, 'Format', fmt)
We could skip specifying the InputFormat and datetime looks like it figures out the correct formatting to use for this particular date and time, but if instead of August 27th you wanted to convert August 1st without an InputFormat MATLAB could guess incorrectly (warning you of the ambiguity) and give you a different date and time -- January 8th.
dt3 = {'1/8/2019','00:48:20'}
dt4 = datetime(join(dt3))

その他の回答 (0 件)

カテゴリ

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