Export multiple PLV matrices

Dear BST community,

If anyone is looking for a script to extract the connectivity values from the .mat files stored in the brainstorm_db folder, I wrote the following script:

% Enter the list of subjects as they appear in Brainstorm
SubjectNames = {'4205', '4227', '4235', '4237', '4240', '4245', '4250', '4267', '4295', '7001', '7002', '7003', '7004', '7005', '7006', '7007', '7008', '7009', '7010', '7012', '7013', '7014', '7015', '7016', '7018', '7019', '7020', '7021', '7022', '7023', '7024', '7025', '7026', '7027', '7028', '7029', '7030', '7031', '7032', '7033', '7034', '7035', '7036', '7037', '7039', '7040', '7041', '7042', '7043', '7044', '7045', '7046', '7047', '7048', '7049', '7050', '7051', '7052', '7053', '7055', '7056', '7059', '7061', '7062', '7064', '7066', '7067', '7068', '7069', '11_4239', '12_4239'};
% Loop through all subjects
for iSubject = 1:length(SubjectNames)
d = dir(['C:\Users\User\Desktop\brainstorm_db\test2\data' num2str(SubjectNames{iSubject}) '\rest\timefreq_connectn_plv_average*.mat']); % "num2str(SubjectNames{iSubject})" allows to loop through each subject, and "*" truncates the ending characters that differ between subjects.
names = {d.name};
x = char(names)
BSTfile = load(x)
PLV = bst_memory('GetConnectMatrix', BSTfile);
PLVdelta = PLV(:,:,1) % The last number represents the frequency band (Delta:1, Theta:2, Alpha:3, Beta:4, Gamma:5).
dlmwrite(['PLVdeltarest' num2str(SubjectNames{iSubject}) '.txt'], PLVdelta,'delimiter','\t'); % Writes each subject's connectivity matrix (in only one frequency band) in a tab-delimited text file.
end

It might not be pretty but it works!

Cheers,
Simon

1 Like