File Exchange Pick of the Week

Our best user submissions

Execute My MATLAB Algorithm in a Web Application

Posted by Guest Picker,

Greg’s pick this week is Generate JavaScript Using MATLAB Coder by Geoff McVittie.

You are building a game where you want to model Einsteinian relativistic physics as part of an online learning tool. The interface is designed in HTML, CSS, and Javascript, but you prototyped the computational algorithms in MATLAB.

You know you can generate C-code from MATLAB….but you can’t deploy that as part of a web application as it won’t run in the web browser.

Do you have to rewrite the entire algorithm in Javascript? Or run the computational algorithm as a remote service?

Enter Emscripten, a transpiler that compiles C/C++ code as WebAssembly with a Javascript interface.

Geoff took this one step further, and created a code generation target for MATLAB Coder that automates the process of converting the algorithm to C-code, compiling it to Web Assembly, and creating the Javascript interface.

Why Not Just Write it in Javascript in the First Place?

That’s a fair question. Technically there’s nothing preventing you from doing that.

I argue its easier to create, manage, and process arrays of data and equations in MATLAB. So writing the algorithm may be simpler.

However, data synthesis is the part I find most valuable. It is very easy to synthesize data in MATLAB for the purposes of testing our algorithms and ideas. I can very easily create different synthetic datasets, import data from different files, and create test cases for exciting our MATLAB algorithms.

When I’ve had to do this for various reasons in other languages like C, C#, and Javascript, I find myself struggling to generate suites of input data, and quickly being able to assess the results. My favorite debugging tool in MATLAB is PLOT, where I can quickly take a look at my data as a graph to see how my algorithm is behaving.

The kind of features needed to create datasets, read datafiles, or review output results need to be created in these other lower-level languages, which makes testing complex computational algorithms trickier.

A Exemplar File Exchange Submission

Geoff has created a complete package that includes:

  • Documentation
  • Examples
  • Easy setup instructions

In addition he has added some features that enable you to target different types of code entrypoints that you would find in web applications, like mouse event callbacks.

He also included some functions that enable basic debugging once the code is deployed to the browser, like the ability to print messages in the developer console of your web browser.

What do you think?

Let us know here.

346 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.