clear all;
load('ContextDecodingDay9.mat')
clearvars -except idxs_of_SC_Cells All_LT_data_ms1 All_OF_data_ms1 All_LT_data_ms2 All_OF_data_ms2 All_LT_data_ms3 All_OF_data_ms3
ms1_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells<436);
ms2_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435 & idxs_of_SC_Cells<=435+279);
ms2_SCs=ms2_SCs-(435);
ms3_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279 & idxs_of_SC_Cells<=435+279+304);
ms3_SCs=ms3_SCs-(435+279);
ms4_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279+304 & idxs_of_SC_Cells<=435+279+304+306);
ms4_SCs=ms4_SCs-(435+279+304);

num_bins=25;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=1;

ms1string='D:/ACC Data/ACC/AllDays/635/';
ms2string='D:/ACC Data/ACC/AllDays/681/';
ms3string='D:/ACC Data/ACC/AllDays/691/';
ms4string='D:/ACC Data/ACC/AllDays/747/';
ltstring='/XYtotalspike3.csv';
ofstring='/XYtotalspike2.csv';

if select_mouse==1;
        setofcells=ms1_SCs;
        elseif select_mouse==2;
        setofcells=ms2_SCs;
        elseif select_mouse==3;
        setofcells=ms3_SCs;
        elseif select_mouse==4;
        setofcells=ms4_SCs;
end;


%idxs_of_SC_Cells=sort(idxs_of_SC_Cells,'ascend');


    if select_mouse==1;
        LT_field_size=NaN(9,size(ms1_SCs,1));LT_field_num=NaN(9,size(ms1_SCs,1));LT_coverage=NaN(9,size(ms1_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms1_SCs,1));OF_field_num=NaN(9,size(ms1_SCs,1));OF_coverage=NaN(9,size(ms1_SCs,1));
        elseif select_mouse==2;
        LT_field_size=NaN(9,size(ms2_SCs,1));LT_field_num=NaN(9,size(ms2_SCs,1));LT_coverage=NaN(9,size(ms2_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms2_SCs,1));OF_field_num=NaN(9,size(ms2_SCs,1));OF_coverage=NaN(9,size(ms2_SCs,1));
        elseif select_mouse==3;
        LT_field_size=NaN(9,size(ms3_SCs,1));LT_field_num=NaN(9,size(ms3_SCs,1));LT_coverage=NaN(9,size(ms3_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms3_SCs,1));OF_field_num=NaN(9,size(ms3_SCs,1));OF_coverage=NaN(9,size(ms3_SCs,1));
        elseif select_mouse==4;
        LT_field_size=NaN(9,size(ms4_SCs,1));LT_field_num=NaN(9,size(ms4_SCs,1));LT_coverage=NaN(9,size(ms4_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms4_SCs,1));OF_field_num=NaN(9,size(ms4_SCs,1));OF_coverage=NaN(9,size(ms4_SCs,1));
    end;
    
    
    
All_LT_data=NaN(9,69,length(setofcells));
All_OF_data=NaN(24,24,9,length(setofcells));
for cellnum=1:length(setofcells);
    cellnum
    raw_placemapOF=zeros(24,24,9);norm_placemapOF=zeros(24,24,9);
    raw_placemapLT=zeros(1,69,9);norm_placemapLT=zeros(1,69,9);

for day_num=1:9;
    day_num
    day_var=sprintf('day%d', day_num);
    if select_mouse==1;
        OF_path_var=strcat(ms1string,day_var,ofstring);
        LT_path_var=strcat(ms1string,day_var,ltstring);
        elseif select_mouse==2;
        OF_path_var=strcat(ms2string,day_var,ofstring);
        LT_path_var=strcat(ms2string,day_var,ltstring);
        elseif select_mouse==3;
        OF_path_var=strcat(ms3string,day_var,ofstring);
        LT_path_var=strcat(ms3string,day_var,ltstring);
        elseif select_mouse==4;
        OF_path_var=strcat(ms4string,day_var,ofstring);
        LT_path_var=strcat(ms4string,day_var,ltstring);
end;

    Ms1OF = readtable(OF_path_var);


    %Open Field
    Ms1OF = readtable(OF_path_var);
    x_posn=table2array(Ms1OF(:,2));
    x_posn=smooth(x_posn,15);
    y_posn=table2array(Ms1OF(:,3));
    y_posn=smooth(y_posn,15);
    Ms1OF=sparse(table2array(Ms1OF(1:end,6:end)));
    
    vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
    vel_cmsec=smooth(vel_cmsec,30);
    
    still_periods=find(vel_cmsec<vel_threshold);
    vel_cmsec(still_periods)=NaN;
    Ms1OF(still_periods,:)=NaN;
    x_posn(still_periods)=NaN;y_posn(still_periods)=NaN;
    
    [Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    Occupancy=Occupancy/20;
    
    temp_cell=Ms1OF(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapOF(:,:,day_num),XEDGES,YEDGES] = histcounts2(x_posn(spike_indxs),y_posn(spike_indxs),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    norm_placemapOF(:,:,day_num)=raw_placemapOF(:,:,day_num)./Occupancy;
    norm_placemapOF(:,:,day_num)=imgaussfilt(norm_placemapOF(:,:,day_num),1);
    raw_placemapOF(isnan(raw_placemapOF))=0;norm_placemapOF(isnan(norm_placemapOF))=0;
    
    
    %Linear Track
    MsLT = readtable(LT_path_var);
    
    LT_xposn=MsLT.Var4;
    %LT_yposn=MsLT.Var3;
     
    LT_vel_cmsec=abs(diff(LT_xposn))*(1/0.05);
    LT_vel_cmsec=smooth(LT_vel_cmsec,30);
    
    
    Ms1LT=sparse(table2array(MsLT(1:end,6:end)));
    LT_still_periods=find(LT_vel_cmsec<LT_vel_threshold);
    LT_vel_cmsec(LT_still_periods)=NaN;
    Ms1LT(LT_still_periods,:)=NaN;
    LT_xposn(LT_still_periods)=NaN;%LT_yposn(LT_still_periods)=[];
    
    [LT_Occupancy,XEDGES,YEDGES] = histcounts(LT_xposn,linspace(1,70,LT_num_bins));
    LT_Occupancy=LT_Occupancy/20;
    
    temp_cell=Ms1LT(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapLT(:,:,day_num),XEDGES,YEDGES] = histcounts(LT_xposn(spike_indxs),linspace(1,70,LT_num_bins));
    norm_placemapLT(:,:,day_num)=raw_placemapLT(:,:,day_num)./LT_Occupancy;
    norm_placemapLT(:,:,day_num)=smooth(norm_placemapLT(:,:,day_num),5);
    norm_placemapLT(isnan(norm_placemapLT))=0;raw_placemapLT(isnan(raw_placemapLT))=0;

end;

All_LT_data(:,:,cellnum)=cat(1,norm_placemapLT(:,:,1),norm_placemapLT(:,:,2),norm_placemapLT(:,:,3),norm_placemapLT(:,:,4),norm_placemapLT(:,:,5),norm_placemapLT(:,:,6),norm_placemapLT(:,:,7),norm_placemapLT(:,:,8),norm_placemapLT(:,:,9));
All_OF_data(:,:,:,cellnum)=norm_placemapOF;

if make_plots==1;
    figure;

    for fig_num=1:9;
    subplot(9,1,fig_num);
    imagesc(norm_placemapLT(:,:,fig_num));colormap jet;
    if max(max(norm_placemapLT))>0;
    clim([0 max(max(norm_placemapLT))]);
    else
        clim([0 1]);
    end;
    end;

    figure;
    for fig_num=1:9;
    subplot(1,9,fig_num)
    imagesc(norm_placemapOF(:,:,fig_num));axis square;colormap jet;
    if max(max(max(norm_placemapOF)))>0;
    clim([0 max(max(max(norm_placemapOF)))]);
    else
        clim([0 1]);
    end;
    end;
end;

end

All_LT_data_ms1=All_LT_data;
All_OF_data_ms1=All_OF_data;



load('ContextDecodingDay9.mat')
clearvars -except idxs_of_SC_Cells All_LT_data_ms1 All_OF_data_ms1 All_LT_data_ms2 All_OF_data_ms2 All_LT_data_ms3 All_OF_data_ms3
ms1_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells<436);
ms2_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435 & idxs_of_SC_Cells<=435+279);
ms2_SCs=ms2_SCs-(435);
ms3_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279 & idxs_of_SC_Cells<=435+279+304);
ms3_SCs=ms3_SCs-(435+279);
ms4_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279+304 & idxs_of_SC_Cells<=435+279+304+306);
ms4_SCs=ms4_SCs-(435+279+304);

num_bins=25;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=2;

ms1string='D:/ACC Data/ACC/AllDays/635/';
ms2string='D:/ACC Data/ACC/AllDays/681/';
ms3string='D:/ACC Data/ACC/AllDays/691/';
ms4string='D:/ACC Data/ACC/AllDays/747/';
ltstring='/XYtotalspike3.csv';
ofstring='/XYtotalspike2.csv';

if select_mouse==1;
        setofcells=ms1_SCs;
        elseif select_mouse==2;
        setofcells=ms2_SCs;
        elseif select_mouse==3;
        setofcells=ms3_SCs;
        elseif select_mouse==4;
        setofcells=ms4_SCs;
