I'm having trouble building the time series for each year separately using loops
Afficher commentaires plus anciens
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 commentaires
Seems the number of data for each year are different.
And for year 1965, it is not equals to what you are expected.
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/899805/moving_windown_test.xlsx');
T.Var1 = datetime(datenum(string(T.Var1),'yyyyMMddhh'),'ConvertFrom','datenum','Format','yyyyMMddhh');
[yearnum,~,index] = unique(year(T.Var1));
count = zeros(length(yearnum),1);
for k = 1:length(yearnum)
count(k,1) = length(T.Var1(index==k));
end
table(yearnum,count)
peter huang
le 20 Fév 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)
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)
Réponses (1)
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)
Catégories
En savoir plus sur Time Series Events dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!