function [s] = otdata(workdir)
%this m file generates an array of images
%input:
 %workdir = working directory
%output:
 %M = array of frames
 %s = if 1 process ended successfully, if 0 wrong number of input variable

if(nargin == 1)
    %load file
    cd(workdir);
    list = dir(fullfile(workdir))
    nfile = numel(list)-1;
    list = list(3:nfile)
    nfile = numel(list);
    for j = 1:nfile
        %for i=1:10
        A = dlmread(list(j).name,'\t',6,0);
        name=list(j).name;  
        fid = fopen(name,'r');     
        line1 = fgets(fid)
        line2 = fgets(fid)
        %tline = fgets(fid)
        fclose(fid); 
        %Nrows = numel(textread(list(j).name,'%1c%*[^\n]')) 
        %nrows = size(A, 1)
        %ncolumns = size(A, 2);
      
        %extract single vectors
        t = A(:,1);
        x = A(:,2);
        F = A(:,3);
        relative = A(:,4);
        
        
      % 1) zero the force
        F=F-F(1);
        
      % 2) find force closest to 26pN (>=26)
        val=F-26; 
        index1=min(find(val>=0));
        closestValue = F(index1); % Finds first one only
       
        
        %set treshold: show only graphs with forces higher than 30pN
        if find(F>30);
            
      %3) zero the elongation
        x=(x-(x(index1)-1379))/1379; 
        
      %4) find max F and corresponing relative elongation value:
        index2 = find(F == max(F(:)));
        elong=x(index2);
        
      %5) find x-range after rupture force:
        F=F(end:-1:1); %reverse vector F
        index=1;
        while F(index)<3; %set treshold to 1
            if index <length(F);
                index(index)=index+1;
            else index=length(F) ;
                break
            end 
        end
   
        % find last F value before treshold is exceeded
        iST=index; %iST: vector of indices smaller treshold
        iLV=max(iST)-1 %index of last value in iteration, before treshold reached
        if iLV >0
            iLV=length(F)-iLV+1;
        else iLV=iLV+1;
        end 
      
        display(iLV);
        F=F(end:-1:1);
        corresponding_F_value=F(iLV);
        display(corresponding_F_value);
        plot(x,F,'r')
        hold on
      
     %6) linear fit (fix m=0) of Force baseline after DNA rupture
       F_fit=F(iLV:end);
       b_fit = mean(F_fit);
      
     %7) Subtract baseline value from F_max
       F_max=max(F)-b_fit;
       ind= find(F==max(F));
       F_1=F(1:(ind-1));
       F_1=F_1';
       F_2=F(ind+1:length(F));
       F_2=F_2';
       F=[F_1 F_max F_2];
      
       %Plot baseline 
       b_fit(1:length(x)) = b_fit;
       plot(x,b_fit,'b')      
       hold on
       plot(x,F,'b')
       set(gca,'FontSize',13)
       xlhand = get(gca,'xlabel')
       set(xlhand,'string','Relative Extension','fontsize',18) 
       ylhand = get(gca,'ylabel')
       set(ylhand,'string','Force [pN]','fontsize',18) 
       hold on
      
     %8) find value corresponding to 0.05x elongation
      index=1
        while x(index)<0.05; %set treshold to 1
           if index <length(x);
               index(index)=index+1;
           else index=length(x) ;
               break
           end
        end
       zero_ind=max(index);
       plot(x(zero_ind),F(zero_ind),'s')
       hold on  
       
     %9) find force plateau or last force value when DNA ruptures before force plateau
       for ind=1:length(F);
       if ind <length(F)-10;
       %calculate SD for dataset with following size(window)
        window=F(ind:ind+10);
        SD1(ind)=std(window); 
        else ind=length(F)-10 ;
        break
       end
       end
        iSD=find(SD1<0.2); %find index of SD values smaller than treshold --> corresponds to plateau in F-x Graph
        dSD=diff(iSD); %find the distance between those indices
        maxdSD=find(dSD==max(dSD))+1; %find index of index with max distance to previous index 
        isP=iSD(maxdSD); %find index of starting point of "possible" second plateau
        if isP <=length(F)
        diffF=F(isP)-F(isP-10) %check whether it is a plateau or rupture
        else isP=length(F)-10 ;
        continue
        end
        if diffF < 0
            ind=isP
            while F(ind+5)-F(ind)>0
            if ind <length(F);
                ind=ind+1;
               else ind=length(F)-5 ;
                   break
               end
            end
        if ind < length(x) %prevents program from crashing
            if F(ind)>5
                ind5= ind - round((ind-zero_ind)/100*15)
                plot(x(ind),F(ind),'s','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10) % plot starting point!
            else ind= find(F==max(F))
              plot(x(ind),F(ind),'s','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10) % plot starting point!   
            end
        hold on
        end
        else
        if isP < length(x)
            isP5 = isP - round((isP-zero_ind)/100*10) %substract from isP 15% of distance between zero_ind and isP
        plot(x(isP5),F(isP5),'s','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10) % plot starting point!
        end
        end
        set(gcf,'Position',[10 10 1200 1400 ])
        
        %insert line1 into graph
        %textul(line1)
        
        %textul1(line2) insert line2
        gtext(line1) %insert string manually
        set(gca,'FontSize',20)
        
        
    % 10) fit exWLC between zero_ind and isP5
    
    
    
      %plot index of treshold as vertical line
      %x=x(iLV)
      %ylimits = get(gca, 'YLim');
      %hold on;   
      %plot([x x], ylimits, 'k');
    hold off
    %plot(SD1) 
    %pause(1)
     M(j) = getframe;
      saveas(gcf, ['C:\Users\Steinbrc\Desktop\OT figures\12.3.13\','figure', num2str(j),'.bmp']);    
      %pause(0.8)   
        end   
    end        
end
s = 0;
end


        