%uses a the sheet "behavior_notes" on the selected excel file to rename a series of movie files
%general stucture : assumes you have 7 baseline videos, you can also have "label" videos, these just need to be defined in the xls file
%naming structure for excel file: It has three columns to explain the video: treatment, day_inj, and plate. Example formatting->
% treatment day_inj plate
% label label label
%doma_vtv  2dpf  1
%label label label
%water_2dpf  ok  1
%label label label
%water_2dpf  ok  2

path = uigetdir() % select folder that contains the movies
cd(path)

list= dir('*.mov');

xlsfile= uigetfile(fullfile(pwd,'folder1','*.xls;*.xlsx'),'select file')

disp('Uses sheet labelled behavior_notes'); 
[~, ~, all] = xlsread(xlsfile, 1);

%if your file is already in the directory, you can hardcode which
%file to use via the command below
%[~, ~, all] = xlsread('trial_behavior_notes.xlsx', 2);

disp('7 videos are baseline at 0.25V');

vid_type = {'basln_1'...
    , 'basln_2', 'basln_3', 'basln_4', 'basln_5', 'basln_6', 'basln_7'}
    %alter this if this is not the sequence of videos for each plate
    
vid_count = 7


col_len = size(all, 2); %number of cols  (assumes all sheets have same num of cols and same header positions
headers = cell(1,col_len);

%get headers 
for l = 1:col_len 
    headers{l} = all{1,l} ;
end  

col_treatment= find(strcmp(headers,'treatment'));
col_dayinj = find(strcmp(headers,'day_inj'));
col_plate = find(strcmp(headers,'plate'));

%%
    
%goes through each row (identifier for each plate)
vid_total = 1;
for row = 2:size(all,1) %data starts on the 2nd row  (first is just headers)
    flag = 0;
    vid_idx = 1;
    for vid =  1:vid_count  
       if(flag==1)% breaks out of loop in flagged (if previous movie was a label)   
           break       
       end
       if strcmp(all{row,col_treatment}, 'label') % if it is a label video
           flag=1; %flag set to 1 so that it will break out of outer for loop
           temp = [all{row + 1 ,col_treatment}, '_' , all{row +1 ,col_dayinj}, '_', num2str(all{row +1 ,col_plate})];  %grabs name of treatment after
           name = ['label_' , temp];
           if ~isequal(list(vid_total).name,[name ' .mov' ]) %if the new name is different from the old one
               movefile(list(vid_total).name,[name ' .mov' ]); %takes corresponding movie, renames it with 'label' and treatment details
           end 
           vid_total = vid_total + 1;
                
       else
           temp = [all{row ,col_treatment}, '_' , all{row ,col_dayinj}, '_', num2str(all{row ,col_plate})];  %grabs name of treatment after
           name = [temp, '_', vid_type{vid_idx}];
           if ~isequal(list(vid_total).name,[name ' .mov' ]) %if the new name is different from the old one
               movefile(list(vid_total).name,[name ' .mov' ]); %takes corresponding movie, renames it with 'label' and treatment details
               if vid_idx < vid_count
                   vid_idx= vid_idx + 1;
               end
               if vid_total <= size(list,1)
                    vid_total = vid_total + 1
               end
               
           end 
           
      end
    end
end
        