calculate something for every piece of text seperatelly
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello! I have some text file (like the one I attached but with many more trials) and I have written a code (also attached) to do some calculations for every trial and print the results in another text file. The problem is I execute the code by importing the data of every trial seperately. Is there a way to import the data of every trial (nonmanually) and do the calculations my code does? Maybe with the textscan command?
Thanks for your precious advice.
~M.
0 commentaires
Réponse acceptée
Walter Roberson
le 28 Nov 2017
Under the assumption that the trial numbers are always numeric:
S = fileread('try4.txt');
blocks = regexp(S, '^\s*\d+.*?end of trial', 'match', 'lineanchors');
Now blocks is a cell array of character vectors, each one of which is the complete block for a trial. Then
trialinfo = cell2mat(regexp(blocks, '(?<trialnum>\d+)\s+(?<trialname>[^\n]*?)\s*\n', 'names', 'once'));
trialnums = str2double({trialinfo.trialnum});
trialnames = {trialinfo.trialname};
trialnums is now a numeric vector of the trial numbers, in order from the file. trialnames is now a cell array of character vectors of the trial names (rest of the line after the trial number)
and then
keyinfo = regexp(blocks,'^(?<key>[^d]\S+)\s+(?<time>\S+)', 'names', 'lineanchors');
keynames = cellfun(@(S) {S.key}, keyinfo, 'uniform', 0);
keytimes = cellfun(@(S) str2double({S.time}), keyinfo, 'uniform', 0);
keynames is now a cell array with one entry per trial. Each entry is a cell array of character vectors of the information such as "LeftArrow".
keytimes is now a cell array with one entry per trial. Each entry is a numeric vector of the corresponding numbers for each of the keynames .
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!