end;


%idxs_of_SC_Cells=sort(idxs_of_SC_Cells,'ascend');


    if select_mouse==1;
        LT_field_size=NaN(9,size(ms1_SCs,1));LT_field_num=NaN(9,size(ms1_SCs,1));LT_coverage=NaN(9,size(ms1_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms1_SCs,1));OF_field_num=NaN(9,size(ms1_SCs,1));OF_coverage=NaN(9,size(ms1_SCs,1));
        elseif select_mouse==2;
        LT_field_size=NaN(9,size(ms2_SCs,1));LT_field_num=NaN(9,size(ms2_SCs,1));LT_coverage=NaN(9,size(ms2_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms2_SCs,1));OF_field_num=NaN(9,size(ms2_SCs,1));OF_coverage=NaN(9,size(ms2_SCs,1));
        elseif select_mouse==3;
        LT_field_size=NaN(9,size(ms3_SCs,1));LT_field_num=NaN(9,size(ms3_SCs,1));LT_coverage=NaN(9,size(ms3_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms3_SCs,1));OF_field_num=NaN(9,size(ms3_SCs,1));OF_coverage=NaN(9,size(ms3_SCs,1));
        elseif select_mouse==4;
        LT_field_size=NaN(9,size(ms4_SCs,1));LT_field_num=NaN(9,size(ms4_SCs,1));LT_coverage=NaN(9,size(ms4_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms4_SCs,1));OF_field_num=NaN(9,size(ms4_SCs,1));OF_coverage=NaN(9,size(ms4_SCs,1));
    end;
    
    
    
All_LT_data=NaN(9,69,length(setofcells));
All_OF_data=NaN(24,24,9,length(setofcells));
for cellnum=1:length(setofcells);

    raw_placemapOF=zeros(24,24,9);norm_placemapOF=zeros(24,24,9);
    raw_placemapLT=zeros(1,69,9);norm_placemapLT=zeros(1,69,9);

for day_num=1:9;
    day_var=sprintf('day%d', day_num);
    if select_mouse==1;
        OF_path_var=strcat(ms1string,day_var,ofstring);
        LT_path_var=strcat(ms1string,day_var,ltstring);
        elseif select_mouse==2;
        OF_path_var=strcat(ms2string,day_var,ofstring);
        LT_path_var=strcat(ms2string,day_var,ltstring);
        elseif select_mouse==3;
        OF_path_var=strcat(ms3string,day_var,ofstring);
        LT_path_var=strcat(ms3string,day_var,ltstring);
        elseif select_mouse==4;
        OF_path_var=strcat(ms4string,day_var,ofstring);
        LT_path_var=strcat(ms4string,day_var,ltstring);
