MATLAB Answers

How to loop over datafiles using the sprintf function?

2 views (last 30 days)
Alix Weidema
Alix Weidema on 6 May 2021
Edited: Jan on 6 May 2021
Hello everyone,
I have just started learning Matlab and I wrote a script for analysis which seems to work! :) Now, at the beginning of my script, I want to create a loop, to loop over all participants (46 participants, divided into 23 dyads) and conditions (2 conditions: ES and NS), using the sprintf function. I just can't seem to figure it out..
Fyi, this means that I have 4 files for every dyad (both conditions for both participants) and my files are named as follows (for clarity, I give all four):
"hyper_cleaned_SNS_003S_004L_E_ES_sub_1.set"
"hyper_cleaned_SNS_003S_004L_E_ES_sub_2.set"
"hyper_cleaned_SNS_003S_004L_E_NS_sub_1.set"
"hyper_cleaned_SNS_003S_004L_E_NS_sub_2.set"
  • '003S' indicates the first subject and '004L' indicates the second subject of the dyad (so this concerns the dyad consisting of subject 3 and subject 4)
  • ES/NS indicates the condition
  • 'sub_1' indicates that this is the data from the first subject of the dyad ('003S') and 'sub_2' indicates that this is the data from the second subject of the dyad ('004S')
Now, I want to create a loop to loop over all dyads and both conditions (and put the rest of my code within this loop), but I get stuck (I am mainly confused because I perform the rest of my analysis separately for the participants within a dyad, but the loop has to go over the dyads and not separate participants.). This is what I have so far:
dyads = 1:23
conditions = 1:2
% number of conditions = 1:2
for i = 1:23, ii = 1:2
filename = sprintf('dyad%d_condition%d',dyads(i), conditions(ii)); % the filename I specify here must manipulate the string with filename used in pop_loadset
addpath('/Users/AlixWeidema/Dropbox/hyper_cleaned');
data_sub_A = pop_loadset('filename','hyper_cleaned_SNS_003S_004L_E_ES_sub_1.set','check','off','loadmode','info');
data_sub_B = pop_loadset('filename','hyper_cleaned_SNS_003S_004L_E_ES_sub_2.set','check','off','loadmode','info');
end
I'm sorry for all this text! Any help would be greatly appreciated, thanks!!

Answers (1)

Jan
Jan on 6 May 2021
Edited: Jan on 6 May 2021
This is valid Matlab code, but it does not do, what you expect:
for i = 1:23, ii = 1:2
This is 1 loop only. ii is set to the vector [1,2] in all cases. I guess you mean:
for i = 1:23
for ii = 1:2
Do not add a folder to Matlab's PATH only to import data. The PATH should contain only the M-files with the code. It is much saver to use absolute path names instead:
folder = '/Users/AlixWeidema/Dropbox/hyper_cleaned';
for i = 1:23
for ii = 1:2
filename = sprintf('dyad%d_condition%d', dyads(i), conditions(ii));
file = fullfile(folder, filename);
data_sub_A = pop_loadset(file, ...
You have provided 'filename' as input of pop_loadset(). Do you really want to provide the char vector 'filename', or the variable filename? In the latter case, omit the quotes.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by