I need to load only part of a file

11 views (last 30 days)
I need MATLAB to load a file but the file is not a uniform matrix of number. I am using the function in matlab: load('file_name.txt'); So for example the file opens as:
46
Graphene-Cs
C 3.069663 1.451149 9.855179
C 1.828334 2.119961 9.393412
C 1.786922 3.540219 9.865837
C 0.586064 4.305576 9.363573
C 0.589753 5.714887 9.934629
C 3.115297 0.068234 9.349019
C 5.495670 1.480486 9.955026
C 4.286234 2.221273 9.447150
C 4.255019 3.612844 9.937289
C 3.019751 4.340426 9.478676
C 3.007670 5.775853 9.955533
But I need it to load the following into matlab:
3.069663 1.451149 9.855179
1.828334 2.119961 9.393412
1.786922 3.540219 9.865837
0.586064 4.305576 9.363570
0.589753 5.714887 9.934629
3.115297 0.068234 9.349019
5.495670 1.480486 9.955026
4.286234 2.221273 9.447150
4.255019 3.612844 9.937289
I am thus receiving the error:
Error using load Number of columns on line 3 of ASCII file C:\Documents\file_name.txt must be the same as previous lines.
Error in get_diff (line 9) load('file_name.txt');
Can anyone help me with this?

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jun 2013
fid = fopen('file_name.txt', 'rt');
datacell = textscan(fid, '%*s%g%g%g', 'HeaderLines', 2, 'Collect', 1);
fclose(fid);
YourData = datacell{1};

More Answers (1)

Image Analyst
Image Analyst on 6 Jun 2013
Use fgetl() and read just however many lines you want to.

Community Treasure Hunt

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

Start Hunting!

Translated by