3
$\begingroup$

I have Mathematica notebook that performs some operation say

a=2
b=3
c=5
d=4

how do I set the output file after evaluation to be named using the values $2,3,5,\ldots$

like "2_3_5_4_" such that the the naming automatically takes the values assigned to a,b,c d

$\endgroup$
1
  • $\begingroup$ Take a look at ToString, StringJoin etc. $\endgroup$
    – Kuba
    Commented May 13, 2014 at 18:32

2 Answers 2

7
$\begingroup$

Your data

{a, b, c, d} = RandomInteger[9, 4];
data = a b c d;

Exporting with nice file-names featuring date and variable values using StringTemplate

Export[
 StringTemplate[
  "Date`1`_Values_a`2`_b`3`_c`4`.txt"
  ][DateString[{"Year", "Month", "Day"}], a, b, c]
 , data]
"Date20141201_Values_a7_b5_c3.txt"

Or using ToString and StringJoin as suggested by @Kuba

 Export[
  StringJoin["Data_", ToString /@ Riffle[{a, b, c, d}, "_"], ".txt"]
 , data]
"Data_7_5_3_7.txt"
$\endgroup$
0
$\begingroup$

There is a simple way if you are using the Table[] function. For example: Table[Export[NotebookDirectory[]<>ToString@StringForm["test``.txt", l], Table[{l,l^2}, {l, 1, 10}];. In this case, the program will generate ten files written as "testl.txt" for each l with the values l and l^2 as columns.

$\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.