% This script adapts tracks from database to a new coordinate system and
% combines data from upstream and downstream cameras.
% Author: Joschka Wiegleb (2021)

%Title: Flow, force, behaviour: Assessment of a prototype hydraulic barrier for
%invasive fish
%Journal: Hydrobiologia
%Article authors: Joschka Wiegleb, Philipp E. Hirsch, Frank Seidel, Georg Rauter,
%Patricia Burkhardt-Holm
%Corresponding Author: Patricia Burkhardt-Holm, Program
%Man-Society-Environment, Department of Environmental Sciences, University of Basel, Vesalgasse 1, 4051 Basel, Switzerland, patricia.holm@unibas.ch



data=readtable('Filepath\DATABASE_tracks.xlsx')

treatments={'stromab112'; 'stromab122'; 'stromab131'; 'stromab132'; 'stromab133'; 'stromab221'; 'stromab231';
    'stromab232'; 'stromab233'; 'stromab321'; 'stromab311'; 'stromab331'; 'stromab332'; 'stromab333';
    'stromauf112'; 'stromauf122'; 'stromauf221'; 'stromauf231'; 'stromauf232'; 'stromauf311'; 'stromauf331';
    'stromauf332'; 'stromauf333'}

treatmentcodestromab={'112'; '122'; '131'; '132'; '133'; '221'; '231';
    '232'; '233'; '321'; '311'; '331'; '332'; '333'}

treatmentcodestromauf={'112'; '122'; '221'; '231';
    '232'; '311'; '331'; '332'; '333'};

dataa=table2cell(data)

%% separate marker names
for i=1:length(dataa)
    val=dataa(i,2);
    mtype=val{1,1}(1:2);
    mnumber=val{1,1}(4:end);
    treat=dataa(i,7);
    if treat{1,1}(1:7)=='stromab'
        pos=char('stromab');
    else pos=char('stromauf');
    end
    treatcode=treat{1,1}(end-2:end);
    ClipName=dataa(i,1);
    MarkerName=dataa(i,2);
    FrameNumber=cell2mat(dataa(i,3));
    X=cell2mat(dataa(i,4));
    Y=cell2mat(dataa(i,5));
    TreatmentL=dataa(i,6);
    Treatment=dataa(i,7);
    dataOrd(i,:)={i TreatmentL ClipName Treatment pos treatcode MarkerName mtype mnumber FrameNumber X Y};
    ProcessInPercent1of5=(i/length(dataa))*100
end


%%Safe new Database
dataOrdTab=cell2table(dataOrd,'VariableNames',{'Number','TreatmentLong', 'ClipName', 'Treatment','Position', 'TreatmentCode', 'MarkerName', 'mtype', 'mnumber', 'FrameNumber','X', 'Y'})
filepath='Filepath\DATABASE_tracks_reordered.xlsx'
writetable(dataOrdTab,filepath)


% filter orientation markers
%stromab
dataOrdTab.Position = categorical(dataOrdTab.Position);
idx = dataOrdTab.Position == 'stromab';
dataStromab = dataOrdTab(idx,:);

%stromauf
dataOrdTab.Position = categorical(dataOrdTab.Position);
idx = dataOrdTab.Position == 'stromauf';
dataStromauf = dataOrdTab(idx,:);

