- convert the time values in column 2 to durations
- round the dates in column 1 down to the start of each day since there could be time components in those datetime values that aren't displayed
- Add the durations to the rounded dates
Extracting 3 columns from a dataset into a new data set
7 次查看(过去 30 天)
显示 更早的评论
Hi all, can someone please help on how I can combine columns Var1 and Var2 into 1 column named for example VarX then create a new data set that will consist of only varX and Var4?
Var1 Var2 Var3 Var4 Var5 Var6 Var7
__________ ____________ ____ ____ ____ ____ ____
19/09/2020 00:00:00.231 -0.6 -0.6 0.4 12.1 15.2
19/09/2020 00:00:10.220 -0.7 -0.6 0.4 12.1 15.2
19/09/2020 00:00:20.223 -0.6 -0.6 0.4 12.1 15.2
19/09/2020 00:00:30.230 -0.7 -0.6 0.4 12.1 15.2
19/09/2020 00:00:40.232 -0.6 -0.6 0.4 12.1 15.2
0 个评论
采纳的回答
Adam Danz
2021-5-10
编辑:Adam Danz
2021-5-10
Join a column of dates with a column of times - datetime format
Assuming you're working with datetime values in both columns, the safe way to combine the dates and times is to
% Create demo table
rng('default') % for reproducibility
dates = datetime('19/09/2020','Format','dd/MM/yyyy') + zeros(5,1);
times = datetime('00:00:00.231','format','HH:mm:ss.SSS') + seconds(sort(rand(5,1)*10));
T = table(dates, times);
% Convert times to durations & remove any hidden date components
justTime = T.times - dateshift(T.times,'start','day');
% Round the dates down to the start of each day, removing any
% hidden time components, and add the time durations
fullDateTime = dateshift(T.dates,'start','day') + justTime;
fullDateTime.Format = 'dd/MM/yyyy HH:mm:ss.SSS';
% add to table
T.DateTime = fullDateTime
Join a column of dates with a column of durations
If your time column does not contain datetime value but contains duration values instead, you can skip a step the process above,
% Create demo table
rng('default') % for reproducibility
dates = datetime('19/09/2020','Format','dd/MM/yyyy') + zeros(5,1);
durations = duration('00:00:00.231','format','hh:mm:ss.SSS') + seconds(sort(rand(5,1)*10));
T = table(dates, durations);
% Round the dates down to the start of each day, removing any
% hidden time components, and add the time durations
fullDateTime = dateshift(T.dates,'start','day') + T.durations;
fullDateTime.Format = 'dd/MM/yyyy HH:mm:ss.SSS';
% add to table
T.DateTime = fullDateTime
17 个评论
Adam Danz
2021-5-11
That error is very different from the error you shared eariler. It's critical to share the entire error message when you're trying to fix the error. Otherwise, we're spending time on the wrong problems.
I also don't konw what version of Matlab you're using which makes it difficult to rule out any version differences.
It looks like you're trying to shift the dates of duration values rather than datetime values. You're using the first method of my answer which assumes the time values are datetime but instead you should be using the second method in my answer which assumes the time values are durations.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!