function Fishtrack3()
clear all
delete(imaqfind)
Datadirectory = uigetdir('C:\user','Set a directory to save data')
root=0;
videoinfo = imaqhwinfo('winvideo');
video=videoinput('winvideo',numel(videoinfo.DeviceIDs));
video2=videoinput('winvideo',numel(videoinfo.DeviceIDs)-1);
video3=videoinput('winvideo',numel(videoinfo.DeviceIDs)-2);
triggerconfig(video,'manual');
triggerconfig(video2,'manual');
triggerconfig(video3,'manual');
set(video,'FramesPerTrigger',1);
set(video,'ReturnedColorSpace','grayscale');
set(video2,'FramesPerTrigger',1);
set(video2,'ReturnedColorSpace','grayscale');
set(video3,'FramesPerTrigger',1);
set(video3,'ReturnedColorSpace','grayscale');
videoparameter=imaqhwinfo(video);
vHeight=videoparameter.MaxHeight;
vWidth=videoparameter.MaxWidth;
videoparameter2=imaqhwinfo(video2);
vHeight2=videoparameter2.MaxHeight;
vWidth2=videoparameter2.MaxWidth;
videoparameter3=imaqhwinfo(video3);
vHeight3=videoparameter3.MaxHeight;
vWidth3=videoparameter3.MaxWidth;
fig=figure(1);
clf;
Imback=zeros(vHeight,vWidth);
Background=0;
Imback2=zeros(vHeight2,vWidth2);
Background2=0;
Imback3=zeros(vHeight3,vWidth3);
Background3=0;
start(video)
start(video2)
start(video3)



uicontrol('style', 'togglebutton', ...
          'position',[150 0 100 20],...
          'string','Video Preview',...
          'min', 0, 'max',1,...
          'value', 0, ...
          'callback', @videoPreview);
      
uicontrol('style', 'text', ...
          'string', 'File Name', ...
          'fontsize', 12, ...
          'position', [470 20 100 20]);

uicontrol('style','edit',...
                    'position',[470 0 100 20], ...
                    'callback', @setfilename);
                      
uicontrol('style','text',...
          'string', 'Timer', ...
          'fontsize', 12, ...
          'position',[360 20 90 20]);
      
uicontrol('style','edit',...
                    'position',[360 0 90 20], ...
                    'callback', @set_timer);
                
uicontrol('style','pushbutton',...
    'string','STOP Program', ...
    'position',[250 0 100 20], ...
    'callback','global zstop; zstop=0;');

uicontrol('style', 'pushbutton', ...
          'position',[150 20 100 20],...
          'string','Acquire ROI',...
          'callback', @AcquireROI);
      
uicontrol('style','pushbutton',...
    'string', 'Start tracking', ...
    'position',[0 0 150 20], ...
    'callback',  @track);

uicontrol('style','pushbutton',...
    'string', 'Fish position', ...
    'position',[250 20 100 20], ...
    'callback', @Initialposition);

fig2=figure(2);

uicontrol('style', 'togglebutton', ...
          'position',[150 0 100 20],...
          'string','Video Preview',...
          'min', 0, 'max',1,...
          'value', 0, ...
          'callback', @videoPreview2);
      
uicontrol('style', 'text', ...
          'string', 'File Name', ...
          'fontsize', 12, ...
          'position', [470 20 100 20]);

uicontrol('style','edit',...
                    'position',[470 0 100 20], ...
                    'callback', @setfilename2);
                                  
uicontrol('style', 'pushbutton', ...
          'position',[150 20 100 20],...
          'string','Acquire ROI',...
          'callback', @AcquireROI2);
      
uicontrol('style','pushbutton',...
    'string', 'Fish position', ...
    'position',[250 20 100 20], ...
    'callback', @Initialposition2);

fig3=figure(3);


uicontrol('style', 'togglebutton', ...
          'position',[150 0 100 20],...
          'string','Video Preview',...
          'min', 0, 'max',1,...
          'value', 0, ...
          'callback', @videoPreview3);
      
uicontrol('style', 'text', ...
          'string', 'File Name', ...
          'fontsize', 12, ...
          'position', [470 20 100 20]);

uicontrol('style','edit',...
                    'position',[470 0 100 20], ...
                    'callback', @setfilename3);
                      
