
function MarinPlusss(userid,modality)


% MarinPlus brings in specific files recursively from the current directory
% and below and then concatenates those files into one big csv for a
% specific modality - a list of those files is saved to the global workspace.
% Then it creates two variables in the global workspace that
% represent the time variable in datevec (mtime) and the stream variable
% (mstream)
% 
% This script can only handle 1 modality at a time and will combine over
% all days found below the pwd. 
%
% userid = s + number = Ex. 's20'
% modality refers to the modality listed in file you want to concatenate =
% Ex. 'BHAcc'
% Ex 
%
% Modality list (taken directly from filename) = BHAccXYZ, BHECG,
% BHECGSamples, BHHeartRate, BHPeakAcc, BHPosture, BHR2R, BHRespRate,
% BHRespWaveform, QSensorData, BodyDyn
%
%
%
%
fname = sprintf('*%s*%s*.csv',userid, modality);
%fname stands in for varargin from subdir.m/bar/barleyhome/tdoty/Desktopleyhome/tdoty/Desktop
%filelistsorted stands in for varargout


 

%%---------------------------
% Get folder and filter
%---------------------------
if nargin == 0
    folder = pwd;
    filter = '*';
else

    [folder, name, ext] = fileparts(fname);
    if isempty(folder)
        folder = pwd;
    end
    if isempty(ext)
        if isdir(fullfile(folder, name))
            folder = fullfile(folder, name);
            filter = '*';
        end
    else
        filter = [name ext];
    end
    
end
%%
%---------------------------
% Search all folders
%---------------------------

pathstr = genpath(folder);
seplocs = findstr(pathstr, pathsep);
loc1 = [1 seplocs(1:end-1)+1];
loc2 = seplocs(1:end)-1;
pathfolders = arrayfun(@(a,b) pathstr(a:b), loc1, loc2, 'UniformOutput', false);

Files = [];
for ifolder = 1:length(pathfolders)
    NewFiles = dir(fullfile(pathfolders{ifolder}, filter));
    if ~isempty(NewFiles)
        fullnames = cellfun(@(a) fullfile(pathfolders{ifolder}, a), {NewFiles.name}, 'UniformOutput', false); 
        [NewFiles.name] = deal(fullnames{:});
        Files = [Files; NewFiles];
    end
end

%%
% Concatenate that shit
%
% Outputs a variable called user_modalityFilesUsed that will let you know which files went
% into the concatenated dataset
%
% Concatenated dataset goes into a file called userid_modalityALL.csv
%
for i = 1:length(Files)
  string = sprintf('cat %s >> %s_%sALL.csv', Files(i).name, userid, modality); unix(string); 
end

FileListFile = sprintf('%s_%sFilesUsed',userid, modality);
assignin('base',FileListFile,Files);

%%  MOVE DATA TO WORKSPACE
% Now read in the the concatenated dataset 
% Column for datetime (into a datevec) and the actual sensor data
% and put it into the workspace
% and save the workspace outside of the function
%
% Will need separate values for each modality - set up an if then loop
%

 %% Import data from text file.
% Script for importing data from the following text file:
%
%    /barleyhome/tdoty/Concatplay/061214/s20/s20_BHHeartRateALL.csv
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2014/07/29 18:41:11

%% Initialize variables.
%filename = '/barleyhome/tdoty/Concatplay/061214/s20/s20_BHHeartRateALL.csv';

filename = sprintf('%s_%sALL.csv',userid, modality);
delimiter = ',';

%% Set up the if/then loop based upon modality

easy4 = ('easy BH streams');
harder = ('XYZ Acc under dev');
harderer = ('ECG Samples from 1 to 63 under dev');
hardest = ('Damn Q sensor under dev');

need4 = strcmpi(modality,{'BHECG';'BHHeartRate';'BHPeakAcc';'BHPosture';'BHR2R';'BHRespRate'; 'BHRespWaveform'});
need456 = strcmpi(modality,'BHAccXYZ');
need163 = strcmpi(modality,'BHECGSamples');
need6811 = strcmpi(modality,'QSensorData');
need4th9 = strcmpi(modality,'BodyDyn');


