classdef icme
    % icme inferior-colliculus-multielectrode-recording
    %   This object characterizes a single IC recording associated to an
    %   animal experiment. Each icme gets created based on a matlab log
    %   file, that is generated by ExpControl during the recording and
    %   needs an associated raw-data folder that is generated by cheetah..

    properties
        ExpID string % Exp/Animal ID eg. GEK111
        SeriesID string % icme ID eg. GEK111_0001
        ExpInfo struct % copied info from ExpControl log file
        Stim struct % Stimulus info and detailed stimulus list
        R struct % Raw data, currently kept empty
        EP struct % Electrode Probe, currently kept empty, electrode is saved with IC user Info
        SL struct % Detected MU spikes in the spikelist structure
        C struct % Calibrated stimulus list and calibration array
        D struct % Directory where the rawdata is stored.   
    end

    methods

        function obj = icme (ee, SeriesID, D)
            % initialize the object with the ExpID, SeriesID and RawDataDirectory
            obj.ExpID = ee.ExpID;
            obj.SeriesID = SeriesID;
            % check if we can get the D from the ee
            ix_D = find([ee.RawDataDir.type] == "IC");
            if ~isempty(ix_D)
                obj.D=ee.RawDataDir(ix_D);
            end
            % overwritewith other directory if given
            if nargin>2 && ~isempty(D)
                obj.D = D;
            end
        end

        function saveIcme(obj)
            % icme\saveIcme - stores the icme in PorcessedDataDir\ICME folder
            if  status_cache==0
                save_name = strcat("IC_",obj.ExpID,"_",obj.SeriesID,".mat");
                testSafeDir(save_name)
                IC = obj;
                IC.R=[]; %empty the raw data to save only the feather
                save(fullfile(expProcDataDir,'ICME','IC',save_name),'IC');
            end
        end
        

        function obj = loadIcme(obj)
            % icme\loadIcme - loads the icme data from the PorcessedDataDir
            load_name = strcat("IC_",obj.ExpID,"_",obj.SeriesID,".mat");
            LO = load(fullfile(expProcDataDir,'ICME','IC',load_name),'IC');%loaded object
            obj = LO.IC;
        end

        function obj = initIcme(obj)
            % icme\initIcme to be used for preprocessing.
           
            obj = loadLogFileInfo (obj);

            %obj = stim (obj);
            saveIcme(obj)
        end

        function obj = loadLogFileInfo (obj)
            % icme\loadLogFileInfo - loads the log file and stores the info in ICME properties
            % It loads the log file created in ExpControl from the
            % directory stored in property 'D' of the IMCE object, stores
            % everything in obj.ExpInfo, extracts the stimulus
            % information and stores it in obj.Stim

            load_name = strcat(obj.SeriesID,".m");
            fpath = fullfile(gen_dir_name(obj.D.dir), load_name);
            assert(isfile(fpath), 'File not found: %s', fpath);
            [fileDir, funcName, ext] = fileparts(fpath);
            assert(strcmpi(ext,'.m'), 'Expected an .m file: %s', fpath);
            
            % Temporarily move into folder so MATLAB resolves the right file
            oldDir = pwd;
            c = onCleanup(@() cd(oldDir));
            cd(fileDir);
            
            % Clear possible script/function caching conflicts
            clear(funcName);
            
            % Call as function and capture returned x
            obj.ExpInfo = feval(funcName);

            % already extract the Stim info from the ExpInfo
            obj(1).Stim(1).exp_type=obj.ExpInfo.exp_type; %MX_tones or OBISPulse etc
            obj(1).Stim(1).stimlist =obj.ExpInfo.stimlist; % descriptive table of the used stimuli
            % For creeLED10x1, check if 'stimlist' is a cell and convert it to double if needed
            if iscell(     obj(1).Stim(1).stimlist)
                % Iterate through the cell array and convert non-numeric entries to NaN
                for i = 1:numel(obj(1).Stim(1).stimlist)
                    if isempty(obj(1).Stim(1).stimlist{i}) || ~isnumeric(obj(1).Stim(1).stimlist{i})
                        obj(1).Stim(1).stimlist{i} = 0; % Replace empty or non-numeric cells with 0
                    end
                end
                % Now attempt to convert the cell array to a double
                obj(1).Stim(1).stimlist = cell2mat(obj(1).Stim(1).stimlist);
            end

            obj(1).Stim(1).stimheader = obj.ExpInfo.stimheader; % header of previous table
            obj(1).Stim(1).dura = obj.ExpInfo.dura; % duration of the full stimulus for each condition
            obj(1).Stim(1).n_rep= obj.ExpInfo.n_rep; % number of presentations per stimulus

            % find the raw data
            p_dat =   obj.ExpInfo.RawDataFolder{1}(strfind(  obj.ExpInfo.RawDataFolder{1},  obj.ExpInfo.animal_ID)+length(obj.ExpInfo.animal_ID)+1:end);
            if  isdir(fullfile(gen_dir_name(obj.D.dir), p_dat)) ==0
                msg = 'there was no Neuralynx data folder found for '+obj.SeriesID;
                error(msg)
            end
            rawFiles = dir(fullfile(gen_dir_name(obj.D.dir), p_dat, '*.ncs'));
            infoFiles = dir(fullfile(gen_dir_name(obj.D.dir), p_dat, 'CheetahLostADRecords.txt'));
            spikeFiles = dir(fullfile(gen_dir_name(obj.D.dir), p_dat,'*.nse'));
            eventFiles =dir(fullfile(gen_dir_name(obj.D.dir), p_dat,'*.nev'));
            %
            % get event info
            filename_evt = fullfile(eventFiles.folder,eventFiles.name);
            % [evt.TimeStamps, evt.EventIDs, evt.TTLs, evt.Extras, evt.EventStrings] = Nlx2MatCSC(rawFiles(5).name), [1 1 1 1 1], 0, 1, 0 );
            % [Timestamps, ChannelNumbers, SampleFrequencies, NumberOfValidSamples, Samples, Header] = Nlx2MatCSC(rawFiles(5).name), FieldSelectionFlags, ExtractHeader);

            [evt.TimeStamps, evt.EventIDs, evt.TTLs, evt.Extras, evt.EventStrings] = Nlx2MatEV(filename_evt, [1 1 1 1 1], 0, 1, 0 );
            obj.R(1).evt =evt;
            obj.R(1).p_nlx_raw_data_folder=p_dat;
            obj.R(1).rawFiles = rawFiles;
            obj.R(1).infoFiles = infoFiles;
            obj.R(1).spikeFiles = spikeFiles;
        end

        function obj = ExtractMUAfromRawDataIntoSL(obj,threshold,pre_time,post_time,ref_time,low_filt,high_filt,filt_ord,prestimrectime,poststimrectime,use_artefact_removal)
            % icme\ExtractMUAfromRawDataIntoSL extracts the the MUA spikes from raw data or loads them from the Resort file
            % to extract the spikes it calls generateSLfromRawNlxData_BackgroundNoise(IC),
            % the spikelist is saved in obj.SL
            % output
            %    obj (icme) IC recording with filled SL field
            % input  
            %   obj (ICME) IC recording to be analysed
            %   optional:
            %       threshold; double (default 3) to adjust spike threshold
            %       pre_time; post_time;  % waveform-window around a detected spike [ms]
            %       ref_time;    % refractory period [ms]
            %       low_filt, high_filt;filt_ord; % bandpass filter properties in beginning of data processing
            %       prestimrectime; poststimrectime % time around trigger that spikes
            %           are extracted from [ms]

            if nargin==1
                threshold = 4; % threshold for waveform extraction (in MAD)
                pre_time = 0.5;
                post_time = 1; % waveform-window
                ref_time = 1; % refractory period
                low_filt = 600;
                high_filt = 6000;
                filt_ord = 4; % filter settings
                % time in ms around trigger that the spikes are extracted
                prestimrectime = 150;
                poststimrectime= 150;
                use_artefact_removal=1;
            end
            
            
            % first test if enable cache is on and load any existing data
            % if possible, otherwise go for spikelist
            if strcmpi(enablecache,'on')  
                save_name = strcat(obj.ExpID,"_",obj.SeriesID,"_ICME_Resort.txt");
                if isfile(fullfile(expProcDataDir,'ICME','RESORT',save_name))
                    % read in file
                    text = string(fileread(fullfile(expProcDataDir,'ICME','RESORT',save_name)));
                    % Split the text into lines
                    lines = strsplit(text, '\n');
                    % Extract the experiment information
                    experiment_info = strsplit(lines{3},'\t');
                    if ~strcmp(obj.SeriesID, experiment_info{2})
                        error('trying to load spikelist from a file with the wrong Experiemnt ID')
                    end
                    % Extract the analysis parameters
                    analysis_params_line = find(contains(lines,'analysis parameters'));
                    fprintf('loading spikelist for %s with following analysis parameters: \n',obj.SeriesID)
                    fprintf('%s \n', lines{analysis_params_line+1:analysis_params_line+9})
                    helper=strsplit(lines{analysis_params_line+1},':');
                    used_analysis_parameters.threshold=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+2},':');
                    used_analysis_parameters.pre_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+3},':');
                    used_analysis_parameters.post_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+4},':');
                    used_analysis_parameters.ref_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+5},':');
                    used_analysis_parameters.low_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+6},':');
                    used_analysis_parameters.high_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+7},':');
                    used_analysis_parameters.filt_ord=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+8},':');
                    used_analysis_parameters.prestimrectime=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+9},':');
                    used_analysis_parameters.poststimrectime=str2num(helper{2});
                     % extract the spikelist
                    spikelist_line=find(contains(lines,'Resulting spikelist')); 
                    % Detect import options and specify the number of header lines to skip
                    import_opts = detectImportOptions(fullfile(expProcDataDir,'ICME','RESORT',save_name), 'Delimiter', ',');% NA 17.12.24
                    % Load the table
                    myTable = readtable(fullfile(expProcDataDir,'ICME','RESORT',save_name), import_opts); % NA 17.12.24 to allow correctly importing from RESORT Files                                  
                    SpikList_all=table2array(myTable);
                    all_electrode_ids=unique(SpikList_all(:,4));
                    spik_lists_all={};
                    all_electrode_names={};
                    for elec_i=1:length(all_electrode_ids)
                        all_electrode_names{end+1}=sprintf('elec%i',all_electrode_ids(elec_i));
                        cur_spik_list=SpikList_all(find(SpikList_all(:,4)==all_electrode_ids(elec_i)),:);
                        spik_lists_all.(all_electrode_names{end})=cur_spik_list;
                    end
                    SL.spik_list_all=spik_lists_all;
                    SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
                    SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
                    SL.analysis_parameters=used_analysis_parameters; 
                    obj.SL=SL;
                    return
                end
            end
            [spik_lists_all,used_analysis_parameters,all_electrode_names,Trigger]=generateSLfromRawNlxData_baseline_global(obj,...
                threshold,pre_time,post_time,ref_time,low_filt,high_filt,filt_ord,prestimrectime,poststimrectime,use_artefact_removal);
            % Info about detected spikes
            SL.spik_list_all=spik_lists_all;
            SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
            SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
            SL.analysis_parameters=used_analysis_parameters; 
            SL.Trigger=Trigger; % to keep track of absolute trigger timestamps with the processed data
            obj.SL=SL;
        end

    	
        function obj = loadMUAfromRESORTFile(obj,dirname)
            % icme\loadMUAfromRESORTFile 
            % loads the the MUA spikes from rfrom the Resort file that is
            % generated during ExtractMUAfromRawDataIntoSL(IC) 
            % the spikelist is saved in obj.SL
            % output
            %    obj (icme) IC recording with filled SL field
            % input  
            %   obj (ICME) IC recording to be analysed
            %   dirname (str, optional) directory to the Resort file 


            if nargin==1
                dirname=fullfile(expProcDataDir,'ICME','RESORT');
            end
            
            % first test if enable cache is on and load any existing data
            % if possible, otherwise go for spikelist
            if strcmpi(enablecache,'on')  
                save_name = strcat(obj.ExpID,"_",obj.SeriesID,"_ICME_Resort.txt");
                if isfile(fullfile(dirname,save_name))
                    % read in file
                    text = string(fileread(fullfile(dirname,save_name)));
                    % Split the text into lines
                    lines = strsplit(text, '\n');
                    % Extract the experiment information
                    experiment_info = strsplit(lines{3},'\t');
                    if ~strcmp(obj.SeriesID, experiment_info{2})
                        error('trying to load spikelist from a file with the wrong Experiemnt ID')
                    end
                    % Extract the analysis parameters
                    analysis_params_line = find(contains(lines,'analysis parameters'));
                    fprintf('loading spikelist for %s with following analysis parameters: \n',obj.SeriesID)
                    fprintf('%s \n', lines{analysis_params_line+1:analysis_params_line+9})
                    helper=strsplit(lines{analysis_params_line+1},':');
                    used_analysis_parameters.threshold=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+2},':');
                    used_analysis_parameters.pre_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+3},':');
                    used_analysis_parameters.post_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+4},':');
                    used_analysis_parameters.ref_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+5},':');
                    used_analysis_parameters.low_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+6},':');
                    used_analysis_parameters.high_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+7},':');
                    used_analysis_parameters.filt_ord=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+8},':');
                    used_analysis_parameters.prestimrectime=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+9},':');
                    used_analysis_parameters.poststimrectime=str2num(helper{2});
                     % extract the spikelist
                    spikelist_line=find(contains(lines,'Resulting spikelist')); 
                    % Detect import options and specify the number of header lines to skip
                    import_opts = detectImportOptions(fullfile(expProcDataDir,'ICME','RESORT',save_name), 'Delimiter', ',');% NA 17.12.24
                    % Load the table
                    myTable = readtable(fullfile(expProcDataDir,'ICME','RESORT',save_name), import_opts); % NA 17.12.24 to allow correctly importing from RESORT Files                                  
                    SpikList_all=table2array(myTable);
                    all_electrode_ids=unique(SpikList_all(:,4));
                    spik_lists_all={};
                    all_electrode_names={};
                    for elec_i=1:length(all_electrode_ids)
                        all_electrode_names{end+1}=sprintf('elec%i',all_electrode_ids(elec_i));
                        cur_spik_list=SpikList_all(find(SpikList_all(:,4)==all_electrode_ids(elec_i)),:);
                        spik_lists_all.(all_electrode_names{end})=cur_spik_list;
                    end
                    SL.spik_list_all=spik_lists_all;
                    SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
                    SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
                    SL.analysis_parameters=used_analysis_parameters; 
                    obj.SL=SL;
                    return
               
                else
                    error(sprintf('no spikelist RESORT file found for %s',obj.SeriesID))
                 end
            end
        end



        function obj = loadSLfeather(obj,threshold,pre_time,post_time,ref_time,low_filt,high_filt,filt_ord,prestimrectime,poststimrectime)
            % icme\loadSLfeather old version of function 
            % extracts the the MUA spikes from raw data or loads them from the Resort file
            % to generate the spikes it calls generateSLfromRawNlxData(IC),
            % the spikelist is saved in obj.SL
            % output
            %    obj (icme) IC recording with filled SL field
            % input  
            %   obj (ICME) IC recording to be analysed
            %   optional:
            %       threshold; double (default 3) to adjust spike threshold
            %       pre_time; post_time;  % waveform-window around a detected spike [ms]
            %       ref_time;    % refractory period [ms]
            %       low_filt, high_filt;filt_ord; % bandpass filter properties in beginning of data processing
            %       prestimrectime; poststimrectime % time around trigger that spikes
            %           are extracted from [ms]

            if nargin==1
                threshold = 3; % threshold for waveform extraction (in MAD)
                pre_time = 0.5;
                post_time = 1; % waveform-window
                ref_time = 1; % refractory period
                low_filt = 600;
                high_filt = 6000;
                filt_ord = 4; % filter settings
                % time in ms around trigger that the spikes are extracted
                prestimrectime = 150;
                poststimrectime= 150;
            end
            
            
            % first test if enable cache is on and load any existing data
            % if possible, otherwise go for spikelist
            if strcmpi(enablecache,'on')  
                save_name = strcat(obj.ExpID,"_",obj.SeriesID,"_ICME_Resort.txt");
                if isfile(fullfile(expProcDataDir,'ICME','RESORT',save_name))
                    % read in file
                    text = string(fileread(fullfile(expProcDataDir,'ICME','RESORT',save_name)));
                    % Split the text into lines
                    lines = strsplit(text, '\n');
                    % Extract the experiment information
                    experiment_info = strsplit(lines{3},'\t');
                    if ~strcmp(obj.SeriesID, experiment_info{2})
                        error('trying to load spikelist from a file with the wrong Experiemnt ID')
                    end
                    % Extract the analysis parameters
                    analysis_params_line = find(contains(lines,'analysis parameters'));
                    fprintf('loading spikelist for %s with following analysis parameters: \n',obj.SeriesID)
                    fprintf('%s \n', lines{analysis_params_line+1:analysis_params_line+9})
                    helper=strsplit(lines{analysis_params_line+1},':');
                    used_analysis_parameters.threshold=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+2},':');
                    used_analysis_parameters.pre_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+3},':');
                    used_analysis_parameters.post_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+4},':');
                    used_analysis_parameters.ref_time=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+5},':');
                    used_analysis_parameters.low_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+6},':');
                    used_analysis_parameters.high_filt=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+7},':');
                    used_analysis_parameters.filt_ord=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+8},':');
                    used_analysis_parameters.prestimrectime=str2num(helper{2});
                    helper=strsplit(lines{analysis_params_line+9},':');
                    used_analysis_parameters.poststimrectime=str2num(helper{2});
                     % extract the spikelist
                    spikelist_line=find(contains(lines,'Resulting spikelist')); 
                    % Detect import options and specify the number of header lines to skip
                    import_opts = detectImportOptions(fullfile(expProcDataDir,'ICME','RESORT',save_name), 'Delimiter', ',');% NA 17.12.24
                    % Load the table
                    myTable = readtable(fullfile(expProcDataDir,'ICME','RESORT',save_name), import_opts); % NA 17.12.24 to allow correctly importing from RESORT Files                                  
                    SpikList_all=table2array(myTable);
                    all_electrode_ids=unique(SpikList_all(:,4));
                    spik_lists_all={};
                    all_electrode_names={};
                    for elec_i=1:length(all_electrode_ids)
                        all_electrode_names{end+1}=sprintf('elec%i',all_electrode_ids(elec_i));
                        cur_spik_list=SpikList_all(find(SpikList_all(:,4)==all_electrode_ids(elec_i)),:);
                        spik_lists_all.(all_electrode_names{end})=cur_spik_list;
                    end
                    SL.spik_list_all=spik_lists_all;
                    SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
                    SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
                    SL.analysis_parameters=used_analysis_parameters; 
                    obj.SL=SL;
                    return
                end
            end
            [spik_lists_all,used_analysis_parameters,all_electrode_names]=generateSLfromRawNlxData(obj,threshold,pre_time,post_time,ref_time,low_filt,high_filt,filt_ord,prestimrectime,poststimrectime);
