Search code examples
wolfram-mathematica

Labeling the name of a file with a given numerical parameter


With python I can save a file labeling its name with a given parameter t in the following way

import matplotlib.pyplot as plt
fig=plt.figure(1)
plt.plot([1,2,3,4])
t=0.1
fig.savefig("filename%f.png" % t)

and the name of the saved figure will be something like "filename0.1000.png". How can I do the same with wolfram mathematica? In other words, what is the mathematica equivalent of %f ?


Solution

  • In Mathematica you can use StringTemplate:

    filenameTemplate = StringTemplate["filename`n`.dat"];
    filename = filenameTemplate[<|"n" -> 1234|>]
    (* "filename1234.dat" *)
    

    This will create a filename with a number from the Association:

    <|"n" -> 1234|>