Re: Problems in writing into a sequential filename
- To: mathgroup at smc.vnet.net
- Subject: [mg97109] Re: Problems in writing into a sequential filename
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 5 Mar 2009 04:56:59 -0500 (EST)
On 3/4/09 at 7:10 AM, xabart at gmail.com wrote: >I am enjoying the beautiful rules of programming in mathematica, and >I step onto a rock: How can I write datas into a sequential >filename? Let's explain myself, with an exemple below: >First I work on mathematica 6.0.2 on linux >I want to make a Do loop in witch I choose values for a variable: Sr >(here, 0.5 and 1) Then I concatenate a "pattern" and the value (here >RappSr and the value) and put the name into a variable (Nomfich) >then I open the file (It works) But then, the problem is it does not >write into the file, but it close it normally. >So How can I do this not-so-tricky-thing in Mathematica? >And by the way, I can't plot graphes into any Loop neither. >Thanks for all your help, Cheers Xavier >The code: >ClearAll["Global`*"]; >Do[ Nomfich = ToString["RappSr" <> ToString[Sr]]; >Print[Nomfich]; poiuy4 = OpenWrite[Nomfich]; >Sr >>> Nomfich; 1 >>> Nomfich; Close[Nomfich]; >, {Sr, {0.5, 1}}]; There are a variety of ways of creating sequential files in Mathematica. And while this could be done using an explicit Do loop, this would be my last choice. Suppose I wanted a list of things sent to various files with a given base name with a sequential number appended to that base name. I would do that using MapIndexed[Put[#1, basename<>ToString[#2[[1]]]<>ext&, listOfStuff] Or if I needed something more readable than what Put creates, MapIndexed[Export[basename<>ToString[#2[[1]]]<>ext, #1, "Table"]&, listOfStuff] Or if I wanted to use explicit values rather than sequential integers: MapThread[Put[#1, basename<>ToString[#2]<>ext]&, {listOfStuff, listOfValues= }] These simple examples can easily be embellished in numerous ways. For example, NumberPadding could be used to format numbers to a fixed width which could be very nice.