2
$\begingroup$

I am generating a table with 4 columns, wheras only the columns 3 and 4 have the same length.

alloutput = 
 TableForm[
  Table[{10^(j), 
    Table[{N[10^(i)], 
      Table[{l, 
        N[ArcTan[BesselJ[l, y'[l/j]]]] /. 
         With[{epsilon = 10^(-10), mphi = 10^(i), end = 10^3}, 
          First@NDSolve[{y''[
                x] + (0.001^2 + 1/(x)*Exp[-mphi*x/(10^(j)*0.01)] - 
                  l (l + 1)/(x^2))*y[x] == 0, y[epsilon] == 1, 
             y'[epsilon] == (l + 1)/(epsilon)}, 
            y, {x, epsilon, end}]]}, {l, 0, 5, 1}]}, {i, 0, 1, 
      1}]}, {j, 2, 3, 1}]]

Now the aim is to export the obtained data in a certain way. Namely to create for each new value in the first column (10^j) a new folder. In this very folder the data from column 3 and 4 should be written seperatly into a data file corresponding to the right value of column 2 (10^i).

Since this might be a bit confusing, I want to give an example:

The values of column 1 are in this example 100 and 1000. So I would like to create the folders with name folder100 and folder1000. In each of both folders then should be generated data files with the name of the value of column 2 (i.e. file1 and file10). Now finally in these data files I would like to write the corresponding values of the columns 3 and 4 in table form.

I guess one has to use CreateDirectory and ToString but I dont get it right.

$\endgroup$
4
$\begingroup$

I think this does what you want.

exportToFolder[{dir_, data : {{_, _?MatrixQ} ..}}] :=
  With[{path = "folder" <> ToString @ dir},
    CreateDirectory @ path;
    Export[FileNameJoin[{path, ToString @ #}], #2, "Table"] & @@@ data;
  ]

Make sure you first remove the TableForm wrapper from your data:

rawoutput = First @ alloutput;

Then your export may be performed with:

Scan[exportToFolder, rawoutput]
$\endgroup$
  • $\begingroup$ Thats a very nice idea! However, how can the files be named like file1.dat, file10.dat instead of 1.,10. ? $\endgroup$ – stef Jul 7 '16 at 11:31
  • 1
    $\begingroup$ okay got it, just replaced ToString @ # with "file" <> ToString@# $\endgroup$ – stef Jul 7 '16 at 14:33
  • 1
    $\begingroup$ @stef Glad I could help, and yes, that's the way I would do it. Let me know if you have any problems with this. Also perhaps see: (20412) $\endgroup$ – Mr.Wizard Jul 7 '16 at 20:44

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.