Convert MATLAB code to R

Is there a tool for converting MATLAB code to R?

I have a lot of code that needs to be converted from MATLAB to R. It doesn't have to be accurate, but it will be helpful in giving a head start.

Answers


Paul Gilbert provides a rough Bash script that could get you started (he claims it will convert about 80% of the way) on the R mailing list:

#!/bin/csh
cp $1 $2
ex -s $2 <<eof
   g/%/s//#/g
   g/function\(..*\)=\(..*\)(\(..*\)/s//\2 <-function( \3 { \1/
   g/end/s//   } #/
   g/for\(..*\)=\(..*\):\(..*\)/s//for ( \1 in \2 : \3 ) {/
   g/_/s//./g
   g/;/s///g
   g/==/s//@@/g
   g/=/s//<-/g
   g/@@/s//==/g
   g/zeros(/s//matrix(0,/g
   g/ones(/s//matrix(1,/g
   g/eye(/s//diag(1,/g
   g/\/s//solve(,)/g
   g/fsolve('\(..*\)'/s//ms(~\1 /g
   g/param(\(..*\))/s//param[ \1 ] /g
   g/var(\(..*\))/s//var[ \1 ] /g
   g/mod1(\(..*\)/s//mod1[ \1 /g
   wq
eof

No there is no easy conversion. Some will translate nearly exactly, some will translate only with great pain and suffering. At least you'll be using R though! Start here to work out analogous functions and syntax:

http://cran.r-project.org/doc/contrib/R-and-octave.txt

http://cran.r-project.org/doc/contrib/Hiebeler-matlabR.pdf

When you get stuck please ask specific questions here. This is really too vague as it stands, though those reference cards will help with getting started.


An alternative to translating the code would be to call MATLAB from within R, using the RMatlab package.

I have not tried RMatlab, but the package description states:

This package provides methods to read and write MAT files. It also makes it possible to communicate (evaluate code, send and retrieve objects etc.) with Matlab v6 or higher running locally or on a remote host.


Please see the comments on this issue: URL:https://mandymejia.wordpress.com/2014/08/18/three-ways-to-use-matlab-from-r/

She mentions several options:

Option 1: Run a single MATLAB command at a time using system()

Option 2: Use R.matlab to send code to the MATLAB server

Option 3: Write an entire MATLAB program using writeLines() and run using system()


Need Your Help

Java, convert lat/lon to UTM

java geolocation latitude-longitude geography geotools

Does anyone know of a way, in Java, to convert an earth surface position from lat, lon to UTM (say in WGS84)? I'm currently looking at Geotools but unfortunately the solution is not obvious.