end;

    Ms1OF = readtable(OF_path_var);


    %Open Field
    Ms1OF = readtable(OF_path_var);
    x_posn=table2array(Ms1OF(:,2));
    x_posn=smooth(x_posn,15);
    y_posn=table2array(Ms1OF(:,3));
    y_posn=smooth(y_posn,15);
    Ms1OF=sparse(table2array(Ms1OF(1:end,6:end)));
    
    vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
    vel_cmsec=smooth(vel_cmsec,30);
    
    still_periods=find(vel_cmsec<vel_threshold);
    vel_cmsec(still_periods)=NaN;
    Ms1OF(still_periods,:)=NaN;
    x_posn(still_periods)=NaN;y_posn(still_periods)=NaN;
    
    [Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    Occupancy=Occupancy/20;
    
    temp_cell=Ms1OF(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapOF(:,:,day_num),XEDGES,YEDGES] = histcounts2(x_posn(spike_indxs),y_posn(spike_indxs),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    norm_placemapOF(:,:,day_num)=raw_placemapOF(:,:,day_num)./Occupancy;
    norm_placemapOF(:,:,day_num)=imgaussfilt(norm_placemapOF(:,:,day_num),1);
    raw_placemapOF(isnan(raw_placemapOF))=0;norm_placemapOF(isnan(norm_placemapOF))=0;
    
    
    %Linear Track
    MsLT = readtable(LT_path_var);
    
    LT_xposn=MsLT.Var4;
    %LT_yposn=MsLT.Var3;
     
    LT_vel_cmsec=abs(diff(LT_xposn))*(1/0.05);
    LT_vel_cmsec=smooth(LT_vel_cmsec,30);
    
    
    Ms1LT=sparse(table2array(MsLT(1:end,6:end)));
    LT_still_periods=find(LT_vel_cmsec<LT_vel_threshold);
    LT_vel_cmsec(LT_still_periods)=NaN;
    Ms1LT(LT_still_periods,:)=NaN;
    LT_xposn(LT_still_periods)=NaN;%LT_yposn(LT_still_periods)=[];
    
    [LT_Occupancy,XEDGES,YEDGES] = histcounts(LT_xposn,linspace(1,70,LT_num_bins));
    LT_Occupancy=LT_Occupancy/20;
    
    temp_cell=Ms1LT(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapLT(:,:,day_num),XEDGES,YEDGES] = histcounts(LT_xposn(spike_indxs),linspace(1,70,LT_num_bins));
    norm_placemapLT(:,:,day_num)=raw_placemapLT(:,:,day_num)./LT_Occupancy;
    norm_placemapLT(:,:,day_num)=smooth(norm_placemapLT(:,:,day_num),5);
    norm_placemapLT(isnan(norm_placemapLT))=0;raw_placemapLT(isnan(raw_placemapLT))=0;

end;

All_LT_data(:,:,cellnum)=cat(1,norm_placemapLT(:,:,1),norm_placemapLT(:,:,2),norm_placemapLT(:,:,3),norm_placemapLT(:,:,4),norm_placemapLT(:,:,5),norm_placemapLT(:,:,6),norm_placemapLT(:,:,7),norm_placemapLT(:,:,8),norm_placemapLT(:,:,9));
All_OF_data(:,:,:,cellnum)=norm_placemapOF;

if make_plots==1;
    figure;

    for fig_num=1:9;
    subplot(9,1,fig_num);
    imagesc(norm_placemapLT(:,:,fig_num));colormap jet;
    if max(max(norm_placemapLT))>0;
    clim([0 max(max(norm_placemapLT))]);
    else
        clim([0 1]);
    end;
    end;

    figure;
    for fig_num=1:9;
    subplot(1,9,fig_num)
    imagesc(norm_placemapOF(:,:,fig_num));axis square;colormap jet;
    if max(max(max(norm_placemapOF)))>0;
    clim([0 max(max(max(norm_placemapOF)))]);
    else
        clim([0 1]);
    end;
    end;
end;

end

All_LT_data_ms2=All_LT_data;
All_OF_data_ms2=All_OF_data;


load('ContextDecodingDay9.mat')
clearvars -except idxs_of_SC_Cells All_LT_data_ms1 All_OF_data_ms1 All_LT_data_ms2 All_OF_data_ms2 All_LT_data_ms3 All_OF_data_ms3
ms1_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells<436);
ms2_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435 & idxs_of_SC_Cells<=435+279);
ms2_SCs=ms2_SCs-(435);
ms3_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279 & idxs_of_SC_Cells<=435+279+304);
ms3_SCs=ms3_SCs-(435+279);
ms4_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279+304 & idxs_of_SC_Cells<=435+279+304+306);
ms4_SCs=ms4_SCs-(435+279+304);

num_bins=25;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=3;

ms1string='D:/ACC Data/ACC/AllDays/635/';
ms2string='D:/ACC Data/ACC/AllDays/681/';
ms3string='D:/ACC Data/ACC/AllDays/691/';
ms4string='D:/ACC Data/ACC/AllDays/747/';
ltstring='/XYtotalspike3.csv';
ofstring='/XYtotalspike2.csv';

if select_mouse==1;
        setofcells=ms1_SCs;
        elseif select_mouse==2;
        setofcells=ms2_SCs;
        elseif select_mouse==3;
        setofcells=ms3_SCs;
        elseif select_mouse==4;
        setofcells=ms4_SCs;
end;


%idxs_of_SC_Cells=sort(idxs_of_SC_Cells,'ascend');


    if select_mouse==1;
        LT_field_size=NaN(9,size(ms1_SCs,1));LT_field_num=NaN(9,size(ms1_SCs,1));LT_coverage=NaN(9,size(ms1_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms1_SCs,1));OF_field_num=NaN(9,size(ms1_SCs,1));OF_coverage=NaN(9,size(ms1_SCs,1));
        elseif select_mouse==2;
        LT_field_size=NaN(9,size(ms2_SCs,1));LT_field_num=NaN(9,size(ms2_SCs,1));LT_coverage=NaN(9,size(ms2_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms2_SCs,1));OF_field_num=NaN(9,size(ms2_SCs,1));OF_coverage=NaN(9,size(ms2_SCs,1));
        elseif select_mouse==3;
        LT_field_size=NaN(9,size(ms3_SCs,1));LT_field_num=NaN(9,size(ms3_SCs,1));LT_coverage=NaN(9,size(ms3_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms3_SCs,1));OF_field_num=NaN(9,size(ms3_SCs,1));OF_coverage=NaN(9,size(ms3_SCs,1));
        elseif select_mouse==4;
        LT_field_size=NaN(9,size(ms4_SCs,1));LT_field_num=NaN(9,size(ms4_SCs,1));LT_coverage=NaN(9,size(ms4_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms4_SCs,1));OF_field_num=NaN(9,size(ms4_SCs,1));OF_coverage=NaN(9,size(ms4_SCs,1));
    end;
    
    
    
