1
$\begingroup$

I have an external data file with several columns and thousands lines of data. For the sake of this question, let's assume that we use

I1 = Import["data.out","Table"];

to import a sample data file which is the following

data = {{2,-1,0},{-4,0,2},{7,5,-1},{},{8,1,-3},{-5,2,-3},{},{-4,-1,3},{3,7,2},{0,0,2},{}}

We see, that there are empty sub-lists {} corresponding to blank lines to the external data file. Also, at the end there is always a blank line. Each blank line acts as a separator distinguishing between different piles in the data file. So, my question is the following: how can plot using ListPlot like

S1 = ListPlot[Flatten[List /@ data[[All, {2, 3}]], 1], Joined -> True,
              Frame -> True, Axes -> False, AspectRatio -> 1, 
              PlotStyle -> {Black, PointSize[0.001]}]

all the different sets of data? In other words, how can I plot only the first sequence until the first blank line ({2,-1,0},{-4,0,2},{7,5,-1} according to the sample), or the data between let's say, 4rd and 5th blank lines, the last set, etc?

$\endgroup$

1 Answer 1

1
$\begingroup$

Since you have so much data I think something based on Position will give adequate speeds:

splitWhere[l_List, e_: {}] := Module[{
   pos = Flatten@{0, Position[l, e, 1, Heads->False]},
   spans},
  If[Last@l =!= e, AppendTo[pos, Length@l + 1]];
  spans = Span @@@ (# + {1, -1} & /@ Partition[pos, 2, 1]);
  l[[#]] & /@ spans
  ]
splitWhere[data];
(* {{{2, -1, 0}, {-4, 0, 2}, {7, 5, -1}},
    {{8, 1, -3}, {-5, 2, -3}},
    {{-4, -1, 3}, {3, 7, 2}, {0, 0, 2}}} *)

Here is an example plotting parts of the data:

(* Generate some data *)
data = RandomReal[1, {10000, 3}];
splitpos = RandomInteger[{1, 10000}, 200];
data[[splitpos]] = {};

(* Split and plot *)
splitData = splitWhere[data];//AbsoluteTiming  (* 0.003s *)
ListPlot[splitData[[{2, 5, 8}, All, {2, 3}]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ It's working fine! I 'll wait a little bit to see if someone else suggest a different approach and then I 'll approve your answer. Many Thanks again! $\endgroup$
    – Vaggelis_Z
    Oct 26, 2013 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.