calculate something for every piece of text seperatelly
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Maria K
el 28 de Nov. de 2017
Comentada: Maria K
el 29 de Nov. de 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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 28 de Nov. de 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 .
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!