# An Introduction to MATLAB Metaprogramming

This post will provide a brief introduction to metaprogramming in MATLAB. MATLAB is both an old and highly specialised language. For this reason, it lacks many of the features found in more modern and general purpose programming languages. However, with a few simple tools, we will find that MATLAB is flexible enough to allow for very simple metaprogramming.

### What is metaprogramming? Why do it in MATLAB?

Roughly speaking, metaprogramming is the process of treating a program as data - meaning that a program can be generated or manipulated as though it were a normal piece of data. Languages such as Ruby expose a number of metaprogramming features. MATLAB offers no such suite of tools whose express purpose is metaprogramming. With a little bit of creativity, this can actually be done, using the symbolic toolbox.

Note that it might be a *bit* of a stretch to call the content of this post metaprogramming, but I feel that it is sufficiently similar to justify the label.

### Dependencies

The code in this post works because of the following:

- Functions are first class objects
- Functions can be declared anonymously
- Symbolic expressions can be mutated programatically.

In broad strokes, this lets us declare symbolic expressions, modify them, and turn them into functions. This is the core idea in this post.

### Using and modifying symbolic expressions

First, let’s declare a symbolic variable

MATLAB will now treat `x`

not as a numerical variable but rather a symbol.
This lets us build up more complicated expressions.
For example:

Of course, the last expression is just . MATLAB knows this:

We can also do calculus:

The symbolic toolbox comes with a function called `matlabFunction`

that will turn a symbolic expression into a function:

With these tools in hand, we can start writing programs that write programs.

### Fourier Series Approximations

A Fourier series is an approximation of a function using a linear combination of sines and cosines of different periods. A real function can be approximated by such a series as follows:

where:

The approximation is exact for certain classes of functions, but we will write a program to approximate arbitrary real functions using a truncated version of the series.

### Metaprogramming Fourier series approximations

Now, we will implement a function that takes a function, and returns another function that is its (truncated) Fourier series approximation. There are better ways to implement a Fourier series, but this is a neat demonstration of metaprogramming in MATLAB.

The function should work as follows

where `f`

is a function and `N`

is the highest term to approximate `f`

to, with `g`

being the approximating function.

First, we create a symbolic expression that will eventually be converted to a MATLAB function, and call the independent variable . Then, we add the first term to the series:

(Notice that we are using MATLAB’s built-in numerical routines to compute the necessary integrals.)

Next, we need to add a finite (`N`

) linear combination of sines and cosines to `series`

:

The last line is key: it converts the purely symbolic variable `series`

into an executable function.
This means that `fn`

behaves as though it was hard coded!
Calling this ‘metaprogramming’ is correct, because we generated this function programatically.

### Running this function

Let’s use `fourierapprox`

to generate some approximating functions.

The following function will plot the original function and the Fourier approximation on the same axes, over the specified interval.

#### Square Wave

### Final remarks

Metaprogramming is not a programming paradigm typically used in MATLAB. I wanted to demonstrate that it is not only possible but also useful.