4

I have a lot of data sets combined in a big list where each data set comprises one first level sub-list. Each data set must be saved into a separate file. The names for the files must automatically assigned during export.

How to export different files at the same time?

The basic form of Export is Export["filename.ext", expr], I have not been able to adapt this basic for a sequence of file names, because I don't know how modify the string "filename.ext" to include a sequence number. How could I do that?

I want to generate ten file names using a loop to export the data automatically. I was thinking maybe an expression like

Do[Export["filename[[n]].txt", data[[n]]], {n, 1, 10}]

where the filename is a list {name_1, name_2, name_3, .., name_10}

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
qfzklm
  • 171
  • 4

2 Answers2

5

In version 10, you can use StringTemplate:

Export[StringTemplate["filename-`1`.txt"][#], data[[#]]] & /@ Range@10

I also like to use IntegerString to get numbers with leading zeros. In this case, base 10 and 3 digits.

Export[StringTemplate["filename-`1`.txt"]@IntegerString[#, 10, 3], data[[#]]] & /@ Range@10
Murta
  • 26,275
  • 6
  • 76
  • 166
4

As rasher wrote in a comment, you need to use some string manipulation in your Export expression to generate the file name on the fly.

Do[Export["filename" <> ToString[n] <> ".txt", data[[n]]], {n, 1, 10}]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257