5.18.2010

Data transfer from Matlab to Stata and reverse

Zip file with Matlab code to export or import datasets to Stata. For use in conjunction with "insheet/outsheet". Importing data from Stata is restricted to numeric variables (but not the reverse).


(also includes scripts for dropping missing obs in matlab)

enjoy!

http://dl.dropbox.com/u/3011470/stata_translation.zip

-------------------------------------

matlab to stata

in matlab:

cell2csv(cellarrayname, 'dataname')

then in stata:

insheet using dataname.csv

-------------------------------------

stata to matlab

in stata:

outsheet varlist using dataname, comma

then in matlab:

data = csv2mat_numeric('dataname.out')

6 comments:

  1. Hi...

    First - Jesse you were a great TA

    Second - Is there any way to open a matlab file with stata without having matlab?

    ReplyDelete
  2. Thanks My! It doesn't look like there's a straightforward way of doing it without using Matlab itself. Apparently later versions of Matlab save .mat files in a format that can be read with R.

    http://stackoverflow.com/questions/7713422/convert-mat-file-to-r-compliant-file

    Perhaps you could try that?

    ReplyDelete
  3. This could be just what I need! Just a question before I dive into this: I need to run two iterative procedures, one in STATA and on in MATLAB, and I need to send newly created results back and forth from one program to the other, and I need to have this automated as I have hundreds of thousands iterations. So essentially, I need to call a STATA dofile from Matlab as a subroutine, and reverse.

    I presume your command just works interactively (user sitting in front of screen and doing manually),and could not be built into scripts?

    I guess I will have to estimate my model either all in STATA, but I am not happy using MATA, or in MATLAB, but I would need to program my estimator(which is precanned in STATA). Can your command spare me from those options?

    Thanks! Katharina.

    ReplyDelete
    Replies
    1. If your script knows the name of the files you are using, you should be able to automate the transfers of data by calling the function from the script.

      Delete
  4. I modified the csv2mat_numeric function a little bit so that it can import also “mixed” data (numeric and string variables) and created a function csv2mat_mixed (download: http://www.columbia.edu/~sl3382/matlab.html). However, it requires the number of variables and the number of observations per variable along with a vector specifying the string variable positions in the csv file as inputs. Therefore, if the data set doesn't actually include string variables, csv2mat_numeric should be preferred since it only needs the filename as input.

    ReplyDelete