# I'm having trouble building the time series for each year separately using loops

1 view (last 30 days)
peter huang on 19 Feb 2022
Answered: Seth Furman on 28 Feb 2022
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 CommentsShow 1 older commentHide 1 older comment
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)
ans = 1×5 datetime array
01-Jan-1961 00:00:00 01-Jan-1961 01:00:00 01-Jan-1961 02:00:00 01-Jan-1961 03:00:00 01-Jan-1961 04:00:00
Tseries(end-4:end)
ans = 1×5 datetime array
31-Dec-1961 19:00:00 31-Dec-1961 20:00:00 31-Dec-1961 21:00:00 31-Dec-1961 22:00:00 31-Dec-1961 23:00:00
Tseries = dateshift(datetime(1964,1,1,0,0,0),'start','hour',0:8783);
Tseries(1:5)
ans = 1×5 datetime array
01-Jan-1964 00:00:00 01-Jan-1964 01:00:00 01-Jan-1964 02:00:00 01-Jan-1964 03:00:00 01-Jan-1964 04:00:00
Tseries(end-4:end)
ans = 1×5 datetime array
31-Dec-1964 19:00:00 31-Dec-1964 20:00:00 31-Dec-1964 21:00:00 31-Dec-1964 22:00:00 31-Dec-1964 23:00:00

Sign in to comment.

### 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 = 43560×2 table
Var1 Var2 ____________ ____ "1961010100" -456 "1961010101" -456 "1961010102" -566 "1961010103" -656 "1961010104" -676 "1961010105" -676 "1961010106" -616 "1961010107" -456 "1961010108" -136 "1961010109" 45 "1961010110" 105 "1961010111" 146 "1961010112" 95 "1961010113" 25 "1961010114" -15 "1961010115" -66
t.Var1 = datetime(t.Var1, "InputFormat", "uuuuMMddHH")
t = 43560×2 table
Var1 Var2 ____________________ ____ 01-Jan-1961 00:00:00 -456 01-Jan-1961 01:00:00 -456 01-Jan-1961 02:00:00 -566 01-Jan-1961 03:00:00 -656 01-Jan-1961 04:00:00 -676 01-Jan-1961 05:00:00 -676 01-Jan-1961 06:00:00 -616 01-Jan-1961 07:00:00 -456 01-Jan-1961 08:00:00 -136 01-Jan-1961 09:00:00 45 01-Jan-1961 10:00:00 105 01-Jan-1961 11:00:00 146 01-Jan-1961 12:00:00 95 01-Jan-1961 13:00:00 25 01-Jan-1961 14:00:00 -15 01-Jan-1961 15:00:00 -66
tt = table2timetable(t)
tt = 43560×1 timetable
Var1 Var2 ____________________ ____ 01-Jan-1961 00:00:00 -456 01-Jan-1961 01:00:00 -456 01-Jan-1961 02:00:00 -566 01-Jan-1961 03:00:00 -656 01-Jan-1961 04:00:00 -676 01-Jan-1961 05:00:00 -676 01-Jan-1961 06:00:00 -616 01-Jan-1961 07:00:00 -456 01-Jan-1961 08:00:00 -136 01-Jan-1961 09:00:00 45 01-Jan-1961 10:00:00 105 01-Jan-1961 11:00:00 146 01-Jan-1961 12:00:00 95 01-Jan-1961 13:00:00 25 01-Jan-1961 14:00:00 -15 01-Jan-1961 15:00:00 -66
unique(dateshift)
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on MATLAB 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!