Inserting Date Time into code

조회 수: 4 (최근 30일)
Queena Edwards
Queena Edwards 2022년 3월 11일
댓글: Walter Roberson 2022년 3월 20일
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
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?
  댓글 수: 1
Queena Edwards
Queena Edwards 2022년 3월 11일
I'm not sure what you mean. I've attached the data to the original code

댓글을 달려면 로그인하십시오.


Peter Perkins
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
Queena Edwards
Queena Edwards 2022년 3월 20일
I took out the "Encoding" and "UTF-16LE". It Runs but I'm getting Nat and Nan
NaT NaN
NaT NaN
NaT NaN
NaT NaN
NaT NaN
NaT NaN
NaT NaN
NaT NaN
Walter Roberson
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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by