function [meanAccurancy,maxC, cmScaled]= svmEciton(savePath,EEG,k,maxExpC,maxPerClass,eegOffset,moveRest)
% svmEciton runs SVM in parallel to decide class for data with different Cs
    load(savePath)
    %    fprintf('%i,%i,%i',size(trainingDataEMG,1),size(trainingDataEEG,1),size(classification,1))
    addpath('/nfs/wsi/ti/messor/hohlochj/libsvm/matlab');

    %choose to estimate based on EEG or EMG
    if strcmp(EEG,'EEG')
        trainingData=trainingDataEEG(1:end-eegOffset,:);
        classification=classificationEEG(eegOffset+1:end);
    elseif strcmp(EEG,'EMG')
        trainingData=trainingDataEMG;
        classification=classificationEMG;
    elseif strcmp(EEG,'LF')
        trainingData=trainingDataEEGlf(1:end-eegOffset,:);
        classification=classificationEEG(eegOffset+1:end);
    else
        error('only EEG, EMG and LF are valid inputs');
    end
    
    clear trainingDataE*;
    clear classifiactionE*;
    clear kinematics;
    clear synergies*;
    
    % only intersting whether moving or resting
    if moveRest
        classification=double(classification~=0);
    end
    
    accuracy=zeros(k,3);
    maxC=zeros(k,1);
    noClasses=size(unique(classification),1);
    cm=zeros(noClasses);
    randMap=randperm(size(trainingData,1));
    %disp('startCV')
    parfor i=1:k %k-fold CV
        % split data into training and test set
        leaveOut=trainingData(mod(randMap,k)==i-1,:,:);
        leaveClasses=classification(mod(randMap,k)==i-1);
        remaining=trainingData(mod(randMap,k)~=i-1,:,:);
        remainingClasses=classification(mod(randMap,k)~=i-1);
        %fprintf('%s create %ith model\n',datestr(datetime('now')),i)
        
        [model,maxC(i)]=kfoldCV(remainingClasses,remaining,k,maxExpC,maxPerClass);

        [predictions,accuracy(i,:),~]=svmpredict(leaveClasses,leaveOut(:,:),model);
        cm=cm+confusionmat(leaveClasses,predictions); %confusion matrix
    end
    meanAccurancy=mean(accuracy(:,1)); % accuracy is the mean of accuracys

    cmScaled=zeros(size(cm)); % scale confusion matrix to 0-1
    for i=1:size(cm,1)
        cmScaled(i,:)=cm(i,:)/sum(cm(i,:));
    end
end
