File names in a Do Loop: Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter ilvreth
  • Start date Start date
  • Tags Tags
    File Loop Mathematica
Click For Summary

Discussion Overview

The discussion revolves around generating file names in a loop using Mathematica. Participants explore how to create multiple files with sequential names based on a loop index, focusing on the syntax and functionality of Mathematica for this task.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant inquires about creating files in a loop, providing a pseudocode example for clarity.
  • Another participant shares a code snippet using a For loop to generate file names, noting the output of the names generated.
  • A third participant suggests using a Do loop with the Put function to create empty files, demonstrating a method to achieve the desired outcome.
  • A warning is issued regarding the use of the underscore character in Mathematica, indicating its special significance.

Areas of Agreement / Disagreement

Participants provide different methods for achieving the same goal, but there is no explicit consensus on a single best approach. The discussion remains open to various coding techniques.

Contextual Notes

Some participants highlight potential pitfalls, such as the special meaning of certain characters in Mathematica, which may affect file naming conventions.

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 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K