Skip to main content

notebooks - How can I automatically name files being exported based on input parameters


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




Answer



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"

Comments