6 methods or suggestions of how to convert or use MatLab code in R

Converting or using MatLab code in R

Method 1)

matconv is a fantastic R library. A pdf on this is as follows:

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Examples:

Method 1 Example: A function that takes in a Matlab lines and changes the data into R data lines
    
dataMap <- makeDataMap("[", "]", "matrix")
dataMap("thing <- [23,2, 3.2; 7, 6, 8]")
# "thing <- matrix(c(23, 2, 3.2, 7, 6, 8), nrow = 2, ncol = 3)"
dataMap <- makeDataMap(rClass = "list", matClass = "cell")
dataMap("otherThing <- {23,2, '3.2'; NaN, 6, 8}")
# "otherThing <- list(list(23, 2, '3.2'), list(NaN, 6, 8))"
    
  
Method 1 Example: Turn dictionary lines into functions that map matlab to R function calls
    
funcMap <- makeFuncMaps("trace: sum, diag(%1)")
funcMap[['trace']]$argMap[[1]]("matThing")
#$rargs
# "sum(diag(matThing)"
funcMap <- makeFuncMaps("mod: , 1 %% 2")
funcMap[['mod']]$argMap[[1]](c(4, 2))
#$rargs
# "(4, %%, 2"
test1 <- "mat"
test2 <- c("mat", "2")
funcMap <- makeFuncMaps(c("size--if 1:dim, 1", "size--if 2: ,dim(%1)[%2]"))
rightConv <- funcMap$size$flags$multSwitch(test1)
funcMap$size$argMap[[rightConv]](test1)
#$rargs
"dim(mat"
rightConv <- funcMap$size$flags$multSwitch(test2)
funcMap$size$argMap[[rightConv]](test2)
#$rargs
"dim(mat)[2]"
    
  
Method 1 Example: A function that takes in a string and converts all the given slice notation
    
sliceMap <- makeSliceMap("{", "}", "list")
sliceMap("junk <- importData{300}")
# "junk <- importData[[300]]"
sliceMap <- makeSliceMap(matClass = "structure", rClass = "list")
sliceMap("junk <- students.AP.GPA")
# junk <- students[['AP']][['GPA']]
    
  
Method 1 Example: The top level driver function to call the converting functions and handle the input and output.
    
mat2r(inMat, pathOutR = "", funcConverters = NULL,
dataConverters = NULL, verbose = 1)
    
  

Method 2)

Paul Gilbert wrote a bash that can apparently get 80% of the MatLab code translated to R which is as follows:

Method 2)

!/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

Method 3)

Method 4)

Method 5)

Method 6)