plot - MatLab - Automated plotting of differently sized segments of a text file -
i have text file 2 columns containing space-delimited numeric data. data divided segments, each of them contains variable number of rows , beginning of next segment indicated single row of nan
(so data below comprised of 4 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 forth...
now i'd generate plot displaying curve each data segment in file (by plotting each segments 1st
column against 2nd
column).
know how if
the number of rows in each segment same ,
the number of segments known.
how can done efficiently when there's variable segment size , unknown number of segments?
i generated random data similar file. text file 1 described can loaded (load
correctly handles nans):
data = load(filename, '-ascii');
i know code not perfect, works arbitrary number of segments separated nans.
% here generate random points test code npoints = 100; data = rand(npoints,2); % insert nans in both columns data( data(:,1)>0.9, : ) = nan; % 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 , end rows of segments numberofsegments = size(segmentstartend,1); fprintf('nans positions: %s\n', sprintf('%g ',idxnan)); fprintf('%d segments found\n', numberofsegments); % plot data figure; hold all; k=1:numberofsegments segmentstart = segmentstartend(k,1); segmentend = segmentstartend(k,2); fprintf('segment #%04d: rows %04d %04d\n', k, segmentstart,segmentend); plot( data(segmentstart:segmentend,1) , data(segmentstart:segmentend,2) ); end
let me know if need explanations.
Comments
Post a Comment