matconv v0.1.0 Released

July 20, 2015
Tags: R, MatLab

I have released a matLab / Octave code translator on my github page, here. Engineering seems to be an untapped market for R. Experiments across disciplines require similar data transformations but MatLab often pushes groups against sharing code. One reason many groups choose to forgo a switch in programming languages is that there is a large code base that would need to be converted which is tedious at best and fatally time consuming at worst.

This program tries to alleviate this reason with a translator that can be molded to a project’s needs. I am actually doing this to convert one of my own projects over from MatLab to R so it at least has to have enough options for a project that spanned multiple years and a variety of MatLab-thonic features. Right now, it just does the base syntax conversion. The base functions and the data structures will be converted using supplied converting functions to a wrapper function that already does the basic syntax. Below is a summary of the features of the present version does.

Basic calls

The first thing that the translator does is go through the program and convert the basic syntax from MatLab to R. This includes the following:

  1. Changing symbols around (‘~’ to ‘!’, ‘end’ to ‘}’)
  2. Get rid of the semicolons at the end of the line
  3. Change the assignment symbol from ‘=’ to ‘<-‘

This is done with simple ‘gsub’ calls and examples are shown below.

matCode rCode
thing = 5 * 3; thing <- 5 * 3
thing2 = (thing ~= 14); thing2 <- (thing != 14)

 

Flow Control

Next it goes through all the different flow control blocks and converts them to the R equivalent. The converted blocks are as follows.

  1. if
  2. if else
  3. for
  4. while

Switch statements will be included in a future version as R does not really have a true switch / case type block. The ‘switch’ function is helpful in a lot of situations but the usage is different from how other languages do switch blocks. Below are some examples of how its used for the other blocks.

matCode rCode
if argLen == 1 if (argLen == 1){
  doThing = 9999;   doThing <- 9999
else } else {
  doThing = 1;   doThing <- 1
end }

 

User functions

In matlab there is a distinction between script files and function files. If a function file is supplied (first word is “function”) than the function definitions will be changed with some of syntax.

The returned objects are gathered and put in a ‘return’ statement at the end of the function. If there is more than one return statement a list of those objects are returned. Default inputs or an unknown length of inputs into a matLab function is handled using a protected word ‘varargin’. The closest R equivalent is ‘…’ which will be turned into a list as the variable ‘varargin’ in the R version. There is a whole family of input and output handlers in matLab but this is the most useful in my eyes so I gave it higher priority. Examples of this type of conversion is shown below.

matCode rCode
function [ dat ] = xlsReadPretty(varargin)) xlsReadPretty <- function(...){
  didThing = 1*3;  varargin <- list(...)
  dat = didThing / 3;   didThing <- 1*3
end   dat <- didThing / 3
 return(dat)
}

 

In another version, there could be options to make this conversion more integrated with the rest of the code or specifying another return class other than ‘list’. This is a precursour to the function converters by adding these user functions into the dictionary of MatLab base functions to R base functions.

rmarkdown files for this post can be found at: https://github.com/sidjai.github.io/_drafts/matconv010.rmd