function [savePath]=readAllPos(pathToFile, subject,number,windowEMG,windowEEG,shiftEMG,shiftEEG,maxFile,...
    threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause,noLFsamples,EMGChannels,noSynergies)
% readAllPos reads the data for given subject and run and saves it with the
% according parameters; savePath is returned
% if the file exists, nothing is done
    %fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now')));
    savePath=strcat(pathToFile,...
        sprintf('../matlabData/%s%i%imsWindowEMG%isWindowEEG%imsShiftEMG%imsShiftEEGFreq%ito%iPause%ipBurg%inoSyn%iPos.mat',...
        subject,number,windowEMG*1000,windowEEG,shiftEMG*1000,shiftEEG*1000,minEEGFreq,maxEEGFreq,pause,pburgOrder,noSynergies));

    %only create file if it doesn't exist yet
    if exist(savePath, 'file') ~= 2
        fprintf(strcat(savePath,' not existing; creating\n'));
        trainingDataEEGcell=cell(maxFile,1);
        trainingDataEEGlfCell=cell(maxFile,1);
        trainingDataEMGcell=cell(maxFile,1);
        classesCell=cell(maxFile,1);
        kin=cell([maxFile,1]);

        % accumulate preprocessed data in cells
        for i=1:maxFile
            [sig, stat, params] = load_bcidat(strcat(pathToFile,...
                sprintf('%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,...
                number,subject,number,i)));
            tmpKin=csvread(strcat(pathToFile,...
                sprintf('kin/%s_B1S00%iR0%i.txt',subject,number,i)),1,0);
            [trainingDataEEGcell{i},trainingDataEEGlfCell{i},...
                trainingDataEMGcell{i},kin{i}]=generateTrainingDataPos(sig,...
                tmpKin(:,1:4),windowEMG,windowEEG,shiftEMG,shiftEEG,params,pburgOrder,...
                minEEGFreq,maxEEGFreq,noLFsamples,EMGChannels);
            classesCell{i}=stat.StimulusCode;
            %fprintf('%ith file processed\n',i)
        end
        bci_sf=params.SamplingRate.NumericValue;

        clear sig

        % reformat to matrices
        trainingDataEEG=cell2mat(trainingDataEEGcell);
        trainingDataEEGlf=cell2mat(trainingDataEEGlfCell);
        trainingDataEMG=cell2mat(trainingDataEMGcell);
        classesMat=cell2mat(classesCell);
        kinMat=cell2mat(kin);
        clear trainingDataEEGcell trainingDataEMGcell classesCell kin

        % reclassification according to EMG
        classificationWithPause=classifyAccordingToEMG(trainingDataEMG,...
            classesMat,shiftEMG,bci_sf,threshold,pause);
        clear classesMat

        % Smoother classifcation
        smoothClassificationEMG=zeros(size(classificationWithPause));
        for i=1:size(classificationWithPause,1)
            smoothClassificationEMG(i)=round(mode(classificationWithPause(...
                max(i-fix(bci_sf/1000),1):min(i+fix(bci_sf/1000),end))));
        end

        clear classificationWithPause

        %adjust to length of EEG data
        smoothClassificationEEG=zeros([size(trainingDataEEG,1),1]);
        for i=1:size(smoothClassificationEEG,1)
            smoothClassificationEEG(i)=mode(smoothClassificationEMG(fix(...
                round(shiftEEG/shiftEMG*(i-1))+1):min(fix(round(...
                shiftEEG/shiftEMG*i)),max(size(smoothClassificationEMG)))));
        end

        % drop data according to classification
        trainingDataEEG=trainingDataEEG(smoothClassificationEEG~=-1,:,:);
        trainingDataEEGlf=trainingDataEEGlf(smoothClassificationEEG~=-1,:,:);
        trainingDataEMG=trainingDataEMG(smoothClassificationEMG~=-1,:);
        classificationEMG=smoothClassificationEMG(smoothClassificationEMG~=-1);
        classificationEEG=smoothClassificationEEG(smoothClassificationEEG~=-1);

        % all created matching the size of EMGdata, if necessary downsampling
        % has to be done when used
        kinematics=kinMat(smoothClassificationEMG~=-1,:);
        ae=trainAutoencoder(trainingDataEMG',noSynergies,'ShowProgressWindow',false);
        synergiesAutoenc=encode(ae,trainingDataEMG')';
        COEFF=pca(trainingDataEMG,'Centered',false);
        synergiesPCA=trainingDataEMG/COEFF(:,1:noSynergies)';
        [~,H]=nnmf(trainingDataEMG,noSynergies);
        synergiesNNMF=trainingDataEMG*H';

        clear smoothClassification i
        save(savePath,'trainingDataEEG','trainingDataEEGlf','trainingDataEMG',...
            'classificationEMG','classificationEEG','kinematics','synergiesAutoenc',...
            'synergiesPCA','synergiesNNMF','-v7.3');


        %fprintf('finished reading %s%i %s\n',subject,number,datestr(datetime('now')));
    else
        fprintf(strcat(savePath,' yet existing; nothing to do\n'));
    end
end