dataStromab.TreatmentCode = categorical(dataStromab.TreatmentCode);
dataStromauf.TreatmentCode = categorical(dataStromauf.TreatmentCode);
%% Fixpoints Stromab
for i=1:length(treatmentcodestromab)
    tr=treatmentcodestromab(i,1);
    char(tr);
    idx = dataStromab.TreatmentCode == tr;
    dataStromabi = dataOrdTab(idx,:);
    dataStromabi.mtype = categorical(dataStromabi.mtype);
    idx2 = dataStromabi.mtype == '33';
    dataStromab33 = dataStromabi(idx2,:);
    uniq=unique(dataStromab33.ClipName);
    dataStromab33.ClipName = categorical(dataStromab33.ClipName);
    for ii=1:length(uniq)
        idx3 = dataStromab33.ClipName == uniq(ii,1);
        compute = dataStromab33(idx3,:);
        mean33_X=mean(compute.X);
        mean33_Y=mean(compute.Y);
        Clip=uniq(ii,1);
        mean33(ii,:)={(Clip) mean33_X mean33_Y};
    end
    clearvars mean33_X mean33_Y Clip compute idx3;
    idx4 = dataStromabi.mtype == '44';
    dataStromab44 = dataStromabi(idx4,:);
    uniq=unique(dataStromab44.ClipName);
    dataStromab44.ClipName = categorical(dataStromab44.ClipName);
    for iii=1:length(uniq)
        idx4 = dataStromab44.ClipName == uniq(iii,1);
        compute = dataStromab44(idx4,:);
        mean44_X=mean(compute.X);
        mean44_Y=mean(compute.Y);
        Clip=uniq(iii,1);
        mean44(iii,:)={(Clip) mean44_X mean44_Y};
    end
    clearvars mean44_X mean44_Y Clip compute idx4; 
    comb=[mean33(:,1) mean33(:,2) mean33(:,3) mean44(:,2) mean44(:,3)];
    for v=1:length(uniq)
        dist=sqrt((mean33{v,2}-mean44{v,2})^2+(mean33{v,3}-mean44{v,3})^2); 
        distcoeff=24/dist; % Distance Coefficient cm/pixel
        a=mean33{v,2}-mean44{v,2};
        c=dist;
        vectangle33to4=acosd(a/c);
        if a<0 
            vectangle33to44=180-vectangle33to4;
        else vectangle33to44=vectangle33to4;
        end  
        vectLength44=sqrt((mean44{v,2}^2)+(mean44{v,3}^2))*distcoeff;
        vectLength33=sqrt((mean33{v,2}^2)+(mean33{v,3}^2))*distcoeff;
        if mean44{v,2}>mean44{v,3} 
            vectangle44=asind(mean44{v,3}/mean44{v,2});
        else vectangle44=acosd(mean44{v,2}/mean44{v,3});
        end
        if mean33{v,2}>mean33{v,3}
            vectangle33=asind(mean33{v,3}/mean33{v,2});
        else vectangle33=acosd(mean33{v,2}/mean33{v,3});
        end
        uniqq=uniq(v,:);
        distpix(v,:)={v uniqq vectangle33to44 dist distcoeff vectLength33 vectLength44 vectangle33 vectangle44 mean33{v,2} mean33{v,3} mean44{v,2} mean44{v,3}}; %hier muss noch der Winken zwisdchen 33-44 und dem pixel-Koordinatensystem berechnet werden.
        clearvars dist distcoeff a c vectangle33to4 vectangle33to44 vectLength44 vectLength33 vectangle44 vectangle33 uniqq; %this line inserted
    end
     distscale=cell2table(distpix, 'VariableNames',{'Number', 'VideoName','AngleBeta33to44', 'DistancePixels', 'DistanceCoefficient (cm/Pixel)', 'VectorLength(cm) 33', 'VectorLength(cm) 44', 'VectorAngle(?) 33', 'VectorAngle(?) 44', 'Mean33_X_Pix','Mean33_Y_Pix', 'Mean44_X_Pix', 'Mean44_Y_Pix'});
     fixpoints_stromab(i,:)={i treatmentcodestromab(i,1) distscale};
     %NOTE: Distances and Angles of 33 and 44 are computed from 0,0 Coords
     %of the video frame coordinate system.
     ProcessInPercent2of5=i/length(treatmentcodestromab)*100
     clearvars distscale uniq mean33 mean44 distpix;
end


    
%
%% Adapt Coordinate systems Stromab
dataStromab.TreatmentCode=categorical(dataStromab.TreatmentCode); 
dataStromab.ClipName=categorical(dataStromab.ClipName);


