%-------------------------------------------------------------------------------
%
%  David S. De Lorenzo
%  August 09, 2018
%
%  Concatenate the matrix output producted by viewdat.exe
%
%-------------------------------------------------------------------------------

clear

path_name = 'C:\DATA\T04_Files\fir_sweep_test_2023_0705\RX1-1401\rec35_sub19';

save_prefix = 'auger';
% save_prefix = 'alloy_titan';

COMPRESS_REC35_SUB19 = true;

suffix_list = {; ...
               'rec35_sub19'; ...
               % 'rec35_sub24'; ...
               % 'rec35_sub25'; ...
               % 'rec35_sub28'; ...
               % 'rec35_sub29'; ...
               % 'rec35_sub30'; ...
               % 'rec35_sub31'; ...
               };

fprintf('\nConcatenating the matrix output producted by viewdat.exe\n\n');

%% --- No changes should be required below this line -----------------------

% Loop over suffixes
for jj = 1:length(suffix_list)
    
    suffix = suffix_list{jj};
    save_name = [save_prefix '_' suffix];
    
    d = dir(fullfile(path_name, ['*_' suffix '.mat']));
    file_list = sort({d.name}.');
    
    data_all = [];
    for ii = 1:length(file_list)
        file_name = file_list{ii};
        fprintf('    %s\n', file_name);
        load(fullfile(path_name, file_name), 'data');
        data_all = [data_all, data];                                 %#ok<AGROW>
    end
    data = data_all;
    clear('data_all');
    
    if strcmp(suffix, 'rec35_sub19')
        if exist('COMPRESS_REC35_SUB19','var') && (COMPRESS_REC35_SUB19 == true)
            data([1 3 4 6 7 9 10 14 15 16],:) = NaN;
        end
    end
    
    if (whos('data').bytes > 2^31)
        save(fullfile(path_name, save_name), 'data', '-v7.3');
    else
        save(fullfile(path_name, save_name), 'data');
    end
    
end

%% --- ALL DONE ------------------------------------------------------------

fprintf('\n  --> ALL DONE!!\n\n');

%%
return
%-------------------------------------------------------------------------------