Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Look at the documentation for ReadList, and particularly the RecordLists->True option setting. –  Verbeia Oct 28 '13 at 4:39
    
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. –  Yves Klett Oct 28 '13 at 12:25
    
@Verbeia, you should post that as an answer with an example---I didn't know that option and its likely the best approach. –  george2079 Oct 28 '13 at 14:26
    
@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. –  Verbeia Oct 28 '13 at 20:53
    
@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. –  user28575 Oct 28 '13 at 23:49

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
This is exactly what i want thanks! –  user28575 Oct 28 '13 at 23:43

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}}}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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