EMat: Embedded Matlab Templating

code

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.

Download

EMat.m (Also available from File Exchange)

Install

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

How to use

First create a document template:

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

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

%main.m

% 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
obj.render('/path/to/rendered.html');

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

<html>
<head>
<title>My template document</title>
</head>
<body>
<p>a = 10</p>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
</body>
</html>

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