for i=1:length(fixpoints_stromab)
    subdata=fixpoints_stromab{i,3};
    for ii=1:height(fixpoints_stromab{i,3}) 
        tr=char(fixpoints_stromab{i,2});
        idxTreat = dataStromab.TreatmentCode == tr; 
        filtdata = dataStromab(idxTreat,:); 
        q=table2array(fixpoints_stromab{i,3}(ii,2));
        idxVid = filtdata.ClipName == q;
        filtdata2=filtdata(idxVid,:);
        for iii=1:height(filtdata2)
            CmeasPointsPix=sqrt(filtdata2.X(iii)^2+filtdata2.Y(iii)^2); 
            takecoeff=subdata; 
            Coeff=subdata.(5)(ii); 
            CmeasPointsCm=CmeasPointsPix*Coeff; 
            BetaPoints=acosd(filtdata2.X(iii)/CmeasPointsPix); 
            Dist_44ToPoint_X=filtdata2.X(iii)-subdata.Mean44_X_Pix(ii);
            Dist_44ToPoint_Y=filtdata2.Y(iii)-subdata.Mean44_Y_Pix(ii);
            C_44ToPoints_Pix=sqrt(Dist_44ToPoint_X^2+Dist_44ToPoint_Y^2);
            C_44ToPoints_cm=C_44ToPoints_Pix*Coeff; 
            
            BetragX=sqrt(Dist_44ToPoint_X^2); 
            BetragY=sqrt(Dist_44ToPoint_Y^2);
            if BetragX>BetragY 
                AngleBetaPoin=asind(BetragY/C_44ToPoints_Pix); 
            else if BetragX<BetragY
                    AngleBetaPoin=acosd(BetragX/C_44ToPoints_Pix);
                else Status='ERROR Line 165'
                end
            end
            if Dist_44ToPoint_Y>0&&Dist_44ToPoint_X>0
                AngleBetaPoint=AngleBetaPoin;
            else if Dist_44ToPoint_Y<0&&Dist_44ToPoint_X>0
                    AngleBetaPoint=360-AngleBetaPoin; 
                else if Dist_44ToPoint_Y<0&&Dist_44ToPoint_X<0
                        AngleBetaPoint=180+AngleBetaPoin;
                    else if Dist_44ToPoint_Y>0&&Dist_44ToPoint_X<0
                            AngleBetaPoint=180-AngleBetaPoin;  
                        else Status='ERROR Line 176';
                        end
                    end
                end
            end
            AngleBeta33to44=subdata.(3)(ii);
            if AngleBetaPoint>AngleBeta33to44 
                AngleNewCoordSysPoint=AngleBetaPoint-AngleBeta33to44+90; 
            else if AngleBetaPoint<AngleBeta33to44
                    AngleNewCoordSysPoint=90-(AngleBeta33to44-AngleBetaPoint);
                else Status='ERROR Line 186';
                end
            end
            if AngleNewCoordSysPoint<90 
                AngleNewCoordBeta=90-AngleNewCoordSysPoint;
            else if AngleNewCoordSysPoint>90&&AngleNewCoordSysPoint<180
                    AngleNewCoordBeta=AngleNewCoordSysPoint-90;
                else if AngleNewCoordSysPoint>180&&AngleNewCoordSysPoint<270
                        AngleNewCoordBeta=270-AngleNewCoordSysPoint;
                    else if AngleNewCoordSysPoint>270
                            AngleNewCoordBeta=90-(360-AngleNewCoordSysPoint);
                        end
                    end
                end
            end
            newY_cm=newY_cm1; 
            newX_cm=newX_cm1; 
            
            filtdata2.(13)(iii)=C_44ToPoints_Pix; 
            filtdata2.(14)(iii)=C_44ToPoints_cm;
            filtdata2.(15)(iii)=AngleNewCoordSysPoint;
            filtdata2.(16)(iii)=Coeff;
            filtdata2.(17)(iii)=newX_cm;
            filtdata2.(18)(iii)=newY_cm;
            clearvars BetragX BetragY BetraDist44Point_X BetraDist44Point_Y CmeasPointsPix takecoeff Coeff AngleNewCoordBeta AngleNewCoordSysPoint CmeasPointsCm BetaPoints Dist_44ToPoint_X Dist_44ToPoint_Y C_44ToPoints_Pix C_44ToPoints_cm AngleBetaPoin newX_cm newY_cm newY_cm1 newX_cm1 Status AngleBeta33to44;
        end
        filtdataEnd_22(ii,:)={ii filtdata2};
        clearvars tr idxTreat q filtdata idxVid filtdata2;
    end
    filtdataEnd_1=vertcat(filtdataEnd_22{1:end,2});
    filtdataEnd_1.Properties.VariableNames{'Var13'}= 'Distance44toPoint_Pix';
    filtdataEnd_1.Properties.VariableNames{'Var14'}= 'Distance44toPoint_cm';
    filtdataEnd_1.Properties.VariableNames{'Var15'}= 'AngleNewCoordSysPoint_degrees';
    filtdataEnd_1.Properties.VariableNames{'Var16'}= 'DistCorr_Coeff_(cm/Pixel)';
    filtdataEnd_1.Properties.VariableNames{'Var17'}= 'newX_cm';
    filtdataEnd_1.Properties.VariableNames{'Var18'}= 'newY_cm';
    filtdataEnd_11(i,:)={i filtdataEnd_1};
    clearvars subdata filtdataEnd_22;
    clearvars filtdataEnd_1;
    ProcessInPercent3of5=i/length(fixpoints_stromab)*100
