I need to remove the NaN lines from my output matrix.

1 view (last 30 days)
I have a m file that is meant to read a text file and and convert the data within into a matrix. The issue is that the data has labels scattered through out and I need to remove these labels. So my code currently looks something like:
fid = fopen('Test_Data_2.xyz', 'rt');
datacell = textscan(fid, '%*s %f %f %f', 'HeaderLines', 2, 'CollectOutput', 1);
fclose(fid);
celldisp(datacell)
I then go on to use the data in the outputted matrix. The "celldisp" function currently gives me an output of;
datacell{1} =
3.0697 1.4511 9.8552
1.8283 2.1200 9.3934
1.7869 3.5402 9.8658
0.5861 4.3056 9.3636
NaN NaN NaN
NaN NaN NaN
3.0705 1.4535 9.8578
1.8297 2.1194 9.3960
1.7861 3.5392 9.8658
0.5880 4.3057 9.3634
Where the original data in my text file reads:
46
text-Cs
C 3.069663 1.451149 9.855179
C 1.828334 2.119961 9.393412
C 1.786922 3.540219 9.865837
46
text-Cs
C 3.070549 1.453499 9.857827
C 1.829730 2.119381 9.396008
C 1.786074 3.539174 9.865825
C 0.587989 4.305654 9.363402
Is there any way I can remove the NaN from my output matrix?

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2013
datacell{1}( all(isnan(datacell{1}),2) ) = [];
If you want it to change it so that rows with at least one nan (instead of all nan) are removed, change the "all" to "any"

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!