I would like to merge two different column in one as datetime

조회 수: 2 (최근 30일)
Younes
Younes 2023년 8월 29일
댓글: Younes 2023년 9월 1일
I want to merge these two column in one column as datetime column
  댓글 수: 3
Younes
Younes 2023년 8월 29일
Thanks stephen
I am not using month and year the type of the datetime data that i want like "DD HH:mm"
Stephen23
Stephen23 2023년 8월 29일
"I am not using month and year the type of the datetime data that i want like "DD HH:mm""
DATETIME objects always have a year and month.
It sould like you should be using a DURATION object instead:

댓글을 달려면 로그인하십시오.

답변 (2개)

Katy Weihrich
Katy Weihrich 2023년 8월 29일
I added two versions. I think you will wanted Version 2 but Version 1 illustrates how to use durations. I prefere to work with durations when no date is given (see Version 1).
%% simulate data
Date = table(ones(9,1),'VariableNames',{'DAY'});
Time = table(char("00:"+string(num2str((0:5:40)','%02.f'))),'VariableNames',{'TIME'});
%% Version 1) transform the number varibale of "Day" into a duration variable
% transform the numeric data into duration
iday = days(Date.DAY);
% transform the string data into datetime
itime = datetime(string(Time.TIME),"InputFormat","hh:mm");
% transform the datetime data into duration
imin = minutes(minute(itime));
ih = hours(hour(itime));
% add day + h + min durations together
iduration = iday+ih+imin;
% Output:
% iduration =
% 9×1 duration array
% 1 day
% 1.0035 days
% 1.0069 days
% 1.0104 days
% 1.0139 days
% 1.0174 days
% 1.0208 days
% 1.0243 days
% 1.0278 days
%% Version 2: transform the char data into datetime, remove today and add the day as duration
% note: if no day is given, datetime gives you the current day and time
% >> datetime
% ans =
% datetime
% 29-Aug-2023 15:29:24
% >> datetime(string(Time.TIME(5,:)),"InputFormat","hh:mm")
% ans =
% datetime
% 29-Aug-2023 00:20:00
itime = datetime(string(Time.TIME),"InputFormat","hh:mm") - today('datetime') + days(Date.DAY);
% Output:
% itime =
% 9×1 duration array
% 24:00:00
% 24:05:00
% 24:10:00
% 24:15:00
% 24:20:00
% 24:25:00
% 24:30:00
% 24:35:00
% 24:40:00
  댓글 수: 4
Katy Weihrich
Katy Weihrich 2023년 8월 31일
Thank you. I keep forgetting that you can change datetimes properties this way.
Younes
Younes 2023년 9월 1일
Thank you very much for your help

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2023년 8월 29일
% Fake data:
A = ones(5,1)
A = 5×1
1 1 1 1 1
B = {'0:00';'0:05';'0:10';'0:15';'0:20'}
B = 5×1 cell array
{'0:00'} {'0:05'} {'0:10'} {'0:15'} {'0:20'}
% Convert to DURATION:
M = str2double(split(B,':'));
D = days(A)+duration(M(:,1),M(:,2),0);
D.Format = 'dd:hh:mm:ss'
D = 5×1 duration array
01:00:00:00 01:00:05:00 01:00:10:00 01:00:15:00 01:00:20:00

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by