Announcement

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

  • Export string matrix from Mata to csv

    I have a string matrix in Mata that I would like to write to csv. This post: https://www.statalist.org/forums/for...=1503609503040 discusses one possible solution, namely that I could exit mata and then use
    getmata (var*)

    This does work. But, it seems to require me to clear all of the data from Stata ahead of time, since the matrix that I have in Mata is not of the same dimensions as my data.

    Is there any way to get a string matrix from mata to a csv file? I'm fine with a solution either that works directly from mata or one that involves first exporting the string matrix from mata to stata and then uses something like mat2txt to get it to csv.

  • #2
    You could use the Mata functions fopen and fwrite to write to the csv file, and maybe combine the invtokens function with the strofreal function to create the comma-separated strings that you will write.

    But you would need to be careful about it, and be a bit experienced in the nuances of the csv format:

    https://tburette.github.io/blog/2014...-own-CSV-code/


    That said, if you know you won't need to enclose your output in quotes, escape it, etc. you reduced the complexity to at most 20 lines of Mata code (YMMV).

    Comment

    Working...
    X