2
$\begingroup$

I often need to export many similar plots with just one parameter changed. I like to include the parameter in the file name. For example

Export["Figure_" <> ToString[#] <> ".pdf", Plot[# x, {x, 0, 1}]] & /@ {1, 2, 3, 4}
(* {"Figure_1.pdf", "Figure_2.pdf", "Figure_3.pdf", "Figure_4.pdf"} *)

However sometimes the parameters are not so nice, e.g. {0.000001,-5,6.23}. I do not want to include the decimal point in the file name, as this could cause some issues if it is interpreted as the beginning of a file extension. In addition I would like to replace the minus sign with an alternative character or strategy.

$\endgroup$
  • 1
    $\begingroup$ I suggest you to save the parameters in a file and name the figures according to the order of the parameter in the file. $\endgroup$ – Wen Chern Oct 11 '19 at 18:53
  • $\begingroup$ @WenChern Thank you, a sensible suggestion in many situations. However I am often exporting a few (say 8) figures that I then drag and drop into presentations (for science communication purposes) so it's useful to quickly see the parameters in the file names. $\endgroup$ – Tom Oct 14 '19 at 11:54
3
$\begingroup$

I have defined a custom function that I find quite convenient

fileName[base_, n_, r_] :=
 StringJoin[{
   base,
   "_",
   StringReplace[ToString[Round[n, r]], {"." -> "p", "-" -> "m"}],
   ".pdf"
   }]

It replaces the decimal point with the letter "p" (for "point"). It replaces minus sign with "m". And you can truncate the number with r for rounding. Here are some example usage

fileName["fig", #, 0.01] & /@ {0.23, -5.2, 10, 0.00001}
(* {"fig_0p23.pdf", "fig_m5p2.pdf", "fig_10p.pdf", "fig_0p.pdf"} *)

fileName["fig", #, 1] & /@ {-2, -1, 0, 1, 2}
(* {"fig_m2.pdf", "fig_m1.pdf", "fig_0.pdf", "fig_1.pdf", "fig_2.pdf"} *)

It's not perfect, but I quite like it.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.