1
$\begingroup$

I have a list, call it mylist, made of many sublists that are just points from a plot and I would like to export that to a single file. Let's say I have, for example, something like this

mylist1={{1,2},{3,4}};
mylist2={{5,6},{7,8}};
mylist={mylist1,mylist2};

I need an output file like

1 2
3 4

5 6
7 8

(notice the empty line between the two sublists). If I do

Export["myfile.txt", mylist1, "Table"]

I obtain a file with

1 2
3 4

which is ok for a single sublist, but I don't know what to do in order to export all the sublists to the same file separated by an empty line.

$\endgroup$
2
$\begingroup$

You can combine the matrices into a single matrix, and then export:

combined = Join[mylist1, {{}}, mylist2];

ExportString[combined, "Table"]
1   2
3   4

5   6
7   8

If you have many lists, you can use Riffle and Catenate to join the lists together:

ExportString[Catenate @ Riffle[mylist, {{{}}}], "Table"]
1   2
3   4

5   6
7   8
$\endgroup$
  • $\begingroup$ Thank you! I did use your second suggestion having multiple sublists with Export["myfile.txt",ExportString[Catenate@Riffle[mylist, {{{}}}], "Table"]] $\endgroup$ – Emmet Jun 22 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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