0

I need to export from Matlab to Stata an array of numbers and strings.

For example:

clear

r=2;
n=4;
X=unifrnd(0,1,r,n);

X_STATA=reshape(X.', r*n,1);
id_STATA=kron((1:1:r).', ones(n,1));
mode_STATA=repmat(["AIR"; "TRAIN"; "BUS"; "CAR"],r,1);

Here, I want to export X_STATA, id_STATAand mode_STATA.

How can I do this?

In particular, I want to make sure that the numbers after the comma of the entries of X_STATA are well exported, without inappropriate approximations.

TEX
  • 2,249
  • 20
  • 43
  • 1
    Write it to a file and make sure you use 17 significant digits for double values ( https://stackoverflow.com/a/35626253/2732801 ) – Daniel Aug 31 '19 at 07:46

1 Answers1

2

In Matlab you can do the following:

writematrix(X_STATA, 'myfile.csv', 'precision', 17)

In Stata you import the data like this:

import delimited myfile.csv, asdouble
  • Thanks, should I do the same for `mode_STATA` which is a string? – TEX Aug 31 '19 at 12:24
  • Also, `writematrix` does not work in my Matlab (version 2017b). It tells me `Undefined function or variable 'writematrix'` – TEX Aug 31 '19 at 14:10
  • `writematrix` is the new unified function that handles both numeric and strings. An alternative is `dlmwrite`. Similar syntax but file name goes first and I think only handles numeric. You really need to study the help files. There is also the user-written function `dlmcell` which you can download for strings. –  Aug 31 '19 at 16:49