---
title: "matconv: How to convert matlab code"
author: "Siddarta Jairam"
date: "`r strftime(Sys.time(),'%A, %B %d, %Y')`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Overall design}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---


```{r setup, include=FALSE}
library(matconv)
library(knitr)

knitr::opts_chunk$set(fig.pos='center', echo=TRUE, comment='>')


matCode <- c("function [out] = csvReadPretty(csvPath, hd)",
 "\tfid = 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;",
 "    end",
 "    ",
 "    for (row = (hd+1):length(parse))",
 "        line = [parse{row} ','];",
 "        commas = regexp(line,',');",
 "        col = 2;",
 "        bef = commas(1);",
 "        if bef~=1",
 "            out{row-hd,1} = line(1:bef-1);",
 "        end",
 "",
 "        for ca = commas(2:end)",
 "            %if the commas are next to each other just add col",
 "",
 "            if (bef+1 ~= ca)",
 "               out{row,col} = line(bef+1:ca-1);",
 "",
 "            end",
 "            col=col+1;",
 "            bef = ca;",
 "        end",
 "",
 "    end",
 "",
 "",
 "end")
```
## Basic Conversion

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

```{r basic}

out <- mat2r(matCode)
names(out)
```

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](https://umaine.edu/mathematics/david-hiebeler/computing-software/matlab-r-reference/) by David Hiebler. This is included in the package as external data and can be used as below.

```{r functionMaps}
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.

```{r dataConverters}
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.

```{r finish}
out <- mat2r(matCode, funcConverters = hMaps, dataConverters = dataConvs, verbose = 2)

```

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