end

filtdataEnd=vertcat(filtdataEnd_11{1:end,2});
path='Filepath\TrackingData_CoordsAdapted_stromab.xlsx'

writetable(filtdataEnd,path)   
clearvars filtdataEnd_11 path

%% Fixpoints Stromauf
for i=1:length(treatmentcodestromauf)
    tr=treatmentcodestromauf(i,1);
    char(tr);
    idx = dataStromauf.TreatmentCode == tr;
    dataStromaufi = dataStromauf(idx,:);
    dataStromaufi.mtype = categorical(dataStromaufi.mtype);
    idx2 = dataStromaufi.mtype == '33';
    dataStromauf33 = dataStromaufi(idx2,:);
    uniq=unique(dataStromauf33.ClipName);
    dataStromauf33.ClipName = categorical(dataStromauf33.ClipName);
    for ii=1:length(uniq)
        idx3 = dataStromauf33.ClipName == uniq(ii,1);
        compute = dataStromauf33(idx3,:);
        mean33_X=mean(compute.X);
        mean33_Y=mean(compute.Y);
        Clip=uniq(ii,1);
        mean33(ii,:)={(Clip) mean33_X mean33_Y};
    end
    clearvars mean33_X mean33_Y Clip compute idx3;
    idx4 = dataStromaufi.mtype == '44';
    dataStromauf44 = dataStromaufi(idx4,:);
    uniq=unique(dataStromauf44.ClipName);
    dataStromauf44.ClipName = categorical(dataStromauf44.ClipName);
    for iii=1:length(uniq)
        idx4 = dataStromauf44.ClipName == uniq(iii,1);
        compute = dataStromauf44(idx4,:);
        mean44_X=mean(compute.X);
        mean44_Y=mean(compute.Y);
        Clip=uniq(iii,1);
        mean44(iii,:)={(Clip) mean44_X mean44_Y};
    end
    clearvars mean44_X mean44_Y Clip compute idx4; 
    comb=[mean33(:,1) mean33(:,2) mean33(:,3) mean44(:,2) mean44(:,3)]; 
    for v=1:length(uniq)
        dist=sqrt((mean33{v,2}-mean44{v,2})^2+(mean33{v,3}-mean44{v,3})^2); 
        distcoeff=13.5/dist; 
        a=mean33{v,2}-mean44{v,2}
        c=dist
        vectangle33to4=acosd(a/c);
        if a<0 
            vectangle33to44=180-vectangle33to4;
        else vectangle33to44=vectangle33to4;
        end  
        vectLength44=sqrt((mean44{v,2}^2)+(mean44{v,3}^2))*distcoeff;
        vectLength33=sqrt((mean33{v,2}^2)+(mean33{v,3}^2))*distcoeff;
        if mean44{v,2}>mean44{v,3} 
            vectangle44=asind(mean44{v,3}/mean44{v,2});
        else vectangle44=acosd(mean44{v,2}/mean44{v,3});
        end
        if mean33{v,2}>mean33{v,3} 
            vectangle33=asind(mean33{v,3}/mean33{v,2});
        else vectangle33=acosd(mean33{v,2}/mean33{v,3});
        end
        uniqq=uniq(v,:)
        distpix(v,:)={v uniqq vectangle33to44 dist distcoeff vectLength33 vectLength44 vectangle33 vectangle44 mean33{v,2} mean33{v,3} mean44{v,2} mean44{v,3}}; %hier muss noch der Winken zwisdchen 33-44 und dem pixel-Koordinatensystem berechnet werden.
        clearvars dist distcoeff a c vectangle33to4 vectangle33to44 vectLength44 vectLength33 vectangle44 vectangle33 uniqq; %this line inserted
    end
     distscale=cell2table(distpix, 'VariableNames',{'Number', 'VideoName','AngleBeta33to44', 'DistancePixels', 'DistanceCoefficient (cm/Pixel)', 'VectorLength(cm) 33', 'VectorLength(cm) 44', 'VectorAngle(?) 33', 'VectorAngle(?) 44', 'Mean33_X_Pix','Mean33_Y_Pix', 'Mean44_X_Pix', 'Mean44_Y_Pix'});
     fixpoints_stromauf(i,:)={i treatmentcodestromauf(i,1) distscale};
     ProcessInPercent4of5=i/length(treatmentcodestromauf)*100
     clearvars distscale uniq mean33 mean44 distpix;
end

