1
$\begingroup$

I want to export some sort of data from Mathematica to .txt files but every time with a different name.clearly I want the name of files change automatically in each loop for each series of data that are produced in each iteration.

s = -1 
While[s < 2*q = 2.;
  \[CapitalDelta] = 0.5;
  smin = -0.3;
  smax = 0.1;
  \[CapitalDelta]s = 0.01;
  L = 5;
  p = 2^L;
  s = s + 0.05;
  H = {{1, 0, 0, 0}, {0, 1 - (\[CapitalDelta] + 1)*q, E^s/q, E^s/q}, 
       {0, q E^s, 1 - (\[CapitalDelta] + 1)/q, q E^s}, 
       {0, \[CapitalDelta]*q*E^s, (\[CapitalDelta]*E^s)/q, 1 - (q + 1/q)}};
  IM = IdentityMatrix[2];
  row = ConstantArray[1, p];
  sum = row.H;
  Export["sum,L=5,q=2,d=0.5,s=-1.txt", sum];
  Export["H,L=5,q=2,d=0.5,s=-1.txt", Flatten[H], "Table"]
  ];

I want to change "s" in the name of the files each time with the real value of it in that itteration

$\endgroup$
6
  • $\begingroup$ You should be more specific (please give some code) but maybe this will help. $\endgroup$
    – SquareOne
    Feb 3, 2015 at 8:40
  • $\begingroup$ Do[Export["superfile-"<>ToString[i]<>".jpg",fig[[i]]],{i,1,10}] $\endgroup$
    – chris
    Feb 3, 2015 at 8:48
  • $\begingroup$ s = -1 While[s < 2*q = 2.; [CapitalDelta] = 0.5; smin = -0.3; smax = 0.1; [CapitalDelta]s = 0.01; L = 5; p = 2^L; s = s + 0.05; H = {{1, 0, 0, 0}, {0, 1 - ([CapitalDelta] + 1)*q, E^s/q, E^s/q}, {0, qE^s, 1 - ([CapitalDelta] + 1)/q, qE^s}, {0, [CapitalDelta]*q*E^s, ([CapitalDelta]*E^s)/q, 1 - (q + 1/q)}}; IM = IdentityMatrix[2]; row = ConstantArray[1, p]; sum = row . H; Export["sum,L=5,q=2,d=0.5,s=-1.txt", sum]; Export["H,L=5,q=2,d=0.5,s=-1.txt", Flatten[H], "Table"]] $\endgroup$ Feb 3, 2015 at 8:52
  • $\begingroup$ I want to change "s" in the name of the files each time with the real value of it in that itteration $\endgroup$ Feb 3, 2015 at 8:53
  • $\begingroup$ @sarakaviani I edited your question to include your code (which I can't get to run as written - the test for the While loop isn't constructed right I think). $\endgroup$
    – Jason B.
    Feb 3, 2015 at 9:36

4 Answers 4

3
$\begingroup$

Instead of "H,L=5,q=2,d=0.5,s=-1.txt" write for example:

"H,L=5,q=2,d=0.5,s="<>ToString[CForm[s]]<>".txt"
$\endgroup$
1
$\begingroup$

A very simple solution using TextString and StringJoin:

Export[StringJoin["C:\\Users\\...\\first_part_of_name", TextString[variable], ".txt"], sum];
$\endgroup$
2
  • $\begingroup$ very clean and neat answer, helped me a lot. $\endgroup$
    – Fierce82
    Dec 6, 2016 at 10:25
  • $\begingroup$ Export[StringJoin["C:/eflut", TextString[A], ".tiff"], out]; I want to label eflut graph of out with the value of A eg 3,4 but got 2 errors StringJoin::string: String expected at position 2 in C:/eflut<>TextString[3]<>.tiff. >> Export::chtype: First argument C:/eflut<>TextString[3]<>.tiff is not a valid file specification. >> $\endgroup$
    – simon
    Oct 3, 2019 at 15:17
0
$\begingroup$

You could generate the filenames using StringTemplate with a positional argument:

s=-1;
mytemplate := StringTemplate["sum, L=5, q=2, d=0.5, s=`svalue`.txt"]
filestring = mytemplate[<|"svalue" -> ToString[s]|>]

(* sum,L=5,q=2,d=0.5,s=-1.txt *)

$\endgroup$
0
$\begingroup$

There is a simple way if you are using the Table[] function. For example: Table[Export[NotebookDirectory[]<>ToString@StringForm["test``.txt", l], Table[{l,l^2}, {l, 1, 10}];. In this case, the program will generate ten files written as "testl.txt" for each l with the values l and l^2 as columns.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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