EMat: Embedded Matlab Templating


Tired to write a bunch of fprintf(fid, '%s %s %s\n', 'this', 'is', 'messy') statements in your matlab code? EMat class brings the ease of Ruby's ERB template in Matlab to release you from painful repetition of low-level fprintf statements.

EMat is suitable to generate a text document (e.g., HTML) from Matlab. It allows you to embed matlab statements within a template document with special percent brackets <% stmt -%>. Also you can render the result of a matlab expression with <%= expr %> within a template. Check out the usage example below.


EMat.m (Also available from File Exchange)


EMat is a regular matlab class. Place EMat.m to somewhere accessible within your matlab path.

How to use

First create a document template:

<head><title><%= page.title %></title></head>
<p>a = <%= page.a %></p>
<% for i = 1:3 -%>
  <li><%= i %></li>
<% end -%>

Let's save this template in /path/to/template.html. Then, execute the following code in matlab:


% Prepare variables used in the template
page.title = 'My template document'; 
page.a = 10; 

% Create an EMat object 
obj = EMat('/path/to/template.html'); 

% Render to a file

You will get the following result in /path/to/rendered.html:

<title>My template document</title>
<p>a = 10</p>

Check help EMat in matlab for the detail. If you have any comment, please visit File Exchange.