【问题标题】:For loop for different datasets and conditions for EEGLAB针对 EEGLAB 的不同数据集和条件的 for 循环
【发布时间】:2016-04-26 04:38:40
【问题描述】:

我必须分析一些 EEG 数据,并且我正在尝试自动化预处理过程。

我有 40 名参与者。每个参与者有 4 个不同的文件,用于 4 个条件。

所以文件保存为

1-1  
1-2  
1-3  
1-4  
2-1  
2-2  
2-3  
2-4  
...  

最多 40-4 个

文件来自 BioSemi (.bdf)

我已经能够自动化预处理过程,但每次我必须选择不同的文件,运行脚本并保存它。

我想运行一个 for 循环,它自己完成所有操作(取 1-1,运行预处理,保存,取 1-2...等)。

接下来我将粘贴到目前为止的内容。

我一整天都在尝试编写一个 for 循环,但它不起作用。

非常感谢任何帮助。

这是当前的预处理脚本:


subject = '1-1.bdf'  

%Open EEGLAB and inizialize several EEGLAB variables (listed in the output
%function  
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;  

%Load a file  
%EEG=pop_loadset;  % pop up window to input arguments  

EEG = pop_biosig(subject)  %Reads in the dataset frin a BIOSEMI file  

% Stores the dataset into EEGLAB  
[ALLEEG EEG CURRENTSET ] = eeg_store(ALLEEG, EEG);  

%Change sampling rate to 512  
EEG = eeg_checkset( EEG );    
EEG = pop_resample( EEG, 512);    
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET); %save it as a new dataset  

% Edit Channel Location  
EEG = eeg_checkset( EEG );  
EEG=pop_chanedit(EEG, 'lookup','D:\\Matlab\\eeglab_current\\eeglab13_5_4b\\plugins\\dipfit2.3\\standard_BESA\\standard-10-5-cap385.elp');  
% Store the dataset into EEGLAB  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

% Add some comments  
EEG.comments = pop_comments(EEG.comments,'','Sampling rate was changed to 512.',1);  
% You can see the comments stored with the dataset either by typing >> EEG.comments or selecting the menu option Edit->About this dataset.  

%Select Data. Remove EXG5:EXG8   
EEG = eeg_checkset( EEG );  
EEG = pop_select( EEG,'nochannel',{'EXG5' 'EXG6' 'EXG7' 'EXG8'});  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  


%HighPassFilter 0.5  
EEG = eeg_checkset( EEG );  
EEG = pop_eegfilt( EEG, 0.5, 0, [], [0], 0, 0, 'fir1', 0);  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

%LowPassFilter 50  
EEG = eeg_checkset( EEG );  
EEG = pop_eegfilt( EEG, 0, 50, [], [0], 0, 0, 'fir1', 0);  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  


%ReReference to 35 36 (EXG3. EXG4)  
EEG = eeg_checkset( EEG );  
EEG = pop_reref( EEG, [35 36] );  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');   

% Reject Continuous By Eye  
EEG = eeg_checkset( EEG );  
pop_eegplot( EEG, 1, 0, 1);  

eeglab redraw % Update the EEGLAB window to view changes  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Automatic Channel Rejection  
EEG = eeg_checkset( EEG );  
EEG = pop_rejchan(EEG, 'elec',[1:34]   ,'threshold',5,'norm','on','measure','prob');  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

eeglab redraw % Update the EEGLAB window to view changes 

【问题讨论】:

    标签: matlab for-loop eeglab


    【解决方案1】:

    以下是循环访问文件名的方法,以便您可以运行 MATLAB 脚本。最简单的做法是将您的 .bdf 文件单独放在一个文件夹中。然后写一个函数来包装你想要的功能,像这样:

    function run_script_with_loop(pathname)
    
    file_struct_list = dir([pathname filesep() '*.bdf']);  %% get list of .bdf files in the pathname specified
    
    filename_list = {file_struct_list.name};  %% extract the filenames into a cellarray
    for subject = filename_list  %% this iterates over the elements of the cell array, one-by-one, setting the `filename` variable like a loop variable
        [ALLEEG EEG CURRENTSET ALLCOM] = eeglab;  
        full_pathname = [pathname filesep() subject{1}];
        EEG = pop_biosig(full_pathname);  %% perform your processing
        ...
    end
    

    几个cmets:

    • 我试图通过使用filesep() 来实现平台不可知论,所以 这应该适用于 linux/mac/windows。如果您正在运行代码 在与数据文件相同的目录中,您当然可以简化 这个

    • 确保在取消引用 subject 时使用花括号 {}。如果您将标准 matlab 数组引用与 subject(1) 一起使用,那么您将获得一个包含文件名字符串的元胞数组,而不仅仅是普通文件名字符串

    • MATLAB 中的 dir() 命令与 Windows 中的 dir 或 Linux 中的 ls 一样,您可以使用通配符获取自定义的文件列表,例如 dir('1-*.bdf') 仅获取列表受试者 1 的数据文件

    【讨论】:

    • 嗨@gariepy,非常感谢您的回复,我真的非常感谢。不幸的是,我仍然无法让它工作。对不起,这是我编程或使用 Matlab 的第一天。我是否必须输入整个路径名,例如D:/5 - EEG/EEG Recordings,无论它写着pathnamefull_pathname 吗?为了让它工作,我应该去:new-> function 复制并粘贴你所有的代码。将函数保存到任意文件夹SethPath->AddFolder。我收到一条错误消息:“所选会话无法评估,因为它包含无效语句”
    • 是的,将代码保存到文件中,在本例中称为 run_script_with_loop.m,因为 MATLAB 希望 function 之后的名称与文件名匹配。确保保存文件的文件夹位于 matlab 路径中。然后您应该可以输入% run_script_with_loop('D:\MATLAB\eeglab_current\data_folder') ,其中data_folder 是您的..bdf 文件所在位置的全名。
    • 另外,... 旨在表示您示例中的所有代码应该去哪里。
    • OH MY GOD,它正在工作 __ 我只需要在我的代码中调整一些东西,但它应该可以工作。非常感谢,真的。我要怎么感谢你? =) 非常感谢您的时间和精力 =)
    • @Glu:我只想问,随着你在 MATLAB 上的进步,一定要教别人,然后回到 StackOverflow 并分享你的知识!
    猜你喜欢
    • 2020-11-04
    • 2016-05-24
    • 2021-05-06
    • 2021-08-01
    • 1970-01-01
    • 2020-01-03
    • 2020-08-27
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多