%This code will take multiple databases of pointers and create a single one


%Input: all data bases (mat)
%Output: single data base

clear;

%Ask the user for all the files that contain pointer information
[file, path] = ...
    uigetfile('*.mat','Select all mat files that have your data base pointers...', 'Z:\DataBase','MultiSelect','on');

if iscell(file)
    NumFiles = length(file);
    for i=1:NumFiles
        temp = file{i};
        FileIndex{i} = temp;
    end
else
    NumFiles = 1;
    FileIndex{1} = file;
end

%Load each database and compile them
Y = zeros(NumFiles+1,1);

for count=1:NumFiles
    %Determine the prevous number of total lines
    Y_tot = sum(Y);
    %Get filename
    filename = FileIndex{count};
    path_and_filename = [path filename];
    filename2 = path_and_filename
    %load the file
    load(filename2)
    
    if exist('Data_Loc_Set')
        %Determine the number in this set
        [X,Y(count)] = size(Data_Loc_Set);
        Data_Loc_Comp_temp((Y_tot+1):(Y_tot+Y(count))) = Data_Loc_Set;
        clear Data_Loc_Set
    elseif exist('Data_Loc_Comp')
        [X,Y(count)] = size(Data_Loc_Comp);
        Data_Loc_Comp_temp((Y_tot+1):(Y_tot+Y(count))) = Data_Loc_Comp;
        clear Data_Loc_Comp
    end
end

Data_Loc_Comp = Data_Loc_Comp_temp;

%Ask the user to pick an output directory
path_base = path;

%Generate a time stamp for the file name
c=clock;
cc = fix(c);
time_stamp = [num2str(cc(1)) '_' num2str(cc(2)) '_' num2str(cc(3)) '_' num2str(cc(4)) '_' num2str(cc(5))];
%Save a mat file of the data
filename_data_out = [path_base '\DataLocation_Comp_' time_stamp '.mat'];
save(filename_data_out,'Data_Loc_Comp');

%save the talbe as a csv file for visual checks
T = struct2table(Data_Loc_Comp);
filename_table_out = [path_base '\DataLocation_Comp_' time_stamp '.csv'];
writetable(T,filename_table_out);
