MATLAB: Extract from text file

text file

Hi, I am trying to use Matlab for this because I have ~300 files for which this needs to be done..
The data in the .txt file looks like this:
Event List
Patient Name: xyz Export Time: Thursday, April 11, 2013, 15:25:00 User Name: xyz Test ID: Name Time Duration Exam Start 00:00:00 00:00 Impedance – Impedance Values 00:00:00 00:00 Change – Montage Apply: 0bical.mtg 00:00:17 00:00 Calibration Off 00:00:18 00:00 Change – Montage Apply: 1doublebanana.mtg 00:00:36 00:00 . . .
I need a matrix file with 3 columns like this 00:00:00 00:00:00 Exam Start 00:00:00 00:00:00 Impedance – Impedance Values
i.e. 1) discard the first 7 lines.. 2) text goes into column 3 3) First values after text go into column 1 3) Second value + first value go into column 2
Is this possible?
Thanks, S

Best Answer

New version, taking into account your last comment.
fname_in = 'myFile.txt' ;
fname_out = 'myFile.xlsx' ;
headerSize = 8 ;
fid = fopen(fname_in, 'r') ;
% - Discard header.
for k = 1 : headerSize
if feof(fid)
error('Structure discrepancy in %s', fname_in) ;
end
fgetl(fid) ;
end
% - Extract/process content.
buffer = cell(1e6, 3) ; % Cheap prealloc..
lineCnt = 0 ;
while ~feof(fid)
lineCnt = lineCnt + 1 ;
line = fgetl(fid) ;
lStr = line(1:end-15) ; % Label string.
dStr = line(end-13:end-6) ; % Date string.
tStr= line(end-4:end) ; % Time string.
fprintf('|%s|%s|%s|\n', dStr, tStr, lStr) ; % Just for testing.
buffer(lineCnt, :) = {dStr, tStr, lStr} ;
end
fclose(fid) ;
buffer = buffer(1:lineCnt,:) ; % Truncate prealloc.
% - Export to XLSX.
xlswrite(fname_out, buffer) ;
You might want to improve the prealloc, adding blocks of cells when/if the initial size is too small. Note that this solution assumes the time stamp (date, time) to have always the same structure.