%% If the modality uses column 4 - i.e. most of the BH data
%
if sum(need4) == 1
    %% Read columns of data as strings:
    % For more information, see the TEXTSCAN documentation.
    formatSpec = '%*s%s%*s%s%[^\n\r]';

    %% Open the text file.
    fileID = fopen(filename,'r');

    %% Read columns of data according to format string.
    % This call is based on the structure of the file used to generate this
    % code. If an error occurs for a different file, try regenerating the code
    % from the Import Tool.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

    %% Close the text file.
    fclose(fileID);

    %% Convert the contents of columns containing numeric strings to numbers.
    % Replace non-numeric strings with NaN.
    raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
    for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
    end
    numericData = NaN(size(dataArray{1},1),size(dataArray,2));

    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{2};
    for row=1:size(rawData, 1);
    % Create a regular expression to detect and remove non-numeric prefixes and
    % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
        result = regexp(rawData{row}, regexstr, 'names');
        numbers = result.numbers;
        
        % Detected commas in non-thousand locations.
        invalidThousandsSeparator = false;
        if any(numbers==',');
            thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
            if isempty(regexp(thousandsRegExp, ',', 'once'));
                numbers = NaN;
                invalidThousandsSeparator = true;
            end
        end
        % Convert numeric strings to numbers.
        if ~invalidThousandsSeparator;
            numbers = textscan(strrep(numbers, ',', ''), '%f');
            numericData(row, 2) = numbers{1};
            raw{row, 2} = numbers{1};
        end
    catch me
        end
    end

% Convert the contents of column with dates to serial date numbers using
% date format string (datenum).
    for row=1:length(dataArray{1})
        try
        numericData(row, 1) = datenum(dataArray{1}{row}, 'yymmddHHMMSSFFF');
        raw{row, 1} = numericData(row, 1);
        catch me
        end
    end


    %% Exclude rows with non-numeric cells
    J = ~all(cellfun(@(x) (isnumeric(x) || islogical(x)) && ~isnan(x),raw),2); % Find rows with non-numeric cells
    raw(J,:) = [];

    %% Allocate imported array to column variable names made with the user id and modality
    mtime = cell2mat(raw(:, 1));
    newnametime = sprintf('%s_%sTime',userid, modality);
    assignin('caller',newnametime,mtime)

    mstream = cell2mat(raw(:, 2));
    newnamestream = sprintf('%s_%s',userid, modality);
    assignin('caller',newnamestream,mstream)

    %mtime = sprintf('%s_%sTime',userid, modality);
    %mstream = sprintf('%s_%s',userid, modality);

    %% Clear temporary variables
    clearvars filename mstream mtime delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me J;




%% If the modality uses columns 456 - i.e. BHAccXYZ
%
elseif sum(need456) == 1
     %% Read columns of data as strings:
    % For more information, see the TEXTSCAN documentation.
    formatSpec = '%*s%s%*s%s%s%s%[^\n\r]';

    %% Open the text file.
    fileID = fopen(filename,'r');

    %% Read columns of data according to format string.
    % This call is based on the structure of the file used to generate this
    % code. If an error occurs for a different file, try regenerating the code
    % from the Import Tool.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

    %% Close the text file.
    fclose(fileID);

    %% Convert the contents of columns containing numeric strings to numbers.
    % Replace non-numeric strings with NaN.
    raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
    for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
    end
    numericData = NaN(size(dataArray{1},1),size(dataArray,2));

    for col=[2,3,4]
    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{col};
    for row=1:size(rawData, 1);
        % Create a regular expression to detect and remove non-numeric prefixes and
        % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
            result = regexp(rawData{row}, regexstr, 'names');
            numbers = result.numbers;
            
            % Detected commas in non-thousand locations.
            invalidThousandsSeparator = false;
            if any(numbers==',');
                thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
                if isempty(regexp(thousandsRegExp, ',', 'once'));
                    numbers = NaN;
                    invalidThousandsSeparator = true;
                end
            end
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end

