Labeling the name of a file with a given numerical parameter










1















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 ?










share|improve this question


























    1















    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 ?










    share|improve this question
























      1












      1








      1








      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 ?










      share|improve this question














      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 ?







      wolfram-mathematica






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 4:35









      3sm1r3sm1r

      918




      918






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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|>





          share|improve this answer

























          • thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

            – 3sm1r
            Nov 15 '18 at 5:32






          • 1





            @3sm1r I have update the answer, I forget that StringForm prints the string.

            – m0nhawk
            Nov 15 '18 at 5:45






          • 1





            @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

            – Rohit Namjoshi
            Nov 15 '18 at 16:04











          • @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

            – m0nhawk
            Nov 15 '18 at 16:44


















          1














          If you want the output to be exactly the same as in your example:



          t = 0.1;
          "filename" <> ToString@NumberForm[t, 1, 4] <> ".png"
          (* filename0.1000.png *)


          EDIT



          StringTemplate is better if you need to do multiple replacements within the string (less messy string concatenation) and avoids some duplication if you need to use the same template in different places in your code. But for the latter case it would be better to encapsulate the filename generation in a separate function.



          StringTemplate has options for specifying a CombinerFunction and an InsertionFunction. The default InsertionFunction is TextString so there is no need for the ToString.



          t = 0.1;
          filenameTemplate = StringTemplate["filename`t`.png"];
          filename = filenameTemplate[<|"t" -> NumberForm[t, 1, 4]|>]
          (* filename0.1000.png *)


          And there is a whole lot more that can be done with the templating system. See the docs for details.






          share|improve this answer

























          • Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

            – 3sm1r
            Nov 15 '18 at 16:47






          • 1





            @3sm1r Answer edited with response to your question.

            – Rohit Namjoshi
            Nov 15 '18 at 18:37











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312503%2flabeling-the-name-of-a-file-with-a-given-numerical-parameter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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|>





          share|improve this answer

























          • thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

            – 3sm1r
            Nov 15 '18 at 5:32






          • 1





            @3sm1r I have update the answer, I forget that StringForm prints the string.

            – m0nhawk
            Nov 15 '18 at 5:45






          • 1





            @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

            – Rohit Namjoshi
            Nov 15 '18 at 16:04











          • @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

            – m0nhawk
            Nov 15 '18 at 16:44















          1














          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|>





          share|improve this answer

























          • thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

            – 3sm1r
            Nov 15 '18 at 5:32






          • 1





            @3sm1r I have update the answer, I forget that StringForm prints the string.

            – m0nhawk
            Nov 15 '18 at 5:45






          • 1





            @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

            – Rohit Namjoshi
            Nov 15 '18 at 16:04











          • @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

            – m0nhawk
            Nov 15 '18 at 16:44













          1












          1








          1







          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|>





          share|improve this answer















          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|>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 16:43

























          answered Nov 15 '18 at 5:13









          m0nhawkm0nhawk

          15.8k83263




          15.8k83263












          • thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

            – 3sm1r
            Nov 15 '18 at 5:32






          • 1





            @3sm1r I have update the answer, I forget that StringForm prints the string.

            – m0nhawk
            Nov 15 '18 at 5:45






          • 1





            @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

            – Rohit Namjoshi
            Nov 15 '18 at 16:04











          • @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

            – m0nhawk
            Nov 15 '18 at 16:44

















          • thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

            – 3sm1r
            Nov 15 '18 at 5:32






          • 1





            @3sm1r I have update the answer, I forget that StringForm prints the string.

            – m0nhawk
            Nov 15 '18 at 5:45






          • 1





            @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

            – Rohit Namjoshi
            Nov 15 '18 at 16:04











          • @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

            – m0nhawk
            Nov 15 '18 at 16:44
















          thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

          – 3sm1r
          Nov 15 '18 at 5:32





          thanks, problem: I tried Export[StringForm["filename``.dat", 2], file] but the output was the following "First argument filename2.dat is not a valid file specification.", whereas if I directly write Export["filename2.dat",file] it works

          – 3sm1r
          Nov 15 '18 at 5:32




          1




          1





          @3sm1r I have update the answer, I forget that StringForm prints the string.

          – m0nhawk
          Nov 15 '18 at 5:45





          @3sm1r I have update the answer, I forget that StringForm prints the string.

          – m0nhawk
          Nov 15 '18 at 5:45




          1




          1





          @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

          – Rohit Namjoshi
          Nov 15 '18 at 16:04





          @m0nhawk Why the number = 1;? Also the output above is incorrect, should be filename1234.dat.

          – Rohit Namjoshi
          Nov 15 '18 at 16:04













          @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

          – m0nhawk
          Nov 15 '18 at 16:44





          @RohitNamjoshi It was a redundant line, it takes the number from Association after the call. Thank you for noticing.

          – m0nhawk
          Nov 15 '18 at 16:44













          1














          If you want the output to be exactly the same as in your example:



          t = 0.1;
          "filename" <> ToString@NumberForm[t, 1, 4] <> ".png"
          (* filename0.1000.png *)


          EDIT



          StringTemplate is better if you need to do multiple replacements within the string (less messy string concatenation) and avoids some duplication if you need to use the same template in different places in your code. But for the latter case it would be better to encapsulate the filename generation in a separate function.



          StringTemplate has options for specifying a CombinerFunction and an InsertionFunction. The default InsertionFunction is TextString so there is no need for the ToString.



          t = 0.1;
          filenameTemplate = StringTemplate["filename`t`.png"];
          filename = filenameTemplate[<|"t" -> NumberForm[t, 1, 4]|>]
          (* filename0.1000.png *)


          And there is a whole lot more that can be done with the templating system. See the docs for details.






          share|improve this answer

























          • Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

            – 3sm1r
            Nov 15 '18 at 16:47






          • 1





            @3sm1r Answer edited with response to your question.

            – Rohit Namjoshi
            Nov 15 '18 at 18:37















          1














          If you want the output to be exactly the same as in your example:



          t = 0.1;
          "filename" <> ToString@NumberForm[t, 1, 4] <> ".png"
          (* filename0.1000.png *)


          EDIT



          StringTemplate is better if you need to do multiple replacements within the string (less messy string concatenation) and avoids some duplication if you need to use the same template in different places in your code. But for the latter case it would be better to encapsulate the filename generation in a separate function.



          StringTemplate has options for specifying a CombinerFunction and an InsertionFunction. The default InsertionFunction is TextString so there is no need for the ToString.



          t = 0.1;
          filenameTemplate = StringTemplate["filename`t`.png"];
          filename = filenameTemplate[<|"t" -> NumberForm[t, 1, 4]|>]
          (* filename0.1000.png *)


          And there is a whole lot more that can be done with the templating system. See the docs for details.






          share|improve this answer

























          • Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

            – 3sm1r
            Nov 15 '18 at 16:47






          • 1





            @3sm1r Answer edited with response to your question.

            – Rohit Namjoshi
            Nov 15 '18 at 18:37













          1












          1








          1







          If you want the output to be exactly the same as in your example:



          t = 0.1;
          "filename" <> ToString@NumberForm[t, 1, 4] <> ".png"
          (* filename0.1000.png *)


          EDIT



          StringTemplate is better if you need to do multiple replacements within the string (less messy string concatenation) and avoids some duplication if you need to use the same template in different places in your code. But for the latter case it would be better to encapsulate the filename generation in a separate function.



          StringTemplate has options for specifying a CombinerFunction and an InsertionFunction. The default InsertionFunction is TextString so there is no need for the ToString.



          t = 0.1;
          filenameTemplate = StringTemplate["filename`t`.png"];
          filename = filenameTemplate[<|"t" -> NumberForm[t, 1, 4]|>]
          (* filename0.1000.png *)


          And there is a whole lot more that can be done with the templating system. See the docs for details.






          share|improve this answer















          If you want the output to be exactly the same as in your example:



          t = 0.1;
          "filename" <> ToString@NumberForm[t, 1, 4] <> ".png"
          (* filename0.1000.png *)


          EDIT



          StringTemplate is better if you need to do multiple replacements within the string (less messy string concatenation) and avoids some duplication if you need to use the same template in different places in your code. But for the latter case it would be better to encapsulate the filename generation in a separate function.



          StringTemplate has options for specifying a CombinerFunction and an InsertionFunction. The default InsertionFunction is TextString so there is no need for the ToString.



          t = 0.1;
          filenameTemplate = StringTemplate["filename`t`.png"];
          filename = filenameTemplate[<|"t" -> NumberForm[t, 1, 4]|>]
          (* filename0.1000.png *)


          And there is a whole lot more that can be done with the templating system. See the docs for details.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 18:34

























          answered Nov 15 '18 at 16:35









          Rohit NamjoshiRohit Namjoshi

          32119




          32119












          • Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

            – 3sm1r
            Nov 15 '18 at 16:47






          • 1





            @3sm1r Answer edited with response to your question.

            – Rohit Namjoshi
            Nov 15 '18 at 18:37

















          • Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

            – 3sm1r
            Nov 15 '18 at 16:47






          • 1





            @3sm1r Answer edited with response to your question.

            – Rohit Namjoshi
            Nov 15 '18 at 18:37
















          Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

          – 3sm1r
          Nov 15 '18 at 16:47





          Ty, are there practical reasons to prefer ToString@NumberForm to StringTemplate or viceversa? Maybe your solution looks slightly more concise.

          – 3sm1r
          Nov 15 '18 at 16:47




          1




          1





          @3sm1r Answer edited with response to your question.

          – Rohit Namjoshi
          Nov 15 '18 at 18:37





          @3sm1r Answer edited with response to your question.

          – Rohit Namjoshi
          Nov 15 '18 at 18:37

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312503%2flabeling-the-name-of-a-file-with-a-given-numerical-parameter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo