简体   繁体   中英

Sequentially save .txt outputs from multiple participants in loop in Matlab

Apologies for the basic question, but if anyone can offer assistance with the following it would be greatly appreciated:

I have a script that calculates the mean reaction time from selected trials in a large matrix, and saves this result to a .txt file for each participant, however it overwrites the txt file each time. I need to save each subject's results individually.

This is what I have so far:

PN = data(:,1);
RT = data(:,9);

for i = 1:20;
    index = PN == i & Variable == 1;
    VariableRT = mean(RT(index));
    dlmwrite('VariableRT.txt', VariableRT)

end

Thank you.

You can change the string you are using to save the file during each iteration of the for loop like so:

for i = 1:20
  ...
  ...
  dlmwrite(['VariableRT',num2str(i),'.txt'], VariableRT);
end

['VariableRT',num2str(i),'.txt'] creates a concatenation of the three strings 'VariableRT', 'iterationNumberAsString' and '.txt'.

you can add a num2str which will number your VariableRT.txt so that you have VariableRT1.txt, VariableRT2.txt ... VariableRT(i).txt. The i is updated each time in your loop, thus naming each txt file with the iteration number.

VariableRTFile = ['VariableRT' num2str(i) '.txt'];
dlmwrite(VariableRTFile, VariableRT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM