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   Matissa Hollister <[email protected]>
To   [email protected]
Subject   st: RE: Mata fopen() and memory, manual error
Date   Thu, 26 Jan 2006 07:21:02 -0800 (PST)

I had also noticed the problem Jesper pointed out,
that the individual matrices are not saved with names
or handles with fopen() and fputmatrix().  I was
hoping that maybe this wasn't the case, and in fact
this brings up a second error in the Mata manual on p.
188 under "mata matsave".  The manual refers to the
use of fopen() when programming and suggests that you
could save a matrix with the command fputmatrix(fh,
X).  It then says you could retrieve it with the
following commands:

fh=fopen("mymatrix.myfile", "r")
fgetmatrix(fh, X)
fclose(fh)

The fgetmatrix command here is in contrast to the
syntax used under the fopen() documentation, which
would be:

X = fgetmatrix(fh)

I was hoping that perhaps this was an alternate syntax
that would allow the user to pull out a specific
matrix from the file, but when I tried it I got an
error saying "wrong number of arguments for
fgetmatrix()".  So I'm assuming that the documentation
under mata matsave is wrong.

It's too bad that mata matsave is only allowed
interactively, since it appears to save files in the
format I'd prefer.  I'm assuming there's a good reason
programming-wise why the two different commands and
formats exist, though.

On a completely different note, I'm wondering about
replying to posts when I get the statalist in digest
form.  I frequently use the archives to look up
information and therefore appreciate the posts being
ordered into threads.  Since I get the messages in
digest form, though, I can't just hit reply to a post
and as far as I can tell there is no option in the
archives or other websites to post replies to past
messages.  I'm not sure of the mechanics of how the
archives keep track of threads.  I'm testing here one
approach which I hope will work, which is copying the
subject line of the original post with an "re:" in
front of it.  Other suggestions would be welcome.

thanks,

Matissa

------------------------------------
Matissa,

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) 
        fclose(fh)

    and the following code reads them back:

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

For instance, you can't do:

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

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

/Jesper


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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