All_LT_data=NaN(9,69,length(setofcells));
All_OF_data=NaN(24,24,9,length(setofcells));
for cellnum=1:length(setofcells);

    raw_placemapOF=zeros(24,24,9);norm_placemapOF=zeros(24,24,9);
    raw_placemapLT=zeros(1,69,9);norm_placemapLT=zeros(1,69,9);

for day_num=1:9;
    day_var=sprintf('day%d', day_num);
    if select_mouse==1;
        OF_path_var=strcat(ms1string,day_var,ofstring);
        LT_path_var=strcat(ms1string,day_var,ltstring);
        elseif select_mouse==2;
        OF_path_var=strcat(ms2string,day_var,ofstring);
        LT_path_var=strcat(ms2string,day_var,ltstring);
        elseif select_mouse==3;
        OF_path_var=strcat(ms3string,day_var,ofstring);
        LT_path_var=strcat(ms3string,day_var,ltstring);
        elseif select_mouse==4;
        OF_path_var=strcat(ms4string,day_var,ofstring);
        LT_path_var=strcat(ms4string,day_var,ltstring);
end;

    Ms1OF = readtable(OF_path_var);


    %Open Field
    Ms1OF = readtable(OF_path_var);
    x_posn=table2array(Ms1OF(:,2));
    x_posn=smooth(x_posn,15);
    y_posn=table2array(Ms1OF(:,3));
    y_posn=smooth(y_posn,15);
    Ms1OF=sparse(table2array(Ms1OF(1:end,6:end)));
    
    vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
    vel_cmsec=smooth(vel_cmsec,30);
    
    still_periods=find(vel_cmsec<vel_threshold);
    vel_cmsec(still_periods)=NaN;
    Ms1OF(still_periods,:)=NaN;
    x_posn(still_periods)=NaN;y_posn(still_periods)=NaN;
    
    [Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    Occupancy=Occupancy/20;
    
    temp_cell=Ms1OF(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapOF(:,:,day_num),XEDGES,YEDGES] = histcounts2(x_posn(spike_indxs),y_posn(spike_indxs),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    norm_placemapOF(:,:,day_num)=raw_placemapOF(:,:,day_num)./Occupancy;
    norm_placemapOF(:,:,day_num)=imgaussfilt(norm_placemapOF(:,:,day_num),1);
    raw_placemapOF(isnan(raw_placemapOF))=0;norm_placemapOF(isnan(norm_placemapOF))=0;
    
    
    %Linear Track
    MsLT = readtable(LT_path_var);
    
    LT_xposn=MsLT.Var4;
    %LT_yposn=MsLT.Var3;
     
    LT_vel_cmsec=abs(diff(LT_xposn))*(1/0.05);
    LT_vel_cmsec=smooth(LT_vel_cmsec,30);
    
    
    Ms1LT=sparse(table2array(MsLT(1:end,6:end)));
    LT_still_periods=find(LT_vel_cmsec<LT_vel_threshold);
    LT_vel_cmsec(LT_still_periods)=NaN;
    Ms1LT(LT_still_periods,:)=NaN;
    LT_xposn(LT_still_periods)=NaN;%LT_yposn(LT_still_periods)=[];
    
    [LT_Occupancy,XEDGES,YEDGES] = histcounts(LT_xposn,linspace(1,70,LT_num_bins));
    LT_Occupancy=LT_Occupancy/20;
    
    temp_cell=Ms1LT(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapLT(:,:,day_num),XEDGES,YEDGES] = histcounts(LT_xposn(spike_indxs),linspace(1,70,LT_num_bins));
    norm_placemapLT(:,:,day_num)=raw_placemapLT(:,:,day_num)./LT_Occupancy;
    norm_placemapLT(:,:,day_num)=smooth(norm_placemapLT(:,:,day_num),5);
    norm_placemapLT(isnan(norm_placemapLT))=0;raw_placemapLT(isnan(raw_placemapLT))=0;

end;

All_LT_data(:,:,cellnum)=cat(1,norm_placemapLT(:,:,1),norm_placemapLT(:,:,2),norm_placemapLT(:,:,3),norm_placemapLT(:,:,4),norm_placemapLT(:,:,5),norm_placemapLT(:,:,6),norm_placemapLT(:,:,7),norm_placemapLT(:,:,8),norm_placemapLT(:,:,9));
All_OF_data(:,:,:,cellnum)=norm_placemapOF;

if make_plots==1;
    figure;

    for fig_num=1:9;
    subplot(9,1,fig_num);
    imagesc(norm_placemapLT(:,:,fig_num));colormap jet;
    if max(max(norm_placemapLT))>0;
    clim([0 max(max(norm_placemapLT))]);
    else
        clim([0 1]);
    end;
    end;

    figure;
    for fig_num=1:9;
    subplot(1,9,fig_num)
    imagesc(norm_placemapOF(:,:,fig_num));axis square;colormap jet;
    if max(max(max(norm_placemapOF)))>0;
    clim([0 max(max(max(norm_placemapOF)))]);
    else
        clim([0 1]);
    end;
    end;
end;

end

All_LT_data_ms3=All_LT_data;
All_OF_data_ms3=All_OF_data;


load('ContextDecodingDay9.mat')
clearvars -except idxs_of_SC_Cells All_LT_data_ms1 All_OF_data_ms1 All_LT_data_ms2 All_OF_data_ms2 All_LT_data_ms3 All_OF_data_ms3
ms1_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells<436);
ms2_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435 & idxs_of_SC_Cells<=435+279);
ms2_SCs=ms2_SCs-(435);
ms3_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279 & idxs_of_SC_Cells<=435+279+304);
ms3_SCs=ms3_SCs-(435+279);
ms4_SCs=idxs_of_SC_Cells(idxs_of_SC_Cells>435+279+304 & idxs_of_SC_Cells<=435+279+304+306);
ms4_SCs=ms4_SCs-(435+279+304);

