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.
If you just want a basic conversion with R syntax, calling “mat2r” with just the Matlab code. The function outputs a list with the converted code as “rCode” and the original code as “matCode”. This is so you can easily look at the differences between the two and what exactly the program is doing.
Option 2: Use R. R. matlab is a package that communicates with MATLAB, can read and write MAT files, and can pass objects to (and receive objects from) MATLAB. It's a little tricky to use at first, but the ability to pass objects between R and MATLAB can be very useful.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With