Mathematica: export to a variable path

It's complicated. Once the path to export data to Mathematica is under quotation marks, how can I insert a variable as part of the path? In other words, I'm inside a loop that increments VAL and wants to export MyData to VAL.dat. Ideas?

pseudocode: Export ["~ / Documents / VAL", MyData]

+3
source share
3 answers

In addition to the answers of Howard and Mr. Wizard, I could say that it would be nice to look FileNameJoinfor a nice, system-independent way of arranging line strings and IntegerStringwhich you could use to convert integers to strings with a fixed number of positions, which makes your files are more beautiful:

In[33]:= VAL = 32;
IntegerString[VAL, 10, 4]

Out[34]= "0032"

( ), -

Export["directoryPart\\FixedFileNamePart"<>IntegerString[VAL, 10, 4]<>".dat","TSV"]

"TSV" , . , , . escape, ; . , UNIX-, . FileNameJoin, .

+13

:

"~/Documents/"<>ToString[VAL]
+4

StringReplace:

Table[
  StringReplace[
     "~/Documents/#.dat", 
     "#" :> IntegerString[VAL, 10, 4]],
  {VAL, 27, 29}
]
   {"~ / Documents / 0027.dat", "~ / Documents / 0028.dat", "~ / Documents / 0029.dat"}

"#" was arbitrarily selected as a placeholder. You can also use a different character or character string.

+1
source

All Articles