MATLAB: Importing ascii data with mixed in headers

asciidata importMATLAB

I have an old program that creates ascii files with data in a format similar to the below:
Header line
Set 1 Parameter 1 1.0 Parameter 2 1.0 Parameter 3 1.1
0.0000 1.0000 2.0000 3.0000
0.1000 1.0001 2.0001 3.0001
0.2000 1.0002 2.0002 3.0002
Set 2 Parameter 1 2.0 Parameter 2 2.0 Parameter 3 2.1
0.0000 1.0005 2.0005 3.0005
0.1000 1.0006 2.0006 3.0006
0.2000 1.0007 2.0007 3.0007
This pattern repeats for several more sets, and is generally much larger than the simplified form I have presented.
I would like to extract the data from each set, as well as the values of the parameters for each set, but am having trouble doing so. I could go line by line with fgetl, but this doesn't seem particularly efficient in this case. All the bulk data readers that I can think of have problems with the mixed data format created by the parameter lines. Is there a way to extract the information I want without having to pass through the file at each individual line?

Best Answer

S = fileread('YourFile.txt');
bpos = regexp(S, '^Set', 'start', 'lineanchors');
blocks = mat2cell(S, 1, diff([1 bpos length(S)]));
blocks(1) = [];
fmt = repmat('%f', 1, 4);
data = cellfun(@(B) textscan(B, fmt, 'HeaderLines', 1, 'CollectOutput', 1), blocks);
data is now a cell array of numeric arrays.
The code does assume that each block has the same number of columns, but it does not assume that the blocks have the same number of rows.
Related Question