how to parse different sections of text file into separate matrices

2 views (last 30 days)
text file looks like this:
# (Line) AR1
# E(x,y,z,f0), Tangential to the Line(s), Fields (Re-Im), V/m
# Index Point_X Point_Y Point_Z Distance(m) Fields (Re-Im)
0 -0.16 -0.01 -0.175 0.001 -85.8983154297 38.1155586243
1 -0.16 -0.01 -0.174 0.002 -86.2879867554 38.2942657471
2 -0.16 -0.01 -0.173 0.003 -86.6776504517 38.4729766846
3 -0.16 -0.01 -0.172 0.004 -87.0673217773 38.6516876221
4 -0.16 -0.01 -0.171 0.005 -87.456993103 38.8303947449
# (Line) AR2
# E(x,y,z,f0), Tangential to the Line(s), Fields (Re-Im), V/m
# Index Point_X Point_Y Point_Z Distance(m) Fields (Re-Im)
0 -0.16 -0.01 -0.175 0.001 -85.8983154297 38.1155586243
1 -0.16 -0.01 -0.174 0.002 -86.2879867554 38.2942657471
2 -0.16 -0.01 -0.173 0.003 -86.6776504517 38.4729766846
3 -0.16 -0.01 -0.172 0.004 -87.0673217773 38.6516876221
4 -0.16 -0.01 -0.171 0.005 -87.456993103 38.8303947449
I need to split the data by AR1 and AR2. In other words, put the data (index=0-4) into a 3D matrix, with the first dimension corresponding to section 1,2 etc (actual file has a lot more sections). Not interested in column headers or description line E(x,y,z,f0)
  2 Comments
Benjamin Thompson
Benjamin Thompson on 25 Jan 2022
textscan can also read in a table using the 'N' argument for number of lines to read, if you happen to know how many lines are in AR1 and AR2. That would make it more scalable size. Maybe you can do one pass for line counting, then two calls to textscan with some fgets calls sprinkled in to skip over lines you are ignoring.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 25 Jan 2022
Edited: Stephen23 on 25 Jan 2022
TEXTSCAN makes this relatively easy:
out = {};
fmt = repmat('%f',1,7);
opt = {'CollectOutput',true};
fid = fopen('test.txt');
while ~feof(fid)
while isempty(sscanf(fgetl(fid),'%d'))
end
out(end+1) = textscan(fid,fmt,opt{:});
end
fclose(fid);
out{:}
ans = 4×7
1.0000 -0.1600 -0.0100 -0.1740 0.0020 -86.2880 38.2943 2.0000 -0.1600 -0.0100 -0.1730 0.0030 -86.6777 38.4730 3.0000 -0.1600 -0.0100 -0.1720 0.0040 -87.0673 38.6517 4.0000 -0.1600 -0.0100 -0.1710 0.0050 -87.4570 38.8304
ans = 4×7
1.0000 -0.1600 -0.0100 -0.1740 0.0020 -86.2880 38.2943 2.0000 -0.1600 -0.0100 -0.1730 0.0030 -86.6777 38.4730 3.0000 -0.1600 -0.0100 -0.1720 0.0040 -87.0673 38.6517 4.0000 -0.1600 -0.0100 -0.1710 0.0050 -87.4570 38.8304
Because you did not upload a sample file I had to create my own (attached), which of course may differ from the exact formatting of your files. To get fast and accurate help on this forum, always upload a sample data file.

More Answers (1)

Benjamin Thompson
Benjamin Thompson on 25 Jan 2022
fgets and textscan, processing line by line. Then convert the cell arrays to row vectors and combine into your AR1 and AR2 matrix. If sizes are uknown you will need to add more checking of string contents, or try/catch exception handling, something like that. For example:
>> fin = fopen('arrays3.txt', 'rt')
fin =
5
>> fgets(fin)
ans =
' # (Line) AR1
'
>> fgets(fin)
ans =
' # E(x,y,z,f0), Tangential to the Line(s), Fields (Re-Im), V/m
'
>> fgets(fin)
ans =
'
'
>> fgets(fin)
ans =
' # Index Point_X Point_Y Point_Z Distance(m) Fields (Re-Im)
'
>> S1 = ans
S1 =
' # Index Point_X Point_Y Point_Z Distance(m) Fields (Re-Im)
'
>> S1 = fgets(fin)
S1 =
'
'
>> S1 = fgets(fin)
S1 =
' 0 -0.16 -0.01 -0.175 0.001 -85.8983154297 38.1155586243
'
>> AR1_row = textscan(S1, '%d %f %f %f %f %f %f')
AR1_row =
1×7 cell array
{[0]} {[-0.1600]} {[-0.0100]} {[-0.1750]} {[1.0000e-03]} {[-85.8983]} {[38.1156]}
>>

Community Treasure Hunt

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

Start Hunting!