%% Adapt Coordinate systems Stromauf
dataStromauf.TreatmentCode=categorical(dataStromauf.TreatmentCode);
dataStromauf.ClipName=categorical(dataStromauf.ClipName);


for i=1:length(fixpoints_stromauf) 
    subdata=fixpoints_stromauf{i,3};
    for ii=1:height(fixpoints_stromauf{i,3})  
        tr=char(fixpoints_stromauf{i,2});
        idxTreat = dataStromauf.TreatmentCode == tr;
        filtdata = dataStromauf(idxTreat,:);
        q=table2array(fixpoints_stromauf{i,3}(ii,2));
        idxVid = filtdata.ClipName == q;
        filtdata2=filtdata(idxVid,:);  
        for iii=1:height(filtdata2)
            CmeasPointsPix=sqrt(filtdata2.X(iii)^2+filtdata2.Y(iii)^2); 
            takecoeff=subdata; 
            Coeff=subdata.(5)(ii); 
            CmeasPointsCm=CmeasPointsPix*Coeff; 
            BetaPoints=acosd(filtdata2.X(iii)/CmeasPointsPix); 
            Dist_44ToPoint_X=filtdata2.X(iii)-subdata.Mean44_X_Pix(ii);
            Dist_44ToPoint_Y=filtdata2.Y(iii)-subdata.Mean44_Y_Pix(ii);
            C_44ToPoints_Pix=sqrt(Dist_44ToPoint_X^2+Dist_44ToPoint_Y^2); 
            C_44ToPoints_cm=C_44ToPoints_Pix*Coeff; 
            
            BetragX=sqrt(Dist_44ToPoint_X^2); 
            BetragY=sqrt(Dist_44ToPoint_Y^2);
            if BetragX>BetragY 
                AngleBetaPoin=asind(BetragY/C_44ToPoints_Pix); 
            else if BetragX<BetragY
                    AngleBetaPoin=acosd(BetragX/C_44ToPoints_Pix); 
                else Status='ERROR Line 331'
                end
            end
            if Dist_44ToPoint_Y>0&&Dist_44ToPoint_X>0 
                AngleBetaPoint=AngleBetaPoin;
            else if Dist_44ToPoint_Y<0&&Dist_44ToPoint_X>0
                    AngleBetaPoint=360-AngleBetaPoin;
                else if Dist_44ToPoint_Y<0&&Dist_44ToPoint_X<0
                        AngleBetaPoint=180+AngleBetaPoin;
                    else if Dist_44ToPoint_Y>0&&Dist_44ToPoint_X<0
                            AngleBetaPoint=180-AngleBetaPoin;  
                        else Status='ERROR Line 342';
                        end
                    end
                end
            end
            AngleBeta33to44=subdata.(3)(ii);
            if AngleBetaPoint>AngleBeta33to44 
                AngleNewCoordSysPoint=AngleBetaPoint-AngleBeta33to44+90; 
            else if AngleBetaPoint<AngleBeta33to44
                    AngleNewCoordSysPoint=90-(AngleBeta33to44-AngleBetaPoint);
                else Status='ERROR Line 352';
                end
            end
            
            if AngleNewCoordSysPoint<90 
                AngleNewCoordBeta=90-AngleNewCoordSysPoint;
            else if AngleNewCoordSysPoint>90&&AngleNewCoordSysPoint<180
                    AngleNewCoordBeta=AngleNewCoordSysPoint-90;
                else if AngleNewCoordSysPoint>180&&AngleNewCoordSysPoint<270
                        AngleNewCoordBeta=270-AngleNewCoordSysPoint;
                    else if AngleNewCoordSysPoint>270
                            AngleNewCoordBeta=90-(360-AngleNewCoordSysPoint);
                        end
                    end
                end
            end
            
            newY_cm1=Dist_44ToPoint_Y*Coeff;
            newX_cm1=Dist_44ToPoint_X*Coeff;
            newY_cm=newY_cm1; 
            newX_cm=newX_cm1; 
            
            
            newY_cm_stromauf=newY_cm+5.5;
            newX_cm_stromauf=newX_cm+64.58;
            filtdata2.(13)(iii)=C_44ToPoints_Pix; 
            filtdata2.(14)(iii)=C_44ToPoints_cm;
            filtdata2.(15)(iii)=AngleNewCoordSysPoint;
            filtdata2.(16)(iii)=Coeff;
            filtdata2.(17)(iii)=newX_cm_stromauf;
            filtdata2.(18)(iii)=newY_cm_stromauf;
            clearvars BetragX BetragY BetraDist44Point_X BetraDist44Point_Y CmeasPointsPix takecoeff Coeff AngleNewCoordBeta AngleNewCoordSysPoint CmeasPointsCm BetaPoints Dist_44ToPoint_X Dist_44ToPoint_Y C_44ToPoints_Pix C_44ToPoints_cm AngleBetaPoin newX_cm newY_cm newY_cm1 newX_cm1 Status AngleBeta33to44;
        end
        filtdataEnd_22(ii,:)={ii filtdata2};
        clearvars tr idxTreat q filtdata idxVid filtdata2;
    end
    filtdataEnd_1=vertcat(filtdataEnd_22{1:end,2});
    filtdataEnd_1.Properties.VariableNames{'Var13'}= 'Distance44toPoint_Pix';
    filtdataEnd_1.Properties.VariableNames{'Var14'}= 'Distance44toPoint_cm';
    filtdataEnd_1.Properties.VariableNames{'Var15'}= 'AngleNewCoordSysPoint_degrees';
    filtdataEnd_1.Properties.VariableNames{'Var16'}= 'DistCorr_Coeff_(cm/Pixel)';
    filtdataEnd_1.Properties.VariableNames{'Var17'}= 'newX_cm';
    filtdataEnd_1.Properties.VariableNames{'Var18'}= 'newY_cm';
    clearvars subdata filtdataEnd_22; 
    filtdataEnd_11(i,:)={i filtdataEnd_1};
    clearvars filtdataEnd_1
    ProcessInPercent5of5=i/length(fixpoints_stromauf)*100
