Mathematica File names in a Do Loop: Mathematica

Click For Summary
To create multiple files in a loop using Mathematica, the recommended approach is to use the Do function. The example provided demonstrates how to generate filenames dynamically by concatenating strings. The correct syntax involves using Do[Put[StringJoin["text_", ToString[i], ".txt"]], {i, 10}], which will create ten empty files named text_1.txt through text_10.txt in the current directory. It's important to note that the underscore character has a special meaning in Mathematica, so it should be avoided in file naming unless intended for pattern matching. This method effectively automates file creation based on loop indices.
ilvreth
Messages
33
Reaction score
0
Does anybody know how i can make files in a do loop in mathematica?

For example i want a do loop as (here is not mathematica language)

Code:
do i=1,10
make file = text_i.txt  (here i is the index of the do loop)
end do

The results of this process would be ten files as
text_1.txt
text_2.txt
text_3.txt
.
.
.
text_10.txt
How this can be written in mathematica?
 
Physics news on Phys.org
In[1]:= For[i=0,i<3,i++,
name="text"<>ToString[i];
Print[name];
]

From In[1]:= text0
From In[1]:= text1
From In[1]:= text2

Underscore "_" has a very special meaning in Mathematica and you almost certainly do not want to do that
 
Do[Put[StringJoin["text_", ToString, ".txt"]], {i, 10}]
will create the 10 empty files in the current Directory[]
 
Thank you!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K