num_bins=25;
vel_threshold=4;
LT_vel_threshold=0.1;
LT_num_bins=70;
make_plots=0;
select_mouse=4;

ms1string='D:/ACC Data/ACC/AllDays/635/';
ms2string='D:/ACC Data/ACC/AllDays/681/';
ms3string='D:/ACC Data/ACC/AllDays/691/';
ms4string='D:/ACC Data/ACC/AllDays/747/';
ltstring='/XYtotalspike3.csv';
ofstring='/XYtotalspike2.csv';

if select_mouse==1;
        setofcells=ms1_SCs;
        elseif select_mouse==2;
        setofcells=ms2_SCs;
        elseif select_mouse==3;
        setofcells=ms3_SCs;
        elseif select_mouse==4;
        setofcells=ms4_SCs;
end;


%idxs_of_SC_Cells=sort(idxs_of_SC_Cells,'ascend');


    if select_mouse==1;
        LT_field_size=NaN(9,size(ms1_SCs,1));LT_field_num=NaN(9,size(ms1_SCs,1));LT_coverage=NaN(9,size(ms1_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms1_SCs,1));OF_field_num=NaN(9,size(ms1_SCs,1));OF_coverage=NaN(9,size(ms1_SCs,1));
        elseif select_mouse==2;
        LT_field_size=NaN(9,size(ms2_SCs,1));LT_field_num=NaN(9,size(ms2_SCs,1));LT_coverage=NaN(9,size(ms2_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms2_SCs,1));OF_field_num=NaN(9,size(ms2_SCs,1));OF_coverage=NaN(9,size(ms2_SCs,1));
        elseif select_mouse==3;
        LT_field_size=NaN(9,size(ms3_SCs,1));LT_field_num=NaN(9,size(ms3_SCs,1));LT_coverage=NaN(9,size(ms3_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms3_SCs,1));OF_field_num=NaN(9,size(ms3_SCs,1));OF_coverage=NaN(9,size(ms3_SCs,1));
        elseif select_mouse==4;
        LT_field_size=NaN(9,size(ms4_SCs,1));LT_field_num=NaN(9,size(ms4_SCs,1));LT_coverage=NaN(9,size(ms4_SCs,1));
        OF_field_size_in_cm=NaN(9,size(ms4_SCs,1));OF_field_num=NaN(9,size(ms4_SCs,1));OF_coverage=NaN(9,size(ms4_SCs,1));
    end;
    
    
    
All_LT_data=NaN(9,69,length(setofcells));
All_OF_data=NaN(24,24,9,length(setofcells));
for cellnum=1:length(setofcells);

    raw_placemapOF=zeros(24,24,9);norm_placemapOF=zeros(24,24,9);
    raw_placemapLT=zeros(1,69,9);norm_placemapLT=zeros(1,69,9);

for day_num=1:9;
    day_var=sprintf('day%d', day_num);
    if select_mouse==1;
        OF_path_var=strcat(ms1string,day_var,ofstring);
        LT_path_var=strcat(ms1string,day_var,ltstring);
        elseif select_mouse==2;
        OF_path_var=strcat(ms2string,day_var,ofstring);
        LT_path_var=strcat(ms2string,day_var,ltstring);
        elseif select_mouse==3;
        OF_path_var=strcat(ms3string,day_var,ofstring);
        LT_path_var=strcat(ms3string,day_var,ltstring);
        elseif select_mouse==4;
        OF_path_var=strcat(ms4string,day_var,ofstring);
        LT_path_var=strcat(ms4string,day_var,ltstring);
