How to automatically import data out of blocks in ascii files?

I have some data from measurements and the data files are like:
WL 1200.00
x1 tab y1
x2 tab y2
.
.
.
WL 1201.00
x1 tab y1
.
.
.
Wl 1202.00
.
.
Every block starts with 'WL' and then a wavelength. Then beneath the x-y data devided by tabs.
The problem is that the data arrays do not always have the same length. How can I tell Matlab that a new block starts with 'WL'. My goal is to get a 3D diagram with all the curves for all the different wavelengths...

 Accepted Answer

Several solutions would be available. Here is one which uses some of the most basic tools for reading ASCII file content:
fid = fopen('myData.txt', 'r') ;
content = {} ;
cId = 0 ; % Content/block counter/ID.
while ~feof(fid)
line = fgetl(fid) ;
if isempty(line), continue ; end
if line(1) == 'W' % Create new block; save
cId = cId + 1 ; % WL, init data array.
content{cId}.WL = sscanf(line, '%*s %f') ;
content{cId}.data = [] ;
else % Append xy to data array.
xy = sscanf(line, '%f\t%f') ;
content{cId}.data = [content{cId}.data; xy.'] ;
end
end
fclose(fid) ;
With that and the following data
WL 1200.00
1.8 2.3
3 8
WL 1200.01
2.6 9.0
3.4 1.2
8 7
7.3 5.4
you get
>> length(content)
ans =
2
>> content{1}
ans =
WL: 1200
data: [2x2 double]
>> content{1}.data
ans =
1.8000 2.3000
3.0000 8.0000
>> content{2}.data
ans =
2.6000 9.0000
3.4000 1.2000
8.0000 7.0000
7.3000 5.4000
Here is a mixed REGEXP/SSCANF solution, just for the fun of it (could also be STRFIND/SSCANF). It is likely to be faster than the first version on large files..
buffer = fileread('myData.txt') ;
blocks = regexp(buffer, 'WL', 'split') ;
blocks = blocks(2:end) ; % Skip 1st empty block.
nBlocks = length(blocks) ;
content_regexp = cell(nBlocks, 1) ;
for k = 1 : nBlocks
values = sscanf(blocks{k}, '%f', Inf) ;
content_regexp{k}.WL = values(1) ;
content_regexp{k}.data = reshape(values(2:end), 2, []).' ;
end

More Answers (0)

Asked:

on 27 Jul 2013

Community Treasure Hunt

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

Start Hunting!