3
$\begingroup$

I have a C++ program that outputs a set of numbers at each time step. A blank line separates successive time steps. Here is an example of my data format:

0.2 42.8
0.4 12.3
0.6 32.1
0.8 37.3

0.2 44.2
0.4 17.8
0.6 39.0
0.8 30.1

I want to import this into Mathematica with the following format:

data = {
  {{0.2`, 42.8`}, {0.4`, 12.3`}, {0.6`, 32.1`}, {0.8`, 37.3`}},
  {{0.2`, 44.2`}, {0.4`, 17.8`}, {0.6`, 39.`},  {0.8`, 30.1`}}
  };

i.e., the data for each time step is in one list. How do I do this?

$\endgroup$
5
  • $\begingroup$ Look at the documentation for ReadList, and particularly the RecordLists->True option setting. $\endgroup$
    – Verbeia
    Commented Oct 28, 2013 at 4:39
  • $\begingroup$ What excatly is your output? In case of a file, you can also try Import. But for us to help you, you need to provide more details. $\endgroup$
    – Yves Klett
    Commented Oct 28, 2013 at 12:25
  • $\begingroup$ @Verbeia, you should post that as an answer with an example---I didn't know that option and its likely the best approach. $\endgroup$
    – george2079
    Commented Oct 28, 2013 at 14:26
  • $\begingroup$ @george2079 - the question has been closed and as moderator I don't think it's appropriate for me to reopen it just so I can answer it. If you like, I can edit your answer, which I don't think it particularly practical at the moment. $\endgroup$
    – Verbeia
    Commented Oct 28, 2013 at 20:53
  • $\begingroup$ @Verbeia I hope the post now is clear. And the solution is answered by george2079 below. Thank you all. (this post is on hold because it's unclear but I don't know how to fix it. $\endgroup$
    – wei
    Commented Oct 28, 2013 at 23:49

2 Answers 2

1
$\begingroup$

Assuming you mean you are writing the c++ program .. one approach (of many) to this is to directly write the file in mathematica form, that is simply put all the curly brackets and commas in the file.

You can then simply read it in with Get[].

This may seem a bit ugly, but with this approach you can readily handle cases where you might have variable length data in each time series.

If you just write the raw numbers to the file and use ReadList[] you'll need to tell mathematica how to partition the data.

Edit: approach based on verbia's comment:

Assuming you have a file like this:

1 2
2 4
5 6
<blank>
1 3
5 9
8 3
9 10
<blank>

You can read in the form requested as :

ReadList[filename, {Number, Number},
      RecordLists -> True,
      RecordSeparators -> {"\n\n"}]

Quite simple and avoids the need to specify the lenght of each record a priori.

$\endgroup$
1
  • $\begingroup$ This is exactly what i want thanks! $\endgroup$
    – wei
    Commented Oct 28, 2013 at 23:43
0
$\begingroup$

edited with updated file format

DeleteCases[SplitBy[ImportString["0.2 42.8
     0.4 12.3
     0.6 32.1
     0.8 37.3 

     0.2 44.2
     0.4 17.8
     0.6 39.0
     0.8 30.1", "Table"], # == {} &], {{}}]

{{{0.2, 42.8}, {0.4, 12.3}, {0.6, 32.1}, {0.8, 37.3}}, {{0.2, 44.2}, {0.4, 17.8}, {0.6, 39.}, {0.8, 30.1}}}

$\endgroup$

Your Answer

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

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