%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           Fish Track Plot                               %
%             this function is designed for fish track only               %
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function FishTrackPlot()
clear all;
clc;
%define variable
global barflag;
global envelopeflag;
global filename;
global interval;
global datanew;
global datanewc;
barflag=0;
envelopeflag=0;
interval=1;


%load data
[file, path] = uigetfile('*.mat',...
'Load data to plot(distance*.mat)');
if isequal(file,0) || isequal(path,0)
disp('Error:No File Load')
return
else
disp(['User selected ', fullfile(path, file)])
end
data=load(strcat(path,file),'totaldistance*');
startTime=load(strcat(path,file),'startTime');
data=struct2cell(data);
data=cell2mat(data);
startTime=struct2cell(startTime);
startTime=cell2mat(startTime);
clear L;
clear i;
%uicontrol setup
uicontrol('style','edit',...
          'string','Interval',...
          'position',[470 0 100 20], ...
          'callback', @time_interval);
uicontrol('style', 'togglebutton', ...
          'position',[90 0 80 20],...
          'string','Enve Off',...
          'min', 0, 'max',1,...
          'value', 0, ...
          'callback', @envelope_flag);
uicontrol('style', 'togglebutton', ...
          'position',[170 0 80 20],...
          'string','Bar Off',...
          'min', 0, 'max',1,...
          'value', 0, ...
          'callback', @bar_flag);
uicontrol('style','pushbutton',...
          'string', 'Save .xls', ...
          'position',[10 0 80 20], ...
          'callback',  @save_data);
uicontrol('style','pushbutton',...
          'string', 'Plot', ...
          'position',[250 0 80 20], ...
          'callback',  @plot_figure);
uicontrol('style','edit',...
          'string','File Name',...
          'position',[350 0 100 20], ...
          'callback', @setfilename);

      
%callback function
    function envelope_flag(hObject,~)
        buttonvalue=get(hObject,'value');
        switch buttonvalue
            case 0
                envelopeflag=0;
                set(hObject,'String','Enve Off');
            case 1
                envelopeflag=1;
                set(hObject,'string','Enve On');
        end
    end
    function bar_flag(hObject,~)
        buttonvalue=get(hObject,'value');
        switch buttonvalue
            case 0
                barflag=0;
                set(hObject,'String','Bar Off');
            case 1
                barflag=1;
                set(hObject,'string','Bar On');
        end
    end
    function setfilename(hObject,~)
        filename=get(hObject,'string');
    end
    function time_interval(hObject,~)
        interval=get(hObject,'string');
        interval=str2double(interval);
    end
    function save_data(~,~)
        Datadirectory = uigetdir('C:\user','Set a directory to save data');
        @setfilename;
        xlswrite(strcat(Datadirectory,'\', filename,'_',...
        num2str(interval),'min'),datanew);
        disp(sprintf('File will be saved to %s\\%s_%dmin.xls',...
            Datadirectory,filename,interval));
    end
    function plot_figure(~,~)   
    @time_interval;
    clear datanew;
    clear datanewc;
   
       L=floor(length(data)/interval);
       
       for i=1:L
          datanew(i,1)=sum(data(((i-1)*interval+1):i*interval,1));
          datanew(i,2)=data(i*interval,2);
       end
       if L<length(data)/interval
         datanew(L+1,1)=sum(data((L*interval+1):length(data),1));
         datanew(L+1,2)=data(length(data),2);
       end
       if L>400
           datanewc=datanew;
       else
       [datanewc(:,2),datanewc(:,1)]=d2a(datanew(:,2),datanew(:,1));
       for i=1:length(datanewc(:,1))
           if datanewc(i,1) < 0
               datanewc(i,1)=0;
           end
       end
       end
       switch barflag*10+envelopeflag
           case 1,
               plot(datanewc(:,2),datanewc(:,1),'r')
           case 10,
               bar(datanew(:,2),datanew(:,1))
           case 11,
               plot(datanewc(:,2),datanewc(:,1),'r'),hold on,...
                   bar(datanew(:,2),datanew(:,1)),hold off
           otherwise,
               cla
       end
       grid on;
        title(strcat('Starting From:',num2str(startTime(1)),...
        '/',num2str(startTime(2)),'/',num2str(startTime(3)),...
        '/',num2str(startTime(4)),':',num2str(startTime(5))));
       xlabel('Time(min)');
       ylabel('Distance(inch)');
    end
end
           
