MATLAB Answers

How do I read only a specific lines from txt file and store as array?

3 views (last 30 days)
Hello .
I have some data in attached file and i want to take only the data between X and Y an store it as array in MATLAB.
How can i do it ?
I know there are already similar threads but most of them doesnt contain comments to the codes so i coudnot get what i want.
Thanks

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 26 Dec 2019
Edited: Stephen Cobeldick on 26 Dec 2019
This is easy and efficient with textscan:
opt = {'HeaderLines',17,'CollectOutput',true};
fmt = repmat('%f',1,5);
[fid,msg] = fopen('example.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1}
Giving:
M =
0 5 5 -5.305e-05 -5.67e-12
1e-07 0 0 -0.001346 -9.39e-09
2e-07 0 0 0.001321 8.899e-09
3e-07 0 0 -0.000177 -1.376e-09
4e-07 0 0 -0.001265 -8.834e-09
5e-07 0 0 -0.000514 -3.687e-09
6e-07 0 0 0.0002366 1.459e-09
7e-07 0 0 0.0009872 6.606e-09
8e-07 0 0 0.0009067 6.053e-09
9e-07 0 0 -0.0002417 -1.822e-09
1e-06 0 0 -0.00139 -9.698e-09
1.1e-06 0 5 0.3 -8.179e-11
1.2e-06 0 5 0.3 3.352e-11
1.3e-06 0 5 0.3 -4.139e-11
1.4e-06 0 5 0.3 -9.998e-11
1.5e-06 0 5 0.3 -6.439e-11
1.6e-06 0 5 0.3 -2.881e-11
1.7e-06 0 5 0.3 6.771e-12
1.8e-06 0 5 0.3 1.787e-11
1.9e-06 0 5 0.3 -4.478e-11
2e-06 0 5 0.3 -1.074e-10
2.1e-06 5 0 0.2999 -3.169e-10
2.2e-06 5 0 0.2999 -5.483e-10
2.3e-06 5 0 0.2999 -5.55e-10
2.4e-06 5 0 0.2999 -4.285e-10
2.5e-06 5 0 0.2999 -3.02e-10
2.6e-06 5 0 0.3 -1.755e-10
2.7e-06 5 0 0.3 -1.304e-10
2.8e-06 5 0 0.3 -2.51e-10
2.9e-06 5 0 0.2999 -3.717e-10
3e-06 5 0 0.2999 -4.923e-10
3.1e-06 5 5 0.008347 -8.29e-12
3.2e-06 5 5 0.005635 -9.013e-12
3.3e-06 5 5 0.003308 2.125e-12
3.4e-06 5 5 0.002145 3.311e-12
3.5e-06 5 5 0.001663 -1.328e-12
3.6e-06 5 5 0.00118 -5.966e-12
3.7e-06 5 5 0.000698 -1.06e-11
3.8e-06 5 5 0.0002156 -1.524e-11
3.9e-06 5 5 -9.295e-05 -7.987e-12
4e-06 5 5 -0.0002999 6.227e-12
4.1e-06 0 0 -0.0005739 -3.378e-09
4.2e-06 0 0 0.000453 2.323e-09
4.3e-06 0 0 -0.001431 -8.135e-09
4.4e-06 0 0 -0.0003192 -1.965e-09
4.5e-06 0 0 0.0007925 4.205e-09
4.6e-06 0 0 0.001314 7.097e-09
4.7e-06 0 0 0.0006017 3.145e-09
4.8e-06 0 0 -0.0001101 -8.069e-10
4.9e-06 0 0 -0.0008218 -4.759e-09
5e-06 0 0 -0.001534 -8.71e-09
>>
If the number of header lines are not fixed and vary between files, then you can use fgets in a while loop until you get to an easily identifiable line, then call textscan afterwards.

  4 Comments

Show 1 older comment
islombek mamatov
islombek mamatov on 26 Dec 2019
Thank you very much for your quick answer,
i am getting the result a bit different. can you help with this also?
M =
0 5.0000 5.0000 -0.0001 -0.0000
0.0000 0 0 -0.0013 -0.0000
0.0000 0 0 0.0013 0.0000
0.0000 0 0 -0.0002 -0.0000
0.0000 0 0 -0.0013 -0.0000
0.0000 0 0 -0.0005 -0.0000
0.0000 0 0 0.0002 0.0000
0.0000 0 0 0.0010 0.0000
0.0000 0 0 0.0009 0.0000
0.0000 0 0 -0.0002 -0.0000
0.0000 0 0 -0.0014 -0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 0.0000
0.0000 0 5.0000 0.3000 0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 0 5.0000 0.3000 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.3000 -0.0000
0.0000 5.0000 0 0.3000 -0.0000
0.0000 5.0000 0 0.3000 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 0 0.2999 -0.0000
0.0000 5.0000 5.0000 0.0083 -0.0000
0.0000 5.0000 5.0000 0.0056 -0.0000
0.0000 5.0000 5.0000 0.0033 0.0000
0.0000 5.0000 5.0000 0.0021 0.0000
0.0000 5.0000 5.0000 0.0017 -0.0000
0.0000 5.0000 5.0000 0.0012 -0.0000
0.0000 5.0000 5.0000 0.0007 -0.0000
0.0000 5.0000 5.0000 0.0002 -0.0000
0.0000 5.0000 5.0000 -0.0001 -0.0000
0.0000 5.0000 5.0000 -0.0003 0.0000
0.0000 0 0 -0.0006 -0.0000
0.0000 0 0 0.0005 0.0000
0.0000 0 0 -0.0014 -0.0000
0.0000 0 0 -0.0003 -0.0000
0.0000 0 0 0.0008 0.0000
0.0000 0 0 0.0013 0.0000
0.0000 0 0 0.0006 0.0000
0.0000 0 0 -0.0001 -0.0000
0.0000 0 0 -0.0008 -0.0000
0.0000 0 0 -0.0015 -0.0000
Thanks a lot!!
Stephen Cobeldick
Stephen Cobeldick on 26 Dec 2019
"i am getting the result a bit different. can you help with this also?"
The difference appears to be only due to our format settings, which affect how data are displayed, but do NOT affect how data are imported or stored in MATLAB memory. You appear to be using short (the MATLAB default), whereas I am using shortG. Feel free to experiment with it:

Sign in to comment.

More Answers (0)

Sign in to answer this question.