Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Mata fopen() and memory, manual error

From   Jesper Kjr Hansen <>
To   <>
Subject   st: RE: Mata fopen() and memory, manual error
Date   Wed, 25 Jan 2006 22:27:23 +0100


you are correct in saying, that you can store muliple matrices in 
the same file using fopen() and fputmatrix().

I don't know if there are any memory issues involved in fopening 
and closing files containing multiple matrices, but I have made
another observation, which might also be of relevance to you.

Both fputmatrix() and fgetmatrix() has file handle as an 
argument, ensuring that you write to or read from the correct 
file. However, no matrix handle is defined, meaning that you have 
to fgetmatrix() your matrices in the same order as you 
fputmatrix() them. E.g. (from the online help):

The following code creates a file containing three matrices

        fh = fopen(filename, "w")
        fputmatrix(fh, a) 
        fputmatrix(fh, b) 
        fputmatrix(fh, c) 

    and the following code reads them back:

        fh = fopen(filename, "r")
        a = fgetmatrix(fh) 
        b = fgetmatrix(fh) 
        c = fgetmatrix(fh) 

For instance, you can't do:

        fh = fopen(filename, "r")
        c = fgetmatrix(fh) 
        b = fgetmatrix(fh) 
        a = fgetmatrix(fh) 

I don't know is this is a problem for you, but it has been for 


Matissa Hollister wrote:
> The Mata manual says that fputmatrix stores matrices
> in a compact and efficient format.  Also, assuming my
> reading is correct, more than one matrix can be saved
> into a single file.  I'm planning to run analysis that
> will do several iterations of a mata function I have
> written with a moderately large matrix as the output
> (perhaps as large as 1000x1000).  My inclination is to
> append the resulting matrices all to one file, thereby
> reducing clutter in my folders.  When I finally use
> the results, though, Ill only use one matrix at a
> time (passing it into a clustering program).  I'm
> wondering whether there are memory or other reasons
> for keeping the matrices in separate files.  For
> instance, when you use the command fopen()does it read
> the entire contents of the file into memory?  If so,
> then obviously creating separate files would use up
> less memory.  My impression, though, is that fopen()
> does not read the file into memory, it would only read
> in the matrix when I specify fgetmatrix().  But I
> cant find documentation to support this and I dont
> have a good enough background in programming to be
> sure.

Kind regards,

Jesper Kjr Hansen
Student Assistant
Department of Statistics
University of Southern Denmark

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index