end;

    Ms1OF = readtable(OF_path_var);


    %Open Field
    Ms1OF = readtable(OF_path_var);
    x_posn=table2array(Ms1OF(:,2));
    x_posn=smooth(x_posn,15);
    y_posn=table2array(Ms1OF(:,3));
    y_posn=smooth(y_posn,15);
    Ms1OF=sparse(table2array(Ms1OF(1:end,6:end)));
    
    vel_cmsec=abs(diff(x_posn))+abs(diff(y_posn))*(1/0.05);
    vel_cmsec=smooth(vel_cmsec,30);
    
    still_periods=find(vel_cmsec<vel_threshold);
    vel_cmsec(still_periods)=NaN;
    Ms1OF(still_periods,:)=NaN;
    x_posn(still_periods)=NaN;y_posn(still_periods)=NaN;
    
    [Occupancy,XEDGES,YEDGES] = histcounts2(x_posn,y_posn,linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    Occupancy=Occupancy/20;
    
    temp_cell=Ms1OF(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapOF(:,:,day_num),XEDGES,YEDGES] = histcounts2(x_posn(spike_indxs),y_posn(spike_indxs),linspace(min(x_posn),max(x_posn),num_bins),linspace(min(y_posn),max(y_posn),num_bins));
    norm_placemapOF(:,:,day_num)=raw_placemapOF(:,:,day_num)./Occupancy;
    norm_placemapOF(:,:,day_num)=imgaussfilt(norm_placemapOF(:,:,day_num),1);
    raw_placemapOF(isnan(raw_placemapOF))=0;norm_placemapOF(isnan(norm_placemapOF))=0;
    
    
    %Linear Track
    MsLT = readtable(LT_path_var);
    
    LT_xposn=MsLT.Var4;
    %LT_yposn=MsLT.Var3;
     
    LT_vel_cmsec=abs(diff(LT_xposn))*(1/0.05);
    LT_vel_cmsec=smooth(LT_vel_cmsec,30);
    
    
    Ms1LT=sparse(table2array(MsLT(1:end,6:end)));
    LT_still_periods=find(LT_vel_cmsec<LT_vel_threshold);
    LT_vel_cmsec(LT_still_periods)=NaN;
    Ms1LT(LT_still_periods,:)=NaN;
    LT_xposn(LT_still_periods)=NaN;%LT_yposn(LT_still_periods)=[];
    
    [LT_Occupancy,XEDGES,YEDGES] = histcounts(LT_xposn,linspace(1,70,LT_num_bins));
    LT_Occupancy=LT_Occupancy/20;
    
    temp_cell=Ms1LT(:,setofcells(cellnum));
    spike_indxs=find(temp_cell>0);
    
    [raw_placemapLT(:,:,day_num),XEDGES,YEDGES] = histcounts(LT_xposn(spike_indxs),linspace(1,70,LT_num_bins));
    norm_placemapLT(:,:,day_num)=raw_placemapLT(:,:,day_num)./LT_Occupancy;
    norm_placemapLT(:,:,day_num)=smooth(norm_placemapLT(:,:,day_num),5);
    norm_placemapLT(isnan(norm_placemapLT))=0;raw_placemapLT(isnan(raw_placemapLT))=0;

end;

All_LT_data(:,:,cellnum)=cat(1,norm_placemapLT(:,:,1),norm_placemapLT(:,:,2),norm_placemapLT(:,:,3),norm_placemapLT(:,:,4),norm_placemapLT(:,:,5),norm_placemapLT(:,:,6),norm_placemapLT(:,:,7),norm_placemapLT(:,:,8),norm_placemapLT(:,:,9));
All_OF_data(:,:,:,cellnum)=norm_placemapOF;

if make_plots==1;
    figure;

    for fig_num=1:9;
    subplot(9,1,fig_num);
    imagesc(norm_placemapLT(:,:,fig_num));colormap jet;
    if max(max(norm_placemapLT))>0;
    clim([0 max(max(norm_placemapLT))]);
    else
        clim([0 1]);
    end;
    end;

    figure;
    for fig_num=1:9;
    subplot(1,9,fig_num)
    imagesc(norm_placemapOF(:,:,fig_num));axis square;colormap jet;
    if max(max(max(norm_placemapOF)))>0;
    clim([0 max(max(max(norm_placemapOF)))]);
    else
        clim([0 1]);
    end;
    end;
end;

end

All_LT_data_ms4=All_LT_data;
All_OF_data_ms4=All_OF_data;

clearvars -except All_LT_data_ms1 All_OF_data_ms1 All_LT_data_ms2 All_OF_data_ms2 ...
    All_LT_data_ms3 All_OF_data_ms3 All_LT_data_ms4 All_OF_data_ms4


save 'SCCellMaps.mat'




mouseLT=All_LT_data_ms4;
mouseOF=All_OF_data_ms4;

for cellnum=1:size(All_LT_data_ms4,3);

    figure(1);
    for fig_num=1:9;
    subplot(9,1,fig_num);
    imagesc(mouseLT(fig_num,:,cellnum));colormap jet;
    if max(max(mouseLT(fig_num,:,cellnum)))>0;
    caxis([0 max(max(mouseLT(fig_num,:,cellnum)))]);
    else
        caxis([0 1]);
    end;
    end;
    saveas(gcf,['LTmaps_cell' num2str(cellnum) '.eps']);
    close all;

    figure(2);
    for fig_num=1:9;
    subplot(1,9,fig_num)
    imagesc(mouseOF(:,:,fig_num,cellnum));axis square;colormap jet;
    if max(max(max(mouseOF(:,:,fig_num,cellnum))))>0;
    caxis([0 max(max(max(mouseOF(:,:,fig_num,cellnum))))]);
    else
        caxis([0 1]);
    end;

    end;
    saveas(gcf,['OFmaps_cell' num2str(cellnum) '.eps']);
    close all;

end;




% 
% 
% mouseLT=All_LT_data_ms4;
% mouseOF=All_OF_data_ms4;
% 
% for cellnum=1:size(All_LT_data_ms4,3);
%     figure;
%     cmxlt=max(max(mouseLT(:,:,cellnum)));
%     cmxof=max(max(max(mouseOF(:,:,:,cellnum))));
% 
% 
%     a = subplot(9,2,2); imagesc(mouseLT(1,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     b = subplot(9,2,4); imagesc(mouseLT(2,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     c = subplot(9,2,6); imagesc(mouseLT(3,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     d = subplot(9,2,8); imagesc(mouseLT(4,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     e = subplot(9,2,10); imagesc(mouseLT(5,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     f = subplot(9,2,12); imagesc(mouseLT(6,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     g = subplot(9,2,14); imagesc(mouseLT(7,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     h = subplot(9,2,16); imagesc(mouseLT(8,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     i = subplot(9,2,18); imagesc(mouseLT(9,:,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     a2 = subplot(9,2,1); imagesc(mouseOF(:,:,1,cellnum));colormap jet;axis off;caxis([0 cmxlt]);%scaled to LT
%     b2 = subplot(9,2,3); imagesc(mouseOF(:,:,2,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     c2 = subplot(9,2,5); imagesc(mouseOF(:,:,3,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     d2 = subplot(9,2,7); imagesc(mouseOF(:,:,4,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     e2 = subplot(9,2,9); imagesc(mouseOF(:,:,5,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     f2 = subplot(9,2,11); imagesc(mouseOF(:,:,6,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     g2 = subplot(9,2,13); imagesc(mouseOF(:,:,7,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     h2 = subplot(9,2,15); imagesc(mouseOF(:,:,8,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     i2 = subplot(9,2,17); imagesc(mouseOF(:,:,9,cellnum));colormap jet;axis off;caxis([0 cmxlt]);
%     set(a, 'Position', [0.25 0.91 .7 .08]);
%     set(b, 'Position', [0.25 0.81 .7 .08]);
%     set(c, 'Position', [0.25 0.71 .7 .08]);
%     set(d, 'Position', [0.25 0.61 .7 .08]);
%     set(e, 'Position', [0.25 0.51 .7 .08]);
%     set(f, 'Position', [0.25 0.41 .7 .08]);
%     set(g, 'Position', [0.25 0.31 .7 .08]);
%     set(h, 'Position', [0.25 0.21 .7 .08]);
%     set(i, 'Position', [0.25 0.11 .7 .08]);
% 
%     set(a2, 'Position', [0.1 0.91 .08 .08]);
%     set(b2, 'Position', [0.1 0.81 .08 .08]);
%     set(c2, 'Position', [0.1 0.71 .08 .08]);
%     set(d2, 'Position', [0.1 0.61 .08 .08]);
%     set(e2, 'Position', [0.1 0.51 .08 .08]);
%     set(f2, 'Position', [0.1 0.41 .08 .08]);
%     set(g2, 'Position', [0.1 0.31 .08 .08]);
%     set(h2, 'Position', [0.1 0.21 .08 .08]);
%     set(i2, 'Position', [0.1 0.11 .08 .08]);axis off;
% 
% 
%     saveas(gcf,['SCmaps_cell' num2str(cellnum) '.png']);
%     close all;

end;

