Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing saved matrix name to mata from stata

    Dear fellow Mata enthusiasts --

    I have the following seemingly simple problem for which I can find no workaround. The problem involves passing a local to Mata from Stata, where the local contains the name of a saved matrix. What I want to do is load up the saved matrix using name contained in the local. For example:
    Code:
    local MatName "SomeMatrix"
    mata
         file=st_local("MatName")
         mata matuse file
    end
    Running the above code gives me the error:
    Code:
    file file.mmat not found
    r(601);
    So, the problem is I can't get mata to use the contents of file, rather than "file" itself, in the matrix importing command.

    Is there any way around this?

    All the best,

    Matt

  • #2
    If you're using matuse in interactive mode, you can use directly the local.

    Code:
    local MatName "SomeMatrix"
    mata
         mata matuse `MatName'
    end
    By the way this command is only for interactive use. If you want to do something similar in non-interactive use fgetmatrix()

    Comment


    • #3
      You can just use the macro directly in the Mata code.
      Here is a silly example that stores a Mata copy of regression coefficients in a
      temporary file, clears Mata, then reads the matrix back in.
      Code:
      sysuse auto
      regress mpg turn trunk
      tempfile bmat
      mata: b = st_matrix("e(b)")
      mata: b
      mata: mata matsave `"`bmat'"' b
      mata: mata matdescribe `"`bmat'"'
      mata: mata clear
      mata: mata d
      mata: mata matuse `bmat'
      mata: mata d
      mata: b

      Comment


      • #4
        Christophe and Jeff --

        Very good! With reference to Christophe's comment, I outsmarted myself by making things harder than they had to be. And Jeff's comment reminded me of the nested quotes - which I never seem to be able to get the hang of!

        Thanks,

        Matt

        Comment

        Working...
        X