uicontrol('style', 'pushbutton', ...
          'position',[150 20 100 20],...
          'string','Acquire ROI',...
          'callback', @AcquireROI3);

uicontrol('style','pushbutton',...
    'string', 'Fish position', ...
    'position',[250 20 100 20], ...
    'callback', @Initialposition3);

global zstop
global durationTimer;
global filename;
global filename2;
global filename3;
global logObj
global logObj2
global logObj3
global recentFullBack;
global recentFullBack2;
global recentFullBack3;
global Lastposition;
global Lastposition2;
global Lastposition3;
Lastposition=[180 120];
Lastposition2=[180 120];
Lastposition3=[180 120];



zstop=1;

    function Initialposition(hObject,~)
        Initialfig=getsnapshot(video);
        fig_initial=figure; set(fig_initial,'position', [300, 250, 600, 450]);
        imagesc(Initialfig);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        Lastposition=ginput(1);
        fprintf('\n Initial fish position set to [%03.2d %03.2d]', round(Lastposition));
        close(fig_initial);
    end
function Initialposition2(hObject,~)
        Initialfig=getsnapshot(video2);
        fig_initial=figure; set(fig_initial,'position', [300, 250, 600, 450]);
        imagesc(Initialfig);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        Lastposition2=ginput(1);
        fprintf('\n Initial fish position set to [%03.2d %03.2d]', round(Lastposition2));
        close(fig_initial);
end
function Initialposition3(hObject,~)
        Initialfig=getsnapshot(video3);
        fig_initial=figure; set(fig_initial,'position', [300, 250, 600, 450]);
        imagesc(Initialfig);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        Lastposition3=ginput(1);
        fprintf('\n Initial fish position set to [%03.2d %03.2d]', round(Lastposition3));
        close(fig_initial);
end


