I'm having trouble building the time series for each year separately using loops
1 view (last 30 days)
Show older comments
I'm having a problem with the year 1965 when I'm using loops to build each year's time series (in hours) separately
my field_day in 1965 year
in 1965 year field_day(i) = 8784
It should be 8760(365*24) but he changed to 8784(366*24) Would like to ask where is the problem, or is there a better way to build a year-by-year time series (in hours)
clear all ; clc ; clf ;
set(gcf,'color','w')
%% load data
filehtm = dir('moving_windown_test.xlsx') ;
for ii = 1 : length(filehtm);
filehtm(ii).name
[num,str,xlsdata] = xlsread(filehtm(ii).name) ; %num數值 str字串
end
time = num(:,1) ;
tide_detrend = num(:,2) ./ 1000 ;
tide = tide_detrend ;
tide_raw = tide_detrend;
%% set time
YYYY = fix(time/1000000) ;
MM = mod(fix(time/10000),100) ;
DD = mod(fix(time/100),100) ;
HH = mod(time,100) ;
tt = datenum(YYYY,MM,DD,HH,0,0) ;
%% remove outliner
for k = 1:1:5
yyyy(k) = 1960 +k;
t1 = datenum( yyyy(k),1,1) : 1/24 : datenum( yyyy(k)+1,1,1);
t1(end)=[];
%% data(依照原始數據不同)
year = YYYY;
index = find(year == yyyy(k));
t = tt(index); % movingwindown 時間
Q1(k) = prctile(tide(index), 25) ;
Q3(k) = prctile(tide(index), 75);
IQR(k) = Q3(k) - Q1(k) ;
upper(k) = Q3(k) + 1.5*IQR(k)
lower(k) = Q1(k) - 1.5*IQR(k)
tide_outline = tide(index);
raw_tide = tide(index);
tide_outline(tide_outline > upper(k) | tide_outline < lower(k)) = nan;
for j = 1 : length(t1);
if (isempty(find(t==t1(j))));
r_tide(j) = nan ;
else
r_tide(j) = tide_outline(find(t==t1(j))) ;
end
end
%% this is my problem
for i = 1 : length(t1) ;
field_day(i) = (yyyy(k)+(i - 1) * 60 * 60 / 86400/365) ;
end
plot(field_day,r_tide);hold on % rawdata
end
3 Comments
peter huang
on 20 Feb 2022
For the variable field_day, I want to create a time series for a full year (in hours), leap year (366*24=8784), and normal year (365*24=8760)
Simon Chan
on 21 Feb 2022
You may use function timeshift to generate:
Tseries = dateshift(datetime(1961,1,1,0,0,0),'start','hour',0:8759);
Tseries(1:5)
Tseries(end-4:end)
Tseries = dateshift(datetime(1964,1,1,0,0,0),'start','hour',0:8783);
Tseries(1:5)
Tseries(end-4:end)
Answers (1)
Seth Furman
on 28 Feb 2022
I'm not clear on the question being asked, but here's a simpler way to import the data into a timetable.
fname = "https://www.mathworks.com/matlabcentral/answers/uploaded_files/899805/moving_windown_test.xlsx";
opts = detectImportOptions(fname);
opts = setvaropts(opts, 1, "Type", "string");
t = readtable(fname, opts)
t.Var1 = datetime(t.Var1, "InputFormat", "uuuuMMddHH")
tt = table2timetable(t)
unique(dateshift)
0 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!