Extracting 3 columns from a dataset into a new data set

조회 수: 7 (최근 30일)
Remember Samu
Remember Samu 2021년 5월 10일
댓글: Remember Samu 2021년 5월 12일
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