Continuing my experimenting with the reach package (by Christoph Schmidt) for R, I’ve begun developing tools for using R Markdown to create course materials that include MATLAB syntax and output. My goal is to gradually improve the reproducibility of my course source files.
So far I’ve developed a template for creating a Bode plot and a step-response plot using MATLAB’s Control System Toolbox with all of the scripting written in R Markdown or R. For example, I show an Rmd script containing MATLAB code that creates a transfer function, executes a step response, writes to file the transfer function created by tf(), and saves to PNG the graph produced by step().
Most importantly, the MATLAB code scripted in the Rmd file is the sole code source—there is no separate, standalone m-file to write or run, thereby eliminating the copy-and-paste of code and graphs that plagued my previous workflow in creating course documents.
Now when I create a document for students to show them some MATLAB code, the lines of code that are executed and the lines of code displayed are from a single listing in the Rmd script. Updating the listing automatically updates the displayed code, the executed code, and all results (such as graphs) displayed in the output document.
Moreover, R Markdown offers the added advantage of being able to select an output format (HTML, PDF, Word, github_document, etc.) to meet the needs of the course.
Compared to my earlier post about reach, my current toolbox approach requires less scripting overhead and resolves the R-to-MATLAB communication issues nicely.
The scripts are available and discussed in detail on GitHub at https://github.com/graphdr/r-matlab-toolbox.