- read in the text file as a timetable or a regular table. The date values should be read in as datetime values.
- use boolean operators to determine which rows have dates within your range. That should create a logical vector with one element per row. Alternatively, if you're working with a timetable you can use one of these methods .
- Use that logical index to get data from other columns.
Compare dates between a matrix and a given range and read the values
3 次查看(过去 30 天)
显示 更早的评论
I have some Text file where values (>thousands) are wriiten like this:-
Dates Time Values1 Values2 Values3
31/03/2021 15:01:34 56.45 89.85324 1000.98
31/03/2021 15:06:34 78.34 90.75836 1000.99
1/04/2021 9:01:34 60.29 72.89434 1001.50
2/03/2021 13:01:34 72.56 60.35986 1001.68
..... Upto thousnds of values
I want to check this file with a date range like (31/03/2021 15:01:34 to 31/03/21 to 15:06:34) or (1/04/2021 to 2/03/201)
and if this range is available I read those values from their corresponding tables.
Is there any short method to do this.
Please help me in this matter.
0 个评论
采纳的回答
Adam Danz
2021-3-31
编辑:Adam Danz
2021-3-31
There's lots of info on each of those steps in the documentation and the forum. If you get stuck update us on where you're at and what the problem is.
9 个评论
Adam Danz
2021-3-31
Do you mean from another column of the same table?
T = table(datetime(2020,03,01)-days(0:5)',randi(50,6,1), randi(10,6,1))
idx = T.Var1 <= datetime(2020,2,28) & T.Var1 >datetime(2020,2,26)
T.Var2(idx)
T(idx,[2:end])
更多回答(1 个)
DGM
2021-3-31
编辑:DGM
2021-3-31
This is not a real answer, but my preference for very large files. I occasionally have to deal with csv files that are several million lines long. Reading all that into memory and trying to parse date strings is expensive. I find that it's much faster to split the file externally and then only deal with the chunk that's necessary.
You'll need to be familiar with how your file is formatted, but this is an example. My lines start with a date, so:
startdate='02/28/2021'; % include this date
stopdate='03/01/2021'; % exclude this date; set to '' to read up to last line
logfile='/path/to/my/giant/logfile.log';
tempfile='/dev/shm/tempms.log'; % don't need to touch the disk
delimiter=',';
% prepare temporary file
[~,b]=system(['wc -l ' logfile ' | cut -d '' '' -f 1']);
totallc=str2double(b);
[~,b]=system(['grep -n ' startdate ' ' logfile ' 2>/dev/null | head -n 1 | cut -d '':'' -f 1']);
startline=str2double(b)-1;
system(['tail -n ' num2str(totallc-startline) ' ' logfile ' > ' tempfile]);
if ~isempty(stopdate)
[~,b]=system(['grep -n ' stopdate ' ' tempfile ' 2>/dev/null | head -n 1 | cut -d '':'' -f 1']);
stopline=str2double(b)-1;
system(['head -n ' num2str(stopline) ' ' tempfile ' | sponge ' tempfile]);
end
% now you can read the temp file instead of the whole log.
Of course, this is in bash, but the idea is the same in other environments. I could have made this neater by writing a bash script to do the job and just calling it from within my m-file. You don't have to do everything in Matlab.
4 个评论
Walter Roberson
2021-3-31
wc implies unix / linux, and if you have that then split can often be useful.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!