2
$\begingroup$

I have a notebook I run for different values of aspect ratio A all integers except 2.5 and copies its multiple plot results to my hard disc. So when I change A and rerun it, I have to manually change all the file output names to avoid getting the existing results for the previous A overwritten. This is time consuming and mistake prone. What is the syntax for a file name that say has a numerical ending of the current value ot A please?

$\endgroup$
  • 2
    $\begingroup$ "fileName"<>TextString[NumberForm[A,{1,1}]]<>".txt" or A = 2.2; "fileName" <> StringReplace[#, "." -> "_"] &@ TextString[NumberForm[A, {1, 1}]] <> ".txt" $\endgroup$ – Rolf Mertig Oct 9 at 14:04
  • 1
    $\begingroup$ Nice one, @RolfMertig I always like to make anonymous functions for this type of thing. Something like: "filehead-"<>ToString[#]&<>"-"<>ToString[A]<>".extail"/@Range@10 $\endgroup$ – CA Trevillian Oct 9 at 14:41
  • $\begingroup$ @simon Why don't you paste your export command? $\endgroup$ – Rolf Mertig Oct 9 at 16:49
  • $\begingroup$ Try Export[FileNameJoin[{$HomeDirectory, "eflut3i.tiff"}], out] $\endgroup$ – Rolf Mertig Oct 10 at 10:57
  • $\begingroup$ Sorry where does the integer A enter in the name to replace my specific value of 3? $\endgroup$ – simon Oct 10 at 14:37
4
$\begingroup$

Regarding the commented example (it is wise to update your post with such things, for future reference) one would do something like the following:

A=3;
out[A_,function_]:=outfunc[A,function];
(*where outfunc[arg, argfunc] is the function based on arg and argfunc one would like to export*)

outexporter[A_,function_,driveletter_]:=outexporter[A,function,driveletter]=Export[ToString[driveletter]<>":\\"<>ToString[function]<>ToString[A]<>"i.tiff", out[A,function],Resolution -> 300]

Hope this helps! Please, let me know if I can explain anything to ya? :D!

$\endgroup$
  • $\begingroup$ @simon thank you for the kind words! I use this method in my normal workflows where I inevitably use it to make files which have dependencies of 8-10 variables that can change, & it works like a charm. The trick is to weave together various combinations of ToString[varID] and just plain-old varString if you’re setting options with atoms. <> is the shortcut for StringJoin[] and you’ll find that if you keep a consistent data structure, you can make custom Import and Export functions that are evaluated purely by your variable inputs. So I just copy and paste the same inputs for each! $\endgroup$ – CA Trevillian Oct 21 at 2:12
  • $\begingroup$ @simon nice! So then, this method should work for you! What do you have so far? I’m not sure what you mean by the “internal Mathematica graph name”, nor the “stub output name” but most everything else should be easily achieved with minor modifications to my answer :) $\endgroup$ – CA Trevillian Oct 21 at 13:45
  • $\begingroup$ @simon I’m not sure what you mean or are asking? You should make re a function of A by simply adding [A_]: between re and =. Then, having done this, you will call re[A] instead of whatever other function you may want to call. So in the end you have re used instead of function when you run outexporter and then outfunc==re. Ie out[A,re] evaluates the output used in outexporter[A,re,C] that gives you the desired effects! $\endgroup$ – CA Trevillian Oct 21 at 19:57
  • $\begingroup$ Let us continue this discussion in chat. $\endgroup$ – CA Trevillian Oct 21 at 20:43
4
$\begingroup$

You may use StringTemplate.

For values of some parameter.

tFilename = StringTemplate["AValueAt`1`.gif"];
tFilename /@ {1, 2, 2.5, 3}
{"AValueAt1.gif", "AValueAt2.gif", "AValueAt2.5.gif", "AValueAt3.gif"}

You can also include the plot result name.

tFilename2 = StringTemplate["Step`1`_AValueAt`2`.gif"];
tFilename2 @@@ {{"One", 1}, {"Two", 2.5}, {"Three", 3}}
{"StepOne_AValueAt1.gif", "StepTwo_AValueAt2.5.gif", "StepThree_AValueAt3.gif"}

StringTemplate can also use named slots with Association.

tFilename3 = StringTemplate["Step`StepName`_AValueAt`Value`.gif"];
tFilename3@<|"StepName" -> "Association", "Value" -> 4|>
"StepAssociation_AValueAt4.gif"

Hope this helps.

$\endgroup$
1
$\begingroup$

Credit to CA Trevillian, the short simple answer I wanted which works is

Export["c:/.graph#"<>ToString[A]<>".tiff", out, ImageResolution -> 300]

where <>ToString[A]<> is the sought insertion for the numerical value of A.

$\endgroup$
  • 2
    $\begingroup$ I don't think this is a "thank you" post. I see it as a self-answer where the OP acknowledges the help received from CA Trevillian.. $\endgroup$ – m_goldberg Oct 24 at 4:09
  • $\begingroup$ Well, my answer was originally accepted but then unaccepted, my intention was to provide a general method with functional approach && I managed that! I hoped OP was able to understand how they could use it, and, I think they landed at a comprehension in the end. Without a functional approach to your project notebook, if one is just setting the A variable, then this would work. You can also, of course, just insert the global A into the format I & others provided! As to which is better, is up to the use case, I think. $\endgroup$ – CA Trevillian Oct 24 at 13:50

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.