function setfilename(hObject,~)
filename=get(hObject,'string');
if ~isdir(strcat(Datadirectory,'\',filename))
    mkdir(strcat(Datadirectory,'\',filename))
end
disp(sprintf('File will be saved to %s\\%s\\', Datadirectory, filename));
if exist('logObj')
    close(logObj);
    clear logObj;
end
end

function setfilename2(hObject,~)
filename2=get(hObject,'string');
if ~isdir(strcat(Datadirectory,'\',filename2))
    mkdir(strcat(Datadirectory,'\',filename2))
end
disp(sprintf('File will be saved to %s\\%s\\', Datadirectory, filename2));
if exist('logObj2')
    close(logObj2);
    clear logObj2;
end
end

function setfilename3(hObject,~)
filename3=get(hObject,'string');
if ~isdir(strcat(Datadirectory,'\',filename3))
    mkdir(strcat(Datadirectory,'\',filename3))
end
disp(sprintf('File will be saved to %s\\%s\\', Datadirectory, filename3));
if exist('logObj3')
    close(logObj3);
    clear logObj3;
end
end


    function set_timer(hObject,~)
        timerlength=str2num(get(hObject,'String'))*3600;  % should be in hour
        disp(sprintf('Timer set to %2.0d hours',str2num(get(hObject,'String'))));
        delete(timerfind);
        durationTimer=timer('TimerFcn', 'global zstop; zstop=0;', 'StartDelay', timerlength);
    end

    function videoPreview(hObject,~)
        buttonvalue=get(hObject,'value');
        switch buttonvalue
            case 0
                stoppreview(video);
                set(hObject,'String','View On');
                closepreview(video);
            case 1
                preview(video);
                set(hObject,'string','View Off');
        end
    end

 function videoPreview2(hObject,~)
        buttonvalue=get(hObject,'value');
        switch buttonvalue
            case 0
                stoppreview(video2);
                set(hObject,'String','View On');
                closepreview(video2);
            case 1
                preview(video2);
                set(hObject,'string','View Off');
        end
 end

 function videoPreview3(hObject,~)
        buttonvalue=get(hObject,'value');
        switch buttonvalue
            case 0
                stoppreview(video3);
                set(hObject,'String','View On');
                closepreview(video3);
            case 1
                preview(video3);
                set(hObject,'string','View Off');
        end
 end



    function AcquireROI(hObject,~)
        Imback=recentFullBack;
        stop(video);
        set(video,'ROIposition',[0 0 vWidth vHeight]);
        ROIback=getsnapshot(video);
        fig_ROI=figure;set(fig_ROI,'position', [300, 250, 600, 450]);
        imagesc(ROIback);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        h = imrect;
        addNewPositionCallback(h,@(p) title(mat2str(p,3)));
        fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
        setPositionConstraintFcn(h,fcn); 
        position=wait(h);
        
        set(video,'ROIposition',position);
        
        start(video);
        Imzero=zeros(fix(position(4)),fix(position(3)));
        
        for m=1:10
            Imzero=Imzero+double(getsnapshot(video));
        end
        Imback=Imzero/10;
        recentFullBack=Imback;
        Background=1;
        cla
        imagesc(Imback);
        axis off;
        axis auto;
        axis normal;
        fprintf('\n ROI set to [%03.2d %03.2d %3.0d %3.0d]', round(position));
    end

function AcquireROI2(hObject,~)
        
        stop(video2);
        set(video2,'ROIposition',[0 0 vWidth vHeight]);
        ROIback=getsnapshot(video2);
     
        fig_ROI=figure;set(fig_ROI,'position', [300, 250, 600, 450]);
        imagesc(ROIback);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        h = imrect;
        addNewPositionCallback(h,@(p) title(mat2str(p,3)));
        fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
        setPositionConstraintFcn(h,fcn); 
        position=wait(h);
        
        set(video2,'ROIposition',position);
        
        start(video2);
        Imzero=zeros(fix(position(4)),fix(position(3)));
        
        for m=1:10
            Imzero=Imzero+double(getsnapshot(video2));
        end
        Imback2=Imzero/10;
        recentFullBack2=Imback2;
        Background2=1;
        cla
        imagesc(Imback2);
        axis off;
        axis auto;
        axis normal;
        fprintf('\n ROI set to [%03.2d %03.2d %3.0d %3.0d]', round(position));
end

function AcquireROI3(hObject,~)
      
        stop(video3);
        set(video3,'ROIposition',[0 0 vWidth3 vHeight3]);
        ROIback=getsnapshot(video3);

        fig_ROI=figure;set(fig_ROI,'position', [300, 250, 600, 450]);
        imagesc(ROIback);   colormap(gray);
        axis off;
        axis auto;
        axis normal;
        h = imrect;
        addNewPositionCallback(h,@(p) title(mat2str(p,3)));
        fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
        setPositionConstraintFcn(h,fcn); 
        position=wait(h);
        
        set(video3,'ROIposition',position);
        
        start(video3);
        Imzero=zeros(fix(position(4)),fix(position(3)));
        
        for m=1:10
            Imzero=Imzero+double(getsnapshot(video3));
        end
        Imback3=Imzero/10;
        recentFullBack3=Imback3;
        Background3=1;
        cla
        imagesc(Imback3);
        axis off;
        axis auto;
        axis normal;
        fprintf('\n ROI set to [%03.2d %03.2d %3.0d %3.0d]', round(position));
end



    function [XY,Flag,Maxintensity]=Fishposition(Imlive,Imback)
        Imsub=double(Imlive)-double(Imback);
        Imsub(isnan(Imsub))=255;
        thresholdedIMG = medfilt2(-Imsub.*(Imsub<0)).^2;
        filter=thresholdedIMG>5;
        CC=bwconncomp(filter);
         for m=1:CC.NumObjects
             meanintensity(m)=sum(sum(thresholdedIMG(cell2mat(CC.PixelIdxList(m)))));
         end
         
       if CC.NumObjects>0 && max(meanintensity)>50000
             
            [~,maxint]=max(meanintensity);
       
          pos=get(video,'ROIposition');
         Area=numel(cell2mat(CC.PixelIdxList(maxint)));
         if Area<25 || Area>0.8*pos(3)*pos(4)
             Flag=0;
             XY=[NaN NaN];
             Maxintensity=0;
             return
         else
             Flag=1;
         end
         
         In=cell(1,numel(CC.ImageSize));
         [In{:}] = ind2sub(CC.ImageSize, cell2mat(CC.PixelIdxList(maxint)));
         pixellist=[In{:}];
         pixellist= pixellist(:,[2 1 3:end]);
         centroid=mean(pixellist,1);
         
         XY = centroid;
         Maxintensity=max(meanintensity);
         else
             Flag=0;
             XY=[NaN NaN];
             Maxintensity=0;
       end
    end

    function track(hObject,~)
        Imback=recentFullBack;
        Imback2=recentFullBack2;
        Imback3=recentFullBack3;
        if isempty(filename) %|| ~exist('logObj')
            error('Please set a filename to save video');
        end
         if isempty(filename2) %|| ~exist('logObj2')
            error('Please set a filename to save video');
         end
         if isempty(filename3) %|| ~exist('logObj3')
            error('Please set a filename to save video');
         end
        
        zstop=1;
        i=1;Cycle=1;
        timeRecord=0;
        start(durationTimer); disp('Starting Timer')
        alpha=0.9;
        frameBounds = size(Imback);
        frameBounds2 = size(Imback2);
        frameBounds3 = size(Imback3);
        totaldistance=[];
        totaldistance2=[];
        totaldistance3=[];
        pos=get(video,'ROIposition');
        pos2=get(video2,'ROIposition');
        pos3=get(video3,'ROIposition');
        Pixellength=20/pos(3); % in inch
        Pixellength2=20/pos2(3); % in inch
        Pixellength3=20/pos3(3); % in inch
        save(strcat(Datadirectory,'\', filename,'\', 'Pixellength1'),...
                      'Pixellength');
        save(strcat(Datadirectory,'\', filename2,'\', 'Pixellength2'),...
                      'Pixellength2');
        save(strcat(Datadirectory,'\', filename3,'\', 'Pixellength3'),...
                      'Pixellength3');   
        nn=1;
        startTime=fix(clock);
        while(zstop)
            tic;
            set(root,'CurrentFigure',fig);
            Imlive=getsnapshot(video);
            [XY,Flag,Maxintensity]=Fishposition(Imlive,Imback);
           clear Fishposition;
          if ~Flag
                XY=Lastposition;
          else
         
            if sqrt((XY(1)-Lastposition(1))^2+(XY(2)-Lastposition(2))^2)>40 ...
                && Maxintensity<300000   % which value
               
                XY(1)=Lastposition(1); XY(2)=Lastposition(2);
            else
                Lastposition(1)=XY(1); Lastposition(2)=XY(2);
            end
          end
                
            if Flag
                FishSquare = Imback(max(1,round(XY(2)-20)):min(frameBounds(1),round(XY(2)+20)),...
                 max(1,round(XY(1)-20)):min(frameBounds(2),round(XY(1)+20)));
                Imback = alpha*Imback+(1-alpha)*double(Imlive);
                Imback(max(1,round(XY(2)-20)):min(frameBounds(1),round(XY(2)+20)),...
                 max(1,round(XY(1)-20)):min(frameBounds(2),round(XY(1)+20))) = FishSquare;
            end
            cla;
            imagesc(Imlive); axis off; colormap(gray); hold all
            plot(XY(1),XY(2),'r.','MarkerSize',20);
            hold off
            drawnow;
            clear frame;
            
            Location(i,:)=[XY,Flag,timeRecord];
            clear Imlive;
            
            set(root,'CurrentFigure',fig2);
            Imlive2=getsnapshot(video2);
            [XY2,Flag2,Maxintensity2]=Fishposition(Imlive2,Imback2);
            clear Fishposition;
          if ~Flag2
                XY2=Lastposition2;
          else
         
            if sqrt((XY2(1)-Lastposition2(1))^2+(XY2(2)-Lastposition2(2))^2)>40 ...
                && Maxintensity2<300000   % which value
               
                XY2(1)=Lastposition2(1); XY2(2)=Lastposition2(2);
            else
                Lastposition2(1)=XY2(1); Lastposition2(2)=XY2(2);
            end
          end
                
            if Flag2
                FishSquare2 = Imback2(max(1,round(XY2(2)-20)):min(frameBounds2(1),round(XY2(2)+20)),...
                 max(1,round(XY2(1)-20)):min(frameBounds2(2),round(XY2(1)+20)));
                Imback2 = alpha*Imback2+(1-alpha)*double(Imlive2);
                Imback2(max(1,round(XY2(2)-20)):min(frameBounds2(1),round(XY2(2)+20)),...
                 max(1,round(XY2(1)-20)):min(frameBounds2(2),round(XY2(1)+20))) = FishSquare2;
            end
            cla;
            imagesc(Imlive2); axis off; colormap(gray); hold all
            plot(XY2(1),XY2(2),'r.','MarkerSize',20);
            hold off
            drawnow;
            clear frame;
            
            Location2(i,:)=[XY2,Flag2,timeRecord];
            clear Imlive2;
            
            set(root,'CurrentFigure',fig3);
            Imlive3=getsnapshot(video3);
            [XY3,Flag3,Maxintensity3]=Fishposition(Imlive3,Imback3);
           clear Fishposition;
          if ~Flag3
                XY3=Lastposition3;
          else
         
            if sqrt((XY3(1)-Lastposition3(1))^2+(XY3(2)-Lastposition3(2))^2)>40 ...
                && Maxintensity3<300000   % which value
               
                XY3(1)=Lastposition3(1); XY3(2)=Lastposition3(2);
            else
                Lastposition3(1)=XY3(1); Lastposition3(2)=XY3(2);
            end
          end
                
            if Flag3
                FishSquare3 = Imback3(max(1,round(XY3(2)-20)):min(frameBounds3(1),round(XY3(2)+20)),...
                 max(1,round(XY3(1)-20)):min(frameBounds3(2),round(XY3(1)+20)));
                Imback3 = alpha*Imback3+(1-alpha)*double(Imlive3);
                Imback3(max(1,round(XY3(2)-20)):min(frameBounds3(1),round(XY3(2)+20)),...
                 max(1,round(XY3(1)-20)):min(frameBounds3(2),round(XY3(1)+20))) = FishSquare3;
            end
            cla;
            imagesc(Imlive3); axis off; colormap(gray); hold all
            plot(XY3(1),XY3(2),'r.','MarkerSize',20);
            hold off
            drawnow;
            clear frame;
            
            Location3(i,:)=[XY3,Flag3,timeRecord];
            clear Imlive3;
            timeRecord=timeRecord + toc;
            if (timeRecord >= nn*3600)
                currenttime=fix(clock);
                a=Location(1,1:2);
                j=1;
                for k=1:1:length(Location);
                   if(sqrt((Location(k,1)-Location(j,1))^2+(Location(k,2)-Location(j,2))^2)>5);
                      a=[a;mean(Location(j:k-1,1)) mean(Location(j:k-1,2))];
                        j=k;
                   end
                end
                Distance1 = ([0 0;a]-[a;0 0]).^2;
                Distance2=sqrt(Distance1(:,1)+Distance1(:,2));
                Distance=sum(Distance2)-Distance2(1)-Distance2(length(Distance2));

                Time = currenttime(5)+ currenttime(4)*10^2 + currenttime(3)*10^4 +currenttime(2)*10^6;

                 clear a;clear k;clear j;clear Distance1;clear Distance2;
                 
                save(strcat(Datadirectory,'\', filename,'\', filename,'_Cycle', num2str(Cycle)),...
                      'Location','currenttime');
                  totaldistance(Cycle,:)=[Distance*Pixellength, nn];
                  save(strcat(Datadirectory,'\', filename,'\', 'distance1'),...
                      'totaldistance','startTime');   
                  
                  a=Location2(1,1:2);
                j=1;
                for k=1:1:length(Location2);
                   if(sqrt((Location2(k,1)-Location2(j,1))^2+(Location2(k,2)-Location2(j,2))^2)>5);
                      a=[a;mean(Location2(j:k-1,1)) mean(Location2(j:k-1,2))];
                        j=k;
                   end
                end
                Distance1 = ([0 0;a]-[a;0 0]).^2;
                Distance2=sqrt(Distance1(:,1)+Distance1(:,2));
                Distance=sum(Distance2)-Distance2(1)-Distance2(length(Distance2));

                Time = currenttime(5)+ currenttime(4)*10^2 + currenttime(3)*10^4 +currenttime(2)*10^6;

                 clear a;clear k;clear j;clear Distance1;clear Distance2;
                 
                save(strcat(Datadirectory,'\', filename2,'\', filename2,'_Cycle', num2str(Cycle)),...
                      'Location2','currenttime');
                  totaldistance2(Cycle,:)=[Distance*Pixellength2, nn];
                  save(strcat(Datadirectory,'\', filename2,'\', 'distance2'),...
                      'totaldistance2','startTime');   
                  
                  a=Location3(1,1:2);
                j=1;
                for k=1:1:length(Location3);
                   if(sqrt((Location3(k,1)-Location3(j,1))^2+(Location3(k,2)-Location3(j,2))^2)>5);
                      a=[a;mean(Location3(j:k-1,1)) mean(Location3(j:k-1,2))];
                        j=k;
                   end
                end
                Distance1 = ([0 0;a]-[a;0 0]).^2;
                Distance2=sqrt(Distance1(:,1)+Distance1(:,2));
                Distance=sum(Distance2)-Distance2(1)-Distance2(length(Distance2));

                Time = currenttime(5)+ currenttime(4)*10^2 + currenttime(3)*10^4 +currenttime(2)*10^6;

                 clear a;clear k;clear j;clear Distance1;clear Distance2;
                 
                save(strcat(Datadirectory,'\', filename3,'\', filename3,'_Cycle', num2str(Cycle)),...
                      'Location3','currenttime');
                  totaldistance3(Cycle,:)=[Distance*Pixellength3, nn];
                  save(strcat(Datadirectory,'\', filename3,'\', 'distance3'),...
                      'totaldistance3','startTime');   
                  
                  Cycle=Cycle+1;
                  nn=nn+1;
                  i=0;
                  clear Location;
                  clear Location2; 
                  clear Location3;
                 
                  flushdata(video);
                  flushdata(video2);
                  flushdata(video3);
                  
            end
            i=i+1;
        end
        currenttime=fix(clock);
        disp(sprintf('\nCompleted at %02.0f:%02.0f:%02.0f\nTotal time: %8.2d seconds',...
        currenttime(4:6),timeRecord));
        save(strcat(Datadirectory,'\', filename,'\', filename,'_Cycle', num2str(Cycle)),...
                      'Location','currenttime');
        save(strcat(Datadirectory,'\', filename,'\', 'distance1'),...
                      'totaldistance','startTime');   
        save(strcat(Datadirectory,'\', filename2,'\', filename2,'_Cycle', num2str(Cycle)),...
                      'Location2','currenttime');
        save(strcat(Datadirectory,'\', filename2,'\', 'distance2'),...
                      'totaldistance2','startTime');
        save(strcat(Datadirectory,'\', filename3,'\', filename3,'_Cycle', num2str(Cycle)),...
                      'Location3','currenttime');
        save(strcat(Datadirectory,'\', filename3,'\', 'distance3'),...
                      'totaldistance3','startTime');
       % close(logObj);        
       h1=figure;
        bar(totaldistance(:,2),totaldistance(:,1));
        title(strcat('Starting From:',num2str(startTime(1)),'/',num2str(startTime(2)),'/',num2str(startTime(3)),...
        '/',num2str(startTime(4)),':',num2str(startTime(5))));
        xlabel('Time(hour)');
        ylabel('Distance(inch)');
        saveas(h1,strcat(Datadirectory,'\', filename,'\', filename,'plot.fig'));
        saveas(h1,strcat(Datadirectory,'\', filename,'\', filename,'plot.jpg'));
        h2=figure;
        bar(totaldistance2(:,2),totaldistance2(:,1));
        title(strcat('Starting From:',num2str(startTime(1)),'/',num2str(startTime(2)),'/',num2str(startTime(3)),...
        '/',num2str(startTime(4)),':',num2str(startTime(5))));
        xlabel('Time(hour)');
        ylabel('Distance(inch)');
        saveas(h2,strcat(Datadirectory,'\', filename2,'\', filename2,'plot.fig'));
        saveas(h2,strcat(Datadirectory,'\', filename2,'\', filename2,'plot.jpg'));
        h3=figure;
        bar(totaldistance3(:,2),totaldistance3(:,1));
        title(strcat('Starting From:',num2str(startTime(1)),'/',num2str(startTime(2)),'/',num2str(startTime(3)),...
        '/',num2str(startTime(4)),':',num2str(startTime(5))));
        xlabel('Time(hour)');
        ylabel('Distance(inch)');
        saveas(h3,strcat(Datadirectory,'\', filename3,'\', filename3,'plot.fig'));
        saveas(h3,strcat(Datadirectory,'\', filename3,'\', filename3,'plot.jpg'));
   
       
    end
        
end