end
%%
filtdataEndStromauf=vertcat(filtdataEnd_11{1:end,2});

rows112=filtdataEndStromauf.TreatmentCode=='112';
rows122=filtdataEndStromauf.TreatmentCode=='122';

rows221=filtdataEndStromauf.TreatmentCode=='221';
rows231=filtdataEndStromauf.TreatmentCode=='231';
rows232=filtdataEndStromauf.TreatmentCode=='232';

rows311=filtdataEndStromauf.TreatmentCode=='311';
rows331=filtdataEndStromauf.TreatmentCode=='331';
rows332=filtdataEndStromauf.TreatmentCode=='332';
rows333=filtdataEndStromauf.TreatmentCode=='333';

filt112=filtdataEndStromauf(rows112,:);
filt122=filtdataEndStromauf(rows122,:);

filt221=filtdataEndStromauf(rows221,:);
filt231=filtdataEndStromauf(rows231,:);
filt232=filtdataEndStromauf(rows232,:);

filt311=filtdataEndStromauf(rows311,:);
filt331=filtdataEndStromauf(rows331,:);
filt332=filtdataEndStromauf(rows332,:);
filt333=filtdataEndStromauf(rows333,:);

filt112ii = filt112.newX_cm >= 43.00;
filt112i = filt112(filt112ii,:);

filt311ii = filt311.newX_cm >= 43.00;
filt311i = filt311(filt311ii,:);

filt122ii = filt122.newX_cm >= 40.00;
filt122i = filt122(filt122ii,:);

filt221ii = filt221.newX_cm >= 40.00;
filt221i = filt221(filt221ii,:);

filt231ii = filt231.newX_cm >= 40.00;
filt231i = filt231(filt231ii,:);

filt232ii = filt232.newX_cm >= 40.00;
filt232i = filt232(filt232ii,:);

filt331ii = filt331.newX_cm >= 40.00;
filt331i = filt331(filt331ii,:);

filt332ii = filt332.newX_cm >= 40.00;
filt332i = filt332(filt332ii,:);

filt333ii = filt333.newX_cm >= 40.00;
filt333i = filt333(filt333ii,:);

grouping={filt112i;filt122i;filt221i;filt231i;filt232i;filt311i;filt331i;filt332i;filt333i};
filtdataEndStromaufI=vertcat(grouping{1:end,1});


path='Filepath\TrackingData_CoordsAdapted_stromauf.xlsx'
writetable(filtdataEndStromaufI,path)

clearvars filtdataEnd_11 path

%% Combine Stromab and Stromauf

combStrAb_Auf1={filtdataEnd; filtdataEndStromaufI};
combStrAb_Auf=vertcat(combStrAb_Auf1{1,1}, combStrAb_Auf1{2,1});

path='Filepath\TrackingData_CoordsAdapted_BothLocations.xlsx'
writetable(combStrAb_Auf,path)

'Script completed! Congratulations!'
