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

采纳的回答

Adam Danz
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
  1. convert the time values in column 2 to durations
  2. 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
  3. Add the durations to the rounded dates
% 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
T = 5×3 table
dates times DateTime __________ ____________ _______________________ 19/09/2020 00:00:01.500 19/09/2020 00:00:01.500 19/09/2020 00:00:06.554 19/09/2020 00:00:06.554 19/09/2020 00:00:08.378 19/09/2020 00:00:08.378 19/09/2020 00:00:09.288 19/09/2020 00:00:09.288 19/09/2020 00:00:09.364 19/09/2020 00:00:09.364
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
T = 5×3 table
dates durations DateTime __________ ____________ _______________________ 19/09/2020 00:00:01.500 19/09/2020 00:00:01.500 19/09/2020 00:00:06.554 19/09/2020 00:00:06.554 19/09/2020 00:00:08.378 19/09/2020 00:00:08.378 19/09/2020 00:00:09.288 19/09/2020 00:00:09.288 19/09/2020 00:00:09.364 19/09/2020 00:00:09.364
  17 个评论
Adam Danz
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.
Remember Samu
Remember Samu 2021-5-12
Hi Adam, Thank you so much, I used the second method, It worked!!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by