Issue using Mann-Kendall Test

Hello, not sure if anyone wwould be able to help me because I am using a specific function for my code. I am using the functions sktt.m and ktuab.m which are Mann-Kendall test functions with the first being one for a seasonal trend analysis.
I have used the same process for several other sets of data and they worked fine so I am unsure of what the issue is here:
clear
clc
fn='Wetland_water_level_new.xlsx';
FM1=table2timetable(readtable(fn,'Sheet','FM1'));
TFM1(:,1)=year(FM1.Var2);
TFM1(:,2)=month(FM1.Var2);
TFM1(:,3)=FM1.Var4;
datain=TFM1
wantplot=1;
startSeason=1;
alpha=.05;
T = table({'FM1';'FM2';'NM1';'GL1';'GL2';'BC1';'SB1'},{'N';'N';'N';'N';'N';'N',;'N'},{'V';'NV';'NV';'NV';'NV';'NV';'NV'},zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),'VariableNames',{'Station','Adjustment Y/N','Valid test & slope V/NV','taubsea','tausea','Sens','h','sig','sigAdj','Zs','Zmod','Ss','Sigmas','CIlower','CIupper'});
[T.taubsea(1) T.tausea(1) T.Sens(1) T.h(1) T.sig(1) T.sigAdj(1) T.Zs(1) T.Zmod(1) T.Ss(1) T.Sigmas(1) T.CIlower(1) T.CIupper(1)]= sktt(datain,alpha,wantplot,startSeason);
I have tried readjusting my code to not create the table and just getting the outputs of the function to see if it was my table indexing that was the issue but it is not as it gives me the same error wither way.
The error I keep getting is:
Index in position 1 exceeds array bounds.
Error in ktaub (line 216)
if b(1,1) > 1
Error in sktt (line 226)
[taub(ii) tau(ii) h(ii) sig(ii) Z(ii) S(ii) sigma sen(ii) n(ii) splot CIlower CIupper D(ii) Dall(ii) C3 nsigma] = ktaub([data(:,1) data(:,3)], alpha);
Error in wetlandwatertrendanalysis (line 63)
[T.taubsea(1) T.tausea(1) T.Sens(1) T.h(1) T.sig(1) T.sigAdj(1) T.Zs(1) T.Zmod(1) T.Ss(1) T.Sigmas(1) T.CIlower(1) T.CIupper(1)]= sktt(datain,alpha,wantplot,startSeason);
The original file I am working with is too big to attach so I just attached the saved variable file FM1. If someone could help me figure out what to fix that would be great thanks!

 Accepted Answer

You have NaT values in your timetable.
When I removed them with
FM1(isnat(FM1.Var2),:) = [];
the code ran to completion.

6 Comments

Thank you that worked for me! I am running it for multiple stations however and I followed the same process with your correction but for some reason I have 2 stations (FM2 and GL2) that wont work even with the fix and they give me the same error, any suggestions?
I simplified my code for ease of reading to just the two stations below:
%% Load the data
clear
clc
fn='Wetland_water_level_new.xlsx';
FM2GL2=readtable(fn,'Sheet','FM2 GL2');
%% Read from sheet into one timetable and remove NaT
FM21=table(data.Var2,data.Var3);
FM22=table(data.Var7,data.Var9);
FM2=table2timetable([FM21;FM22]);
FM2(isnat(FM2.Var1),:) = [];
GL21=table(data.Var16,data.Var17);
GL22=table(data.Var20,data.Var22);
GL2=table2timetable([GL21;GL22]);
GL2(isnat(GL2.Var1),:) = [];
%% Reformat for the function with the year, season number, and value
TFM2(:,1)=year(FM2.Var1);
TFM2(:,2)=month(FM2.Var1);
TFM2(:,3)=FM2.Var2;
TGL2(:,1)=year(GL2.Var1);
TGL2(:,2)=month(GL2.Var1);
TGL2(:,3)=GL2.Var2;
%% Apply function for trend analysis and read into a table
wantplot=1;
startSeason=1;
alpha=.05;
T = table({'FM1';'FM2';'NM1';'GL1';'GL2';'BC1';'SB1'},{'N';'N';'N';'N';'N';'N',;'N'},{'V';'NV';'NV';'NV';'NV';'NV';'NV'},zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),zeros(7,1),'VariableNames',{'Station','Adjustment Y/N','Valid test & slope V/NV','taubsea','tausea','Sens','h','sig','sigAdj','Zs','Zmod','Ss','Sigmas','CIlower','CIupper'});
[T.taubsea(2) T.tausea(2) T.Sens(2) T.h(2) T.sig(2) T.sigAdj(2) T.Zs(2) T.Zmod(2) T.Ss(2) T.Sigmas(2) T.CIlower(2) T.CIupper(2)]= sktt(TFM2,alpha,wantplot,startSeason);
[T.taubsea(5) T.tausea(5) T.Sens(5) T.h(5) T.sig(5) T.sigAdj(5) T.Zs(5) T.Zmod(5) T.Ss(5) T.Sigmas(5) T.CIlower(5) T.CIupper(5)]= sktt(TGL2,alpha,wantplot,startSeason);
disp(T)
Can you upload the data?
Oh yes sorry! I knew I forgot something!
I should be able to take a look later today, but let me tell you how I found the mistake before. I used the debugger. Specifically, I used
dbstop if error
and then ran your code. With the above command, your code will stop at exactly the line that give the error. Then it was a matter of a little back-tracing to see why the error occurred.
After the investigation, I will usually do
dbclear if error
or
dbclear all
to remove the breakpoints.
The debugger is a very powerful tool.
Okay great thank you very much!
This error is more subtle, and looks like your data does not obey an assumption the author made.
In sktt(), in these lines of code:
for ii = 1:NumOfSeasons
data = sorteds(sorteds(:,2)==ii,:);
your data has NumOfSeasons==4, and I believe the author is therefore expecting sorteds(:,2) to consist of the numbers [1,2,3,4]. But your data has [6,7,8,9]. So, when the code looks for data with sorteds(:,2)==1, it gets an empty array, and throws the error.

Sign in to comment.

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products

Release

R2021a

Asked:

JMG
on 4 Aug 2021

Commented:

on 5 Aug 2021

Community Treasure Hunt

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

Start Hunting!