% Convert the contents of column with dates to serial date numbers using
% date format string (datenum).
    for row=1:length(dataArray{1})
        try
        numericData(row, 1) = datenum(dataArray{1}{row}, 'yymmddHHMMSSFFF');
        raw{row, 1} = numericData(row, 1);
        catch me
        end
    end


    %% Exclude rows with non-numeric cells
    J = ~all(cellfun(@(x) (isnumeric(x) || islogical(x)) && ~isnan(x),raw),2); % Find rows with non-numeric cells
    raw(J,:) = [];

    %% Allocate imported array to column variable names made with the user id and modality
    mtime = cell2mat(raw(:, 1));
    newnametime = sprintf('%s_%sTime',userid, modality);
    assignin('caller',newnametime,mtime)

    mstreamx = cell2mat(raw(:, 2));
    newnamestreamx = sprintf('%s_%s_Xonly',userid, modality);
    assignin('caller',newnamestreamx,mstreamx)

    mstreamy = cell2mat(raw(:, 3));
    newnamestreamy = sprintf('%s_%s_Yonly',userid, modality);
    assignin('caller',newnamestreamy,mstreamy)

    mstreamz = cell2mat(raw(:, 4));
    newnamestreamz = sprintf('%s_%s_Zonly',userid, modality);
    assignin('caller',newnamestreamz,mstreamz)

    
    %mtime = sprintf('%s_%sTime',userid, modality);
    %mstream = sprintf('%s_%s',userid, modality);

    %% Clear temporary variables
    clearvars filename mstream mtime delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me J;



    
    
%% If the modality  uses columns 1-63 - i.e. BHECGSamples
%
elseif sum(need163) == 1
    disp(harderer)
    
    
%% if the modality uses columns 6,7,8, and 11 - i.e. Q Sensor Data
elseif sum(need6811) == 1 
    %% Read columns of data as strings:
    % For more information, see the TEXTSCAN documentation.
    formatSpec = '%*s%s%*s%*s%*s%s%s%s%*s%*s%s%[^\n\r]';

    %% Open the text file.
    fileID = fopen(filename,'r');

    %% Read columns of data according to format string.
    % This call is based on the structure of the file used to generate this
    % code. If an error occurs for a different file, try regenerating the code
    % from the Import Tool.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

    %% Close the text file.
    fclose(fileID);

    %% Convert the contents of columns containing numeric strings to numbers.
    % Replace non-numeric strings with NaN.
    raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
    for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
    end
    numericData = NaN(size(dataArray{1},1),size(dataArray,2));

    for col=[2,3,4,5]
    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{col};
    for row=1:size(rawData, 1);
        % Create a regular expression to detect and remove non-numeric prefixes and
        % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
            result = regexp(rawData{row}, regexstr, 'names');
            numbers = result.numbers;
            
            % Detected commas in non-thousand locations.
            invalidThousandsSeparator = false;
            if any(numbers==',');
                thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
                if isempty(regexp(thousandsRegExp, ',', 'once'));
                    numbers = NaN;
                    invalidThousandsSeparator = true;
                end
            end
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end

% Convert the contents of column with dates to serial date numbers using
% date format string (datenum).
    for row=1:length(dataArray{1})
        try
        numericData(row, 1) = datenum(dataArray{1}{row}, 'yymmddHHMMSSFFF');
        raw{row, 1} = numericData(row, 1);
        catch me
        end
    end


    %% Exclude rows with non-numeric cells
    J = ~all(cellfun(@(x) (isnumeric(x) || islogical(x)) && ~isnan(x),raw),2); % Find rows with non-numeric cells
    raw(J,:) = [];

    %% Allocate imported array to column variable names made with the user id and modality
    mtime = cell2mat(raw(:, 1));
    newnametime = sprintf('%s_%sTime',userid, modality);
    assignin('caller',newnametime,mtime)

    mstreameda = cell2mat(raw(:, 5));
    newnamestreameda = sprintf('%s_%s_EDA',userid, modality);
    assignin('caller',newnamestreameda,mstreameda)

    mstreamx = cell2mat(raw(:, 4));
    newnamestreamx = sprintf('%s_%s_Xonly',userid, modality);
    assignin('caller',newnamestreamx,mstreamx)

    mstreamy = cell2mat(raw(:, 3));
    newnamestreamy = sprintf('%s_%s_Yonly',userid, modality);
    assignin('caller',newnamestreamy,mstreamy)

    mstreamz = cell2mat(raw(:, 2));
    newnamestreamz = sprintf('%s_%s_Zonly',userid, modality);
    assignin('caller',newnamestreamz,mstreamz)

    
    %mtime = sprintf('%s_%sTime',userid, modality);
    %mstream = sprintf('%s_%s',userid, modality);

    %% Clear temporary variables
    clearvars filename mstream mtime delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me J;