%             [spik_lists_all,used_analysis_parameters,all_electrode_names]=generateSLfromRawNlxData_BackgroundNoise(obj,threshold,pre_time,post_time,low_filt,high_filt,filt_ord,prestimrectime,poststimrectime);
            % Info about detected spikes
            SL.spik_list_all=spik_lists_all;
            SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
            SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
            SL.analysis_parameters=used_analysis_parameters; % should include all variables during spiek detection which are
            % pre_time, post-time around trigger, low/high pass
            % filter + filter order, ggf threshold
            obj.SL=SL;
        end

        function obj = loadSLfeather_fromRESORT(obj,dirname)
            % icme\loadSLfeather_fromRESORT loads SL from a RESORT file
            % generated with AD old scripts, this funciton only exists for
            % sanity checks when reproducing old figures
            % output
            %    obj (icme) IC recording with filled SL field
            % input  
            %   obj (ICME) IC recording to be analysed
            %   dirname (str) path to the _RESORT file that was created
            %   with the old AD analysis scripts

            
            save_name = strcat(obj.SeriesID,"_Resort.mat");

            if isfile(fullfile(gen_dir_name(dirname),save_name))
                ResortFile = load(fullfile(gen_dir_name(dirname),save_name));
                SpikList_all=ResortFile.Spik_list;
                all_electrode_ids=0:1:31;
                spik_lists_all={};
                all_electrode_names={};
                for elec_i=1:length(all_electrode_ids)
                    all_electrode_names{end+1}=sprintf('elec%i',all_electrode_ids(elec_i));
                    cur_spik_list=SpikList_all(find(SpikList_all(:,4)==all_electrode_ids(elec_i)),:);
                    spik_lists_all.(all_electrode_names{end})=cur_spik_list;
                end
                SL.spik_list_all=spik_lists_all;
                SL.all_electrode_names=all_electrode_names; % constantly needed for further analysis
                SL.all_electrodes=[0:1:length(all_electrode_names)-1]';
                SL.analysis_parameters=['loaded from AD resort file']; 
                obj.SL=SL;
                    
            else
                fprintf('no AD style resort file found for %s \n',obj.SeriesID)
            end
        
        end


        %
        function obj = stim(obj)
            % populates the stim field of the icme. It requires that the raw
            % data are already loaded.
            obj(1).Stim(1).exp_type=obj.ExpInfo.exp_type; %MX_tones or OBISPulse etc
            obj(1).Stim(1).stimlist =obj.ExpInfo.stimlist; % descriptive table of the used stimuli
            % For creeLED10x1, check if 'stimlist' is a cell and convert it to double if needed
            if iscell(     obj(1).Stim(1).stimlist)
                % Iterate through the cell array and convert non-numeric entries to NaN
                for i = 1:numel(obj(1).Stim(1).stimlist)
                    if isempty(obj(1).Stim(1).stimlist{i}) || ~isnumeric(obj(1).Stim(1).stimlist{i})
                        obj(1).Stim(1).stimlist{i} = 0; % Replace empty or non-numeric cells with 0
                    end
                end
                % Now attempt to convert the cell array to a double
                obj(1).Stim(1).stimlist = cell2mat(obj(1).Stim(1).stimlist);
            end
            obj(1).Stim(1).stimheader = obj.ExpInfo.stimheader; % header of previous table
            obj(1).Stim(1).dura = obj.ExpInfo.dura; % duration of the full stimulus for each condition
            obj(1).Stim(1).n_rep= obj.ExpInfo.n_rep; % number of presentations per stimulus
        end

        function obj = setRawDataDir (obj, updateRawDataDir)
            % icme\setRawDataDir update the rawDataDirectory
            % variable type - struct
%                 obj.D= updateRawDataDir;

                if any(contains(updateRawDataDir.dir,obj.ExpID))
                    obj.D= updateRawDataDir;
                else
                    warning(sprintf('%s: the raw  data directory does not contain the expID. Operation aborted. Are you sure you know what you are doing?',obj.SeriesID))
                end
            
        end



    end
end