i have loop problem

6 visualizaciones (últimos 30 días)
peter huang
peter huang el 7 de En. de 2022
Respondida: Voss el 7 de En. de 2022
i have 5 year of data with water level and time
i want to remove outlines for each year here is how i remove outlines :
i would like to know how to remove the outlines for each year separaetely using the loop way or other better way
code:
%%load file
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 ;
%% 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) ;
t1 = datenum(1961,1,1) : 1/24 : datenum(1966,1,1) ;
t1(end)=[]
%% how to remove outlines
Q1 = prctile(tide, 25)
Q3 = prctile(tide, 75)
IQR = Q3 - Q1
upper = Q3 + 1.5*IQR
lower = Q1 - 1.5*IQR
tide(tide > upper | tide<lower) = nan ;

Respuestas (1)

Voss
Voss el 7 de En. de 2022
You want to remove outliers by year separately, but it's not clear whether each file corresponds to one year of data or whether the code would need to read all the files and then parse the data by year and operate on each year separately. Your code reads (up to) one file (the dir() call will only ever return at most one file), but then there is a loop to read multiple files, so I'll assume you actually have multiple files with data and that there is not necessarily a one-to-one correspondence between years and files. In that case, you can accumulate all the data together and then calculate the outliers in each year separately. Something like this:
% collect time and tide data from the xlsx files in the current directory:
filehtm = dir(fullfile(pwd(),'*.xlsx'));
time = [];
tide = [];
for ii = 1:numel(filehtm)
temp = xlsread(filehtm(ii).name);
time = [time; temp(:,1)];
tide = [tide; temp(:,2)/1000];
end
% go through each year spanned by time and set the outlier tide data to NaN:
YYYY = fix(time/1000000);
uY = unique(YYYY);
for ii = 1:numel(uY)
idx = YYYY == uY(ii);
temp = tide(idx);
Q1 = prctile(temp,25);
Q3 = prctile(temp,75);
IQR = Q3 - Q1;
upper = Q3 + 1.5*IQR;
lower = Q1 - 1.5*IQR;
temp(temp > upper | temp < lower) = NaN;
tide(idx) = temp;
end

Categorías

Más información sobre Oceanography and Hydrology en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by