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.
답변 (2개)
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)
댓글 수: 0
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'), :)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Database Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!