%% Description file

%% Clear and close
clear;
close all;

%% Load data;
load('GaitVarOutcomes30april.mat')
Participants = fields(OutcomesAcc);

%% Settings;
counter = 1;
run = 1;
ParticipantNr = 0;
ConditionNames = {'TwoMWT','InsidePath','InsideStraight','Outside','Treadmill_Comfortable','Treadmill_Condition1','Treadmill_Condition2','Treadmill_Condition3','Treadmill_Condition4','Treadmill_Condition5','Treadmill_Condition6','Treadmill_Condition7','Treadmill_Condition8'};

%% Reorder data;
for i = 1: length(Participants);
    % Participant Nr;
    ParticipantNr = ParticipantNr + 1;
    
    % Group Nr;
    if contains(Participants(i),'Y');
        Group = 1;
    else
        Group = 2;
    end
    
    WalkingConditions = fields(OutcomesAcc.([char(Participants(i))]));
    
    for j = 1: length(WalkingConditions);
        
        if strcmp(char(WalkingConditions(j)),'Treadmill')
            
            SubConditions = fieldnames(OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]));
            
            for k = 1: length(SubConditions);
                
                NameTreadmill = [char(WalkingConditions(j)),'_',char(SubConditions(k))];
                ConditionNr = find(strcmp(ConditionNames, NameTreadmill));
                Parameters = fieldnames(OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]).([char(SubConditions(k))]));
                
                for l = 1: length(Parameters);
                    
                    Data(counter+1,1) = {([char(Participants(i))])};
                    Data(counter+1,2) = {ParticipantNr};
                    Data(counter+1,3) = {Group};
                    Data(counter+1,4) = {([char(NameTreadmill)])};
                    Data(counter+1,5) = {ConditionNr};
                    Data(counter+1,l+5) = {OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]).([char(SubConditions(k))]).([char(Parameters(l))])(1)};
                    
                end
                counter = counter+1;
                
            end
            
        else
            
            ConditionNr = find(strcmp(ConditionNames, WalkingConditions(j)));
            Parameters = fieldnames(OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]));
            
            for l = 1: length(Parameters);
                
                Data(counter+1,1) = {([char(Participants(i))])};
                Data(counter+1,2) = {ParticipantNr};
                Data(counter+1,3) = {Group};
                Data(counter+1,4) = {([char(WalkingConditions(j))])};
                Data(counter+1,5) = {ConditionNr};
                Data(counter+1,l+5) = {OutcomesAcc.([char(Participants(i))]).([char(WalkingConditions(j))]).([char(Parameters(l))])(1)};
                
            end
            
            counter = counter+1;
            
        end
        if run == 1;
            Data(1,1) = {'ParticipantCode'};
            Data(1,2) = {'ParticipantNr'};
            Data(1,3) = {'Group'};
            Data(1,4) = {'ConditionName'};
            Data(1,5) = {'ConditionNr'};
            Data(1,6:(size(Parameters,1)+5)) = Parameters'; % First row with variable names;
            run = run+1;
        end
        
    end
end

if ispc;
    xlswrite('GaitVariabilityOutcomes.xls', 'Data');
elseif ismac;
    filename = 'GaitVariabilityOutcomes.xlsx';
    writecell(Data,filename);
else
end