matconv: How to convert matlab code

Siddarta Jairam

Wednesday, May 12, 2021

Basic Conversion

If you just want a basic conversion with R syntax, calling “mat2r” with just the Matlab code.

out <- mat2r(matCode)
> The previous code had 34 lines and the R code has 34 lines
> [1] "matCode" "rCode"

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. There is also a default message that shows the number of lines in each. This can be turned off using “verbose = 0”. Verbose 2 makes it so that the resultant code is outputted as a message. The code can be written by supplying a destination path with the “pathOutR” parameter.

For a more complicated conversion you can use function covnerters or data converters. The function converters change some of the base functions of Matlab to R. A whole dictionary notation can be used to convert what ever functions you want which can be seen used in “vignettes/functionCalls.rmd”. With the “makeFuncMaps” function and these dictionaries a whole slew of conversions can be specified. As a start, the base Matlab functions have been made into dictionary lines using the reference by David Hiebler. This is included in the package as external data and can be used as below.

hMaps <- makeFuncMaps(
    pathDict = system.file("extdata", "HiebelerDict.txt", package = "matconv"))

There are also data converters that come in either instation (“makeDataMap”) or slicing (makeSliceMap). To identify the Matlab code that needs converting, you can either give the left and right symbol that encloses the data or the Matlab class. The output is determined by the Rclass name given to the converter. Base converters are included as R code in the external data and can be used as below.

source(system.file("extdata", "defDataConv.R", package = "matconv"))

All these base dictionaries can be added to user functions as a new entry in the list. These are then given to the main driver as shown below.

out <- mat2r(matCode, funcConverters = hMaps, dataConverters = dataConvs, verbose = 2)
> The previous code had 34 lines and the R code has 34 lines
> csvReadPretty <- function(csvPath, hd){   fid <- fopen(csvPath,'r')    parse <- textscan(fid, '%s','delimiter','\n')    parse <- parse[[1]]    thing <- 5.4    fclose(fid)    if (isempty(parse) || isempty(parse[[1]])){        out <- parse    }    for ((row in (hd+1):length(parse))){        line <- c(paste0(parse[[row]], ','))        commas <- (gregexpr(',', line)[[1]])        col <- 2        bef <- commas(1)        if (bef!=1){            out[[row-hd,1]] <- line(1:bef-1)        }        for (ca in commas(2:length(commas))){            #if the commas are next to each other just add col            if (bef+1 != ca){               out[[row,col]] <- line(bef+1:ca-1)            }            col <- col+1            bef <- ca        }    }}

This system was made to be customizable for a projects needs as well as being extendable for other languages.