MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: using file txt in mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49421] Re: using file txt in mathematica
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Mon, 19 Jul 2004 07:46:11 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/18/04 at 8:09 AM, tarpanelli at libero.it (paolo ) wrote:

>If I save an output from a module in a txt file, how can i use it
>again in one other module? For example:

>The output from my module is

>a = {1,2,3,4}{5,6,7,8}{9,10,11,12}{13,14,15,16}

>save this output is a .txt file format

>TextForm @ TableForm[a] >> "a.txt"

>then load the file txt

>!! a.txt

>but how can I use, in one other module, the single elements of
>matrix "a" that I saved in the file .txt?? for example how can i do
>this:

>a[[1]][[3]] * y ???????

This last implies a is a matrix and the line above defining a should have been

a = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}

There are a couple of approaches to your problem.

First, rather than using Put (>>) to write a out to a file, you could use DumpSave["a.mx",a]. DumpSave writes out all information associated with a symbol, including the name. It makes it possible to do something like

DumpSave["a.mx",a];
Remove@a;
<<a.mx
a[[1]][[3]] y

and get what you expect.

But the file format used by DumpSave is binary not text. So, if you want to use the file in something other than Mathematica, this will be a problem. In fact, the format used by DumpSave is not constant from platform to platform. So, if I save symbols using DumpSave on my Mac, my colleagues using Windows based machines will not be able to use the resulting file.

If it is essential to have cross platform compatibility, the simplest approach is to use either Put or Export. If human readability is important, probably best is

Export["a.txt". a, "Table"];

Export evaluates a and writes out the result to a.txt. So, the name, "a", is lost. To do something like

a[[1]][[3]] y

you will need to restore a with either

a = Import["a.txt", "Table"]  or
a = ReadList["a.txt", Number, RecordLists->True]; 

The second form is faster and will work fine if the file consists of nothing but numbers.

It is also possible to create text files that act like the files created using DumpSave. For example, you could do:

str-OpenWrite["a.txt"];
WriteString[str, "a=", a];
Close[str];

then later

<<a.txt

would allow usage of a[[1]][[3]]
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: importing image and getting numbers from the gray intensity
  • Next by Date: Farey Comb Histogram
  • Previous by thread: Re: using file txt in mathematica
  • Next by thread: Return repeated values of a list