Counting the number of events and total number of blocks from a file.

2 Ansichten (letzte 30 Tage)
I have results of my experiment on a .dat file (.text version is attached here with). It is obtained after doing experiments for several cycles (each cycle is named 'injection#1,injection#2...). I have a program which reads the data eliminating the 'injection' headers and other texts.
PATH = 'E:\MyData\Experiments';
fid=fopen(fullfile(PATH,'Test'));
fgetl(fid) ;
% Skip 1st line (text describing the experiment).
buffer = fread(fid, [1,Inf], '*char') ;
fclose(fid) ;
% Eliminate 'injection' headers.
buffer = regexprep( buffer, 'i.*?\n', '\n') ;
% Convert to numeric type.
data = textscan(buffer, '%f %f %f') ;
x = data{1} ;
y = data{2} ;
time = data{3} ;
data = [data{:}] ;
In this data, each row of number is an 'event'. How can I get the total number of injections and number of events in some injections (for example, number of events in injection 5, or total number of events between injection 5 to 20). I think, this should be done before eliminating the 'injection' headers in the program.
To make more clear, in the attached file there are 94 injections (total injections) and there are 29 events from injection#1 to injection#4 (injection#1: 12 events, injection#2: 6 events, injection#3: 4 events, injection#4: 7 events). As I have a lot of files with a lot of injections, it is difficult to look manually into each file. So, the program should read and give me the total number of injections and number of events between two specified injections.

Akzeptierte Antwort

Cedric
Cedric am 6 Mai 2014
Bearbeitet: Cedric am 6 Mai 2014
Hello Aneps, when I run the following code on your text document:
content = fileread( 'Test.txt' ) ;
blocks = regexp( content, '#:\d+\s*([^i]+)', 'tokens' ) ;
data = cellfun( @(c) reshape(sscanf(c{1}, '%f'), 3, []).', blocks, ...
'UniformOutput', false ) ;
counts = cellfun( @(d) size(d, 1), data ) ;
I get a cell array of data, e.g.:
>> data{2}
ans =
405 160 220515
304 144 653785
445 224 688895
429 272 743333
343 368 753373
306 352 869463
which are all events associated with injection #2, and a numerical array of counts, which are the number of events for the 94 injections:
>> counts
counts =
Columns 1 through 16
12 6 4 7 7 7 7 12 8 12 11 7 9 7 7 5
Columns 17 through 32
10 6 8 9 16 13 14 14 9 14 8 5 10 9 17 9
Columns 33 through 48
6 11 11 9 11 5 9 12 8 14 8 8 7 9 11 10
Columns 49 through 64
7 6 9 5 10 8 4 8 10 8 9 10 8 6 8 13
Columns 65 through 80
11 4 9 11 7 6 13 7 5 3 7 3 9 8 9 4
Columns 81 through 94
5 8 6 9 7 11 5 7 7 7 6 8 8 5
i.e.
>> counts(2)
ans =
6
indicates that there were 6 events associated with injection #2 (displayed above). I think that it is what you are looking for (?)
Note 1: this does the whole processing in a simpler way than my solution from 2013, so you don't need the former code anymore.
Note 2: you can concatenate the cell array data if you still want to have a large array with all data blocks merged, e.g. this way:
>> allData = vertcat( data{:} ) ;

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by