Extracting 3 columns from a dataset into a new data set

4 views (last 30 days)
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

Accepted Answer

Adam Danz
Adam Danz on 10 May 2021
Edited: Adam Danz on 10 May 2021
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 Comments
Adam Danz
Adam Danz on 11 May 2021
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 on 12 May 2021
Hi Adam, Thank you so much, I used the second method, It worked!!!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!