Inserting Date Time into code
3 次查看(过去 30 天)
显示 更早的评论
The following code uses continuous rainfall data and extracts events based on a certain criteria. Pevents.txt displays the extracted rainfall event and also at the end of the code the total number of events is given. These files are used:
20142018_2.txt (input rainfall data only)
Pevents.txt (extracted event)
I would like to add the date and Time to the code. In other words i would like to see from what day and time is the rainfall event being extracted from. Using this file:
20142018_1.txt(date,time and rainfall event)
function extract_events_onlyP(filename,dt)
P=load(filename);
% -----------------------------------------
% Parameters
% -----------------------------------------
P_tr_mm = 0.51; % rainfall threshold for the extraction of the event (mm)
P_tr_h = 4; % maximum length of non-rainfall period (hour)
No_pio = 1; % threshold for zero rainfall (mm)
% -----------------------------------------
% Separating the Rainfall Data into Independent Events
i=1; jj=1; k=1;
Pevents=[];
while i<length(P)-P_tr_h/dt
if P(i)>0
i_in=i;
ii=i_in+P_tr_h/dt-1;
while sum(P(ii-P_tr_h/dt+1:ii))>=No_pio
ii=ii+1;
if ii>length(P),break,end
end
i_fin=ii-1;
for iii=i_fin:-1:i_in
if P(iii)>0
i_fin=iii;
break
end
end
i=i_fin;
if sum(P(i_in:i_fin))>P_tr_mm
Pev=P(i_in:i_fin);
Pevents=[Pevents;NaN;k;Pev];
k=k+1;
end
end
i=i+1;
end
disp(['Total number of events: ',num2str(k-1)])
save Pevents.txt Pevents -ascii -double
回答(2 个)
Walter Roberson
2022-3-11
Use fileparts(filename) and take the second output. This will strip off any directory and extension. Now take the first 6 characters. datetime() with 'inputformat'.
Your filename gives the impression of having a year and an hour and minute but no day or month, so I do not see at the moment how to get date and time from the name?
Peter Perkins
2022-3-11
You need to read 20141018_1.txt into a timetable so you have the timestamps for each for of rainfall data. Every other line in that file is blank, and it appears to be UTF-16. Is that really what you have?
Also, it's usually the case that you don't want giant loops like that, and that finding regions of interest can be vectorized. With no comments in the code, I am not going to try to guess what defines your events, but with a clear explanation and an example, people might be able to simplify your code. Maybe not, but your code is pretty opaque, so hard to tell.
6 个评论
Walter Roberson
2022-3-20
You have a small number of possibilities at this point:
- give up
- be satisfied with the NaT and NaN
- be satisfied with the warning
- turn that particular warning off to hide the problem
- upgrade matlab versions, as the warning goes away around r2019b (which would probably automatically detect the utf16 as well)
- use fopen and fread to read the file as uint8, and pass it through native2unicode to get MATLAB char vector, that you then pass as the first parameter to textscan, with a carefully constructed format as the second parameter in order to parse the data.
- fread, native2unicode and then use text processing methods such as regexp() to extract the data data yourself instead of using textscan.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!