Fetch data on specific condition from matlab

1 次查看(过去 30 天)
26-Apl-2014 18:30:00 50 60 33
26-May-2014 17:30:00 60 80 54
26-May-2014 18:30:00 40 50 98
27-May-2014 10:44:34 56 43 76
28-May-2014 12:33:54 45 65 43
26-Jun-2014 10:30:00 78 79 44
26-jul-2014 11:30:00 33 54 39
26-Sep-2014 15:30:00 88 45 74
Suppose i have data similar to above and each column is saved in different variables. I want to fetch all data like mysql that meet the specific condition on time e.g. from 26-May-2014 18:30:00 to 28-May-2014 20:00:00. Kindly tell me the solution how to do it.
  3 个评论
siki
siki 2017-1-3
code should fetch all data between 26-May-2014 to 28-May-20:00:00. No matter if 28-May-2014 20:00:00 is not given in datetime.
siki
siki 2017-1-3
i can not keep my data in txt file.... Its huge I have saved each column in a .mat file and I load all mat files. The data i have given in this question is not the same... its sample data(to ask how to fetch data)

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2017-1-3
fid = fopen('data.txt') ; % let your data be in txt file
S = textscan(fid,'%s','delimiter','\n') ;
fclose(fid) ;
S = S{1} ;
d0 = '26-May-2014 18:30:00' ;
d1 = '26-jul-2014 11:30:00' ;
% get the indices of d0 to d1 to getch data
idxd0 = strfind(S,d0);
idxd0 = find(not(cellfun('isempty',idxd0)));
%
idxd1 = strfind(S,d1);
idxd1 = find(not(cellfun('isempty',idxd1)));
% extract the data
iwant = S(idxd0:idxd1)

Guillaume
Guillaume 2017-1-3
What do you mean by fetch?
If you mean you only want to load the parts of variables in a mat file that satisfy a condition, then it's easily possible, I'm afraid. I guess, you could sort of subvert mapreduce to subset your data but that would involve writing your own FileDatastore which would still have to load the entirety of the mat files.
If you mean you want to keep only the rows of the variables that match the condition, then it's fairly easy. While it can be done with individual variables, I would recommend you regroup them all into a table as it makes the filtering easier. In any case, filtering is simply done with logical operations. e.g:
var1 = {'26-Apr-2014 18:30:00'
'26-May-2014 17:30:00'
'26-May-2014 18:30:00'
'27-May-2014 10:44:34'
'28-May-2014 12:33:54'
'26-Jun-2014 10:30:00'
'26-jul-2014 11:30:00'
'26-Sep-2014 15:30:00'};
var2 = [50 60 40 56 45 78 33 88].';
var3 = [60 80 50 43 65 79 54 45].';
var4 = [33 54 98 76 43 44 39 74].'
%merge everything into a table. convert date strings to date time for easy manipulation
t = table(datetime(var1), var2, var3, var4, 'VariableNames', {'date', 'something', 'another', 'col4'});
%filter by date:
t(t.date>= datetime('26-May-2014 18:30:00') & t.date <= datetime('28-May-2014 20:00:00'), :)

Community Treasure Hunt

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

Start Hunting!

Translated by