how to find the data for same period of time ?

6 次查看(过去 30 天)
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