matlabfilesaveexporteeglab

Save eeglab to mat file in for loop Matlab


I would like to save set files in .mat files in a loop, keeping the original filename, just changing the extension to .mat.

I tried different ways of coding I found online, but I never got it to make change the filename for each subject. This code below I can use without problems for exporting set files in .txt files. Isn't there a way to export in .mat too?

for i = 1:length(files)
    filename = files(i).name;
    
    EEG = pop_loadset('filename', filename, 'filepath', directory_name);
    
    EEG = eeg_checkset( EEG );
    EEG = pop_export(EEG,fullfile(directory_name, [filename(1:end-4),'.mat']));
  
end 

When I run this code and I want to load this file in Matlab, it gives me the error:

load('AB24 task_ipol_500Hz_7min_MARA.mat')

Error using load
Unable to read MAT-file
C:\MTB862015\eeglab2019_1\All task\_2_min7_task\_6_min7_MARA\AB24 task_ipol_500Hz_7min_MARA.mat.
Not a binary MAT-file. Try load -ASCII to read as text.

Thank you very much!!


Solution

  • this code works for me, maybe it can help someone else :)

    
    for i = 1:length(files)
        filename = files(i).name;
        
        EEG = pop_loadset('filename', filename, 'filepath', directory_name);
        
        EEG = eeg_checkset( EEG );
        s = filename(1:end-29); % Get the subjects code
        
        save([ num2str(s) 'task_ipol_500Hz_7min_MARA.mat'],'EEG')
    end