calculate something for every piece of text seperatelly

1 Ansicht (letzte 30 Tage)
Maria K
Maria K am 28 Nov. 2017
Kommentiert: Maria K am 29 Nov. 2017
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 .

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by