1
$\begingroup$

I want to export sequence text files. I can Export a single text file, designating the Directory, however I cannot make it to several files.

I thought if I have data called input, I can get 6 text files in the current directory, such as "C:\testfolder\new1.txt", "C:\testfolder\new2.txt", ..., "C:\testfolder\new6.txt".

However {$Failed, $Failed, $Failed, $Failed, $Failed, $Failed} is returned.

If someone knows how to do this, please tell me.

input = {{{6, 7, 9, 1, 5, 5, 7, 3, 7, 4}, {2, 7, 3, 2, 0, 10, 1, 0, 4,
  3}, {0, 0, 4, 10, 0, 8, 7, 2, 2, 5}, {6, 1, 5, 10, 3, 1, 3, 10, 
 5, 7}}, {{6, 10, 6, 0, 0, 0, 2, 1, 0, 6}, {4, 2, 6, 5, 1, 8, 9, 
 7, 3, 9}, {4, 5, 8, 0, 9, 5, 4, 8, 9, 6}, {1, 0, 1, 2, 3, 7, 3, 
 4, 2, 3}}, {{4, 6, 9, 10, 3, 3, 0, 1, 7, 6}, {3, 2, 10, 8, 3, 2, 
 3, 6, 2, 2}, {10, 0, 3, 7, 1, 1, 3, 10, 5, 4}, {4, 7, 3, 2, 2, 4,
  9, 4, 3, 0}}, {{8, 9, 0, 10, 1, 7, 2, 10, 4, 0}, {9, 10, 4, 9, 
 4, 10, 0, 2, 5, 8}, {4, 6, 4, 10, 0, 7, 0, 0, 0, 5}, {8, 1, 1, 2,
  2, 1, 5, 7, 2, 8}}, {{0, 1, 1, 0, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 
 1, 1, 1, 0, 0, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 1, 0, 
 1, 1, 1, 1, 0, 1, 0}}, {{13, 4, 3, 6, 8, 19, 7, 3, 4, 12}, {6, 9,
  1, 9, 13, 11, 3, 11, 8, 2}, {12, 9, 4, 18, 14, 3, 8, 9, 12, 
 1}, {8, 11, 4, 3, 15, 0, 11, 7, 18, 2}}};

st = StringJoin["C:\\testfolder\\new", ToString[#], ".txt"] & /@ 
stringform = ToString[TextString[#] // InputForm] & /@ st
Export[stringform[[#]], input[[#]], "Table"] & /@ Range[Length[st]]
$\endgroup$
1
  • $\begingroup$ The line st = StringJoin["C:\\testfolder\\new", ToString[#], ".txt"] & /@ obviously makes no sense. Do you mis-copy the code sample? $\endgroup$
    – xzczd
    Commented Dec 21, 2022 at 8:48

1 Answer 1

1
$\begingroup$
input = {{{6, 7, 9, 1, 5, 5, 7, 3, 7, 4}, {2, 7, 3, 2, 0, 10, 1, 0, 4,
      3}, {0, 0, 4, 10, 0, 8, 7, 2, 2, 5}, {6, 1, 5, 10, 3, 1, 3, 10, 
     5, 7}}, {{6, 10, 6, 0, 0, 0, 2, 1, 0, 6}, {4, 2, 6, 5, 1, 8, 9, 
     7, 3, 9}, {4, 5, 8, 0, 9, 5, 4, 8, 9, 6}, {1, 0, 1, 2, 3, 7, 3, 
     4, 2, 3}}, {{4, 6, 9, 10, 3, 3, 0, 1, 7, 6}, {3, 2, 10, 8, 3, 2, 
     3, 6, 2, 2}, {10, 0, 3, 7, 1, 1, 3, 10, 5, 4}, {4, 7, 3, 2, 2, 4,
      9, 4, 3, 0}}, {{8, 9, 0, 10, 1, 7, 2, 10, 4, 0}, {9, 10, 4, 9, 
     4, 10, 0, 2, 5, 8}, {4, 6, 4, 10, 0, 7, 0, 0, 0, 5}, {8, 1, 1, 2,
      2, 1, 5, 7, 2, 8}}, {{0, 1, 1, 0, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 
     1, 1, 1, 0, 0, 1, 0}, {1, 1, 1, 0, 1, 0, 1, 0, 1, 0}, {0, 1, 0, 
     1, 1, 1, 1, 0, 1, 0}}, {{13, 4, 3, 6, 8, 19, 7, 3, 4, 12}, {6, 9,
      1, 9, 13, 11, 3, 11, 8, 2}, {12, 9, 4, 18, 14, 3, 8, 9, 12, 
     1}, {8, 11, 4, 3, 15, 0, 11, 7, 18, 2}}};

Dimensions@input

{6, 4, 10}

Create the file names:

st = StringJoin[{"C:/test/new", ToString[#], ".txt"}] & /@ Range[6]

{"C:/test/new1.txt", "C:/test/new2.txt", "C:/test/new3.txt",
"C:/test/new4.txt", "C:/test/new5.txt", "C:/test/new6.txt"}

Export[st[[#]], input[[#]], "Table"] & /@ Range[Length[st]]

OR

Using MapIndexed:

MapIndexed[
 Export["C:/test/new" <> ToString[First@#2] <> ".txt", #1, 
   "Table"] &, input]
$\endgroup$
3
  • $\begingroup$ Please explore the FileNameJoin command as well. $\endgroup$
    – Syed
    Commented Dec 21, 2022 at 7:25
  • 1
    $\begingroup$ Thank you so much!!! $\endgroup$
    – rani
    Commented Dec 21, 2022 at 9:17
  • $\begingroup$ Now I could successfully export them. $\endgroup$
    – rani
    Commented Dec 21, 2022 at 9:18

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.