%% If the modality uses columns 4 through 9 - i.e. BodyDyn
%
elseif sum(need4th9) == 1
     %% Read columns of data as strings:
    % For more information, see the TEXTSCAN documentation.
    formatSpec = '%*s%s%*s%s%s%s%s%s%s%[^\n\r]';

    %% Open the text file.
    fileID = fopen(filename,'r');

    %% Read columns of data according to format string.
    % This call is based on the structure of the file used to generate this
    % code. If an error occurs for a different file, try regenerating the code
    % from the Import Tool.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

    %% Close the text file.
    fclose(fileID);

    %% Convert the contents of columns containing numeric strings to numbers.
    % Replace non-numeric strings with NaN.
    raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
    for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
    end
    numericData = NaN(size(dataArray{1},1),size(dataArray,2));

    for col=[2,3,4,5,6,7]
    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{col};
    for row=1:size(rawData, 1);
        % Create a regular expression to detect and remove non-numeric prefixes and
        % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
            result = regexp(rawData{row}, regexstr, 'names');
            numbers = result.numbers;
            
            % Detected commas in non-thousand locations.
            invalidThousandsSeparator = false;
            if any(numbers==',');
                thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
                if isempty(regexp(thousandsRegExp, ',', 'once'));
                    numbers = NaN;
                    invalidThousandsSeparator = true;
                end
            end
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end

% Convert the contents of column with dates to serial date numbers using
% date format string (datenum).
    for row=1:length(dataArray{1})
        try
        numericData(row, 1) = datenum(dataArray{1}{row}, 'yymmddHHMMSSFFF');
        raw{row, 1} = numericData(row, 1);
        catch me
        end
    end


    %% Exclude rows with non-numeric cells
    J = ~all(cellfun(@(x) (isnumeric(x) || islogical(x)) && ~isnan(x),raw),2); % Find rows with non-numeric cells
    raw(J,:) = [];

    %% Allocate imported array to column variable names made with the user id and modality
    mtime = cell2mat(raw(:, 1));
    newnametime = sprintf('%s_%sTime',userid, modality);
    assignin('caller',newnametime,mtime)

    mstreamaccx = cell2mat(raw(:, 2));
    newnamestreamaccx = sprintf('%s_%s_accXonly',userid, modality);
    assignin('caller',newnamestreamaccx,mstreamaccx)

    mstreamaccy = cell2mat(raw(:, 3));
    newnamestreamaccy = sprintf('%s_%s_accYonly',userid, modality);
    assignin('caller',newnamestreamaccy,mstreamaccy)

    mstreamaccz = cell2mat(raw(:, 4));
    newnamestreamaccz = sprintf('%s_%s_accZonly',userid, modality);
    assignin('caller',newnamestreamaccz,mstreamaccz)

    mstreamgyrox = cell2mat(raw(:, 5));
    newnamestreamgyrox = sprintf('%s_%s_gyroXonly',userid, modality);
    assignin('caller',newnamestreamgyrox,mstreamgyrox)

    mstreamgyroy = cell2mat(raw(:, 6));
    newnamestreamgyroy = sprintf('%s_%s_gyroYonly',userid, modality);
    assignin('caller',newnamestreamgyroy,mstreamgyroy)

    mstreamgyroz = cell2mat(raw(:, 7));
    newnamestreamgyroz = sprintf('%s_%s_gyroZonly',userid, modality);
    assignin('caller',newnamestreamgyroz,mstreamgyroz)
    
    %mtime = sprintf('%s_%sTime',userid, modality);
    %mstream = sprintf('%s_%s',userid, modality);

    %% Clear temporary variables
    clearvars filename mstream mtime delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me J;





end


%% Save workspace

outputfile = sprintf('%s_%sComb',userid, modality);
%matlab.io.saveVariablesToScript(outputfile)
%save(outputfile)
save(outputfile)





   


end

%
%
%  Tracy Jill Doty 2014
%  tracy.doty@alumni.duke.edu
%
