Message Boards Message Boards

0
|
3301 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[✓] Write to files that have different names

Hi there,

I am working on a notebook that tries to solve a system of three equations that depend on a parameter, and for each value of that parameter (in a custom interval) I want to write the solutions to an external file.

I saw that one can use Put or just the >> operator and write the name of the file. The thing is that I want to write those solutions in separate files, where the name of the file will depend on three values: say I have l1,l2,l3, then I want a filename called output_l1_l2_l3.dat. In my code, l1, l2 and l3 take values from 1-5, so I would have a lot of output files to generate. I was thinking of doing this in a Do procedure. I can do my calculations fine for just one file, but I have no idea how to generate another file with the corresponding name in the same Do loop.

Any ideas how can I do this? I attached a simple document with a sketch of what I want to do (not the actual notebook)

Thank you in advance!

Attachments:
POSTED BY: Robert Poenaru
2 Replies
Posted 4 years ago

Hi Robert,

Instead of hardcoding the file name, you can use StringTemplate to dynamically generate the file name.

{k1, k2, k3} = {3, 4, 5};
StringTemplate["output_``_``_``.dat"][k1, k2, k3]
(* output_3_4_5.dat *)
POSTED BY: Rohit Namjoshi

Thank you. This works great!

POSTED BY: Robert Poenaru
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract