1
votes

I have a text file with two columns containing space-delimited numeric data. The data are divided into segments, where each of them contains a variable number of rows and the beginning of the next segment is indicated by a single row of NaN (so the data below would be comprised of four segments):


5.1 [space] 0.0
9.2 [space] 1.4
3.7 [space] 0.6
... [space] ...
... [space] ...
... [space] ...
NaN [space] NaN
9.7 [space] 6.3
1.4 [space] 1.0
... [space] ...
... [space] ...
NaN [space] NaN
8.7 [space] 0.0
5.1 [space] 7.4
3.7 [space] 2.6
... [space] ...
NaN [space] NaN
1.7 [space] 8.4
... [space] ...

and so forth...


Now I'd like to generate a plot displaying a curve for each data segment in the file (by plotting each segments 1st column against its 2nd column).
I know how to do this if

  • the number of rows in each segment is the same and

  • the number of segments is known.

How can that be done efficiently when there's a variable segment size and an unknown number of segments?

1

1 Answers

2
votes

I generated some random data similar to your file. Text file like the one you described can be loaded like this (load correctly handles NaNs):

Data = load(filename, '-ascii');

I know that this code is not perfect, but it works for arbitrary number of segments separated by NaNs.

% Here I generate some random points to test the code
NPoints                     = 100;

Data                        = rand(NPoints,2);
% Insert NaNs in both columns
Data( Data(:,1)>0.9, : )    = NaN;

% Now do some magic
idxNaN                          = find( isnan( Data(:,1) ) );
idxNearNaNs                     = [ idxNaN-1 idxNaN+1 ]';
SegmentStartEnd                 = reshape( [1;idxNearNaNs(:);NPoints] , 2, [] )';
SegmentsOK                      = ( SegmentStartEnd(:,2) - SegmentStartEnd(:,1) ) >= 0;
SegmentStartEnd( ~SegmentsOK,:) = [];

% SegmentStartEnd contain start and end rows of good segments
NumberOfSegments = size(SegmentStartEnd,1);
fprintf('NaNs positions: %s\n', sprintf('%g ',idxNaN));
fprintf('%d segments found\n', NumberOfSegments);

% Now plot the data
figure;
hold all;
for k=1:NumberOfSegments
    SegmentStart    = SegmentStartEnd(k,1);
    SegmentEnd      = SegmentStartEnd(k,2);
    fprintf('Segment #%04d: Rows %04d to %04d\n', k, SegmentStart,SegmentEnd);
    plot( Data(SegmentStart:SegmentEnd,1) , Data(SegmentStart:SegmentEnd,2) );
end

Let me know if you need some explanations.