how to find the data for same period of time ?

조회 수: 4 (최근 30일)
pruth
pruth 2018년 5월 17일
답변: Peter Perkins 2018년 5월 17일
hi, I have two mat files, the first column of both files is date-time and the second column is data. I want to compare both the data sets. however, all dates and time are not same. here I want to find data from both files which has same measurement date and time. other data I won't need it. at last, i would get one mat file which would have date-time, data from first file and data from the second file. any idea how to make it possible ?

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 17일
편집: Ameer Hamza 2018년 5월 17일
Try this
data1 = load('MRRarranged.mat');
data2 = load('distro-data.mat');
dates1 = data1.MRR_arranged_data(:,1);
dates2 = data2.rawdata(:,1);
indexDates2 = ismember(dates2, dates1);
indexDates1 = ismember(dates1, dates2);
finalDates = data2.rawdata(indexDates2, 1);
finalData1 = data1.MRR_arranged_data(indexDates1, 2);
finalData2 = data2.rawdata(indexDates2, 2);
final = [table(finalDates) table(finalData1) table(finalData2)];
then you can save the variable final.
  댓글 수: 4
pruth
pruth 2018년 5월 17일
Ameer, that works as expected. thanks a lot. I find it interesting to play with Matlab commands. I found another command which also should work.
[C,ia,ib] = intersect(rawdata,MRR_arranged_data,'rows')
i tried this but it gives me empty matrix. i dont know why ? anyway your code works perfect.
Ameer Hamza
Ameer Hamza 2018년 5월 17일
The option 'rows' only compares row-wise
A B
1 2 <- does not intersect
2 2 <- intersect
3 1 <- does not intersect
1 1 <- intersect
Since you don't want this behavior, use the command like this
[C,ia,ib] = intersect(rawdata,MRR_arranged_data)

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 5월 17일
It may be that join on timetables is the way to go here.
>> load('distro-data.mat')
>> load('MRRarranged.mat')
>> tt1 = array2timetable(MRR_arranged_data(:,2:end),'RowTimes',datetime(MRR_arranged_data(:,1),'ConvertFrom','datenum'))
tt1 =
15314×3 timetable
Time Var1 Var2 Var3
____________________ ____ ____ ____
05-Apr-2018 00:00:02 0 0 0
05-Apr-2018 00:00:12 0 0 0
05-Apr-2018 00:00:22 0 0 0
05-Apr-2018 00:00:32 0 0 0
05-Apr-2018 00:00:42 0 0 0
[snip]
>> tt2 = array2timetable(rawdata(:,2:end),'RowTimes',datetime(rawdata(:,1),'ConvertFrom','datenum'))
tt2 =
2880×3 timetable
Time Var1 Var2 Var3
____________________ ____ ____ ______
05-Apr-2018 10:08:59 0 0 0.0049
05-Apr-2018 10:09:59 0 0 0.0049
05-Apr-2018 10:10:59 0 0 0.0049
05-Apr-2018 10:12:00 0 0 0.0049
05-Apr-2018 10:13:00 0 0 0.0049
[snip]
>> tt12 = innerjoin(tt1,tt2,'Key','Time')
tt12 =
69×6 timetable
Time Var1_tt1 Var2_tt1 Var3_tt1 Var1_tt2 Var2_tt2 Var3_tt2
____________________ ________ ________ ________ ________ ________ ________
05-Apr-2018 10:12:00 0 0 0 0 0 0.0049
05-Apr-2018 10:13:00 0 0 0 0 0 0.0049
05-Apr-2018 10:14:00 0 0 0 0.0006 0 0.005
05-Apr-2018 10:18:00 0 0 0 0 0 0.005
05-Apr-2018 10:19:00 0 0 0 0 0 0.005
[snip]
I can't tell if this is what you are looking fior; there are only 69 matches. It may be that you really want some form of synchronize.

카테고리

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