View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
EMat: Embedded Matlab Templating

4.5 | 2 ratings Rate this file 4 Downloads (last 30 days) File Size: 4.96 KB File ID: #32362 Version: 1.5
image thumbnail

EMat: Embedded Matlab Templating


Kota Yamaguchi (view profile)


28 Jul 2011 (Updated )

EMat class provides Ruby's ERB-like templating system in Matlab.

| Watch this File

File Information

EMAT Embedded Matlab templating
    EMat class provides a templating system in Matlab like Ruby's ERB
    system. Matlab code can be embedded inside any text document to
    easily control the document generation flow.
    A simple example is illustrated here:
      >> x = 42;
      >> tmpl = ' The value of x is <%= x %>';
      >> obj = EMat(tmpl);
      >> disp(obj.render);
          The value of x is 42
    obj = EMat
    obj = EMat( S )
    obj = EMat( file_path )
    obj.set( S )
    obj.set( file_path )
    S = obj.render()
    obj.render( file_path )
    obj = EMat creates an empty EMat object. EMat object accepts
    template string either by string variable S or by specifying a
    path to the template text file_path. To set a template to the
    object, use obj.set(S) or obj.set(file_path). EMat(S) and
    EMat(file_path) are the shorthand for obj=EMat; obj.set(...);
    S = obj.render() returns a string of the rendered document.
    obj.render(file_path) instead renders output to a file specified
    by the file_path.
      tmpl_path: template file path. Use set() method to change
           tmpl: template string. Use set() method to change
         errchk: logical flag to enable/disable syntax check
                  (default: true)
           trim: logical flag to enable/disable whitespace trim when
                  suppresseing newline at the end (default: true)
  Template format:
    Any text document can embed matlab code with the following syntax.
    <% stmt %> matlab statement
    <% stmt -%> matlab statement with newline suppression at the end
    <%= expr %> matlab expression with rendering
    <%# comt %> comment line
    <%# comt -%> comment line with newline suppression at the end
    <%% %%> escape to render '<%' or '%>', respectively
    <%= expr %> renders output of the matlab expression to the output.
    Note that numeric variables will be converted to string by
    NUM2STR(). When -%> is specified at the end of the line in
    statement or comment, a following newline will be omitted from the
    rendering. Any other texts appearing outside of these special
    brackets are rendered as is. When trim property is set true,
    leading whitespace in the template is also removed from the output
    with newline suppression syntax.
    <!-- template.html.emat -->
    <head><title><%= t %></title></head>
    <%# this is a comment line -%>
    <p><%= a %></p>
    <% for i = 1:3 -%>
      <li><%= i %></li>
    <% end -%>
    % In your matlab code
    % Prepare variables used in the template
    t = 'My template document';
    a = 10;
    % Create an EMat object
    obj = EMat('/path/to/template.html.emat');
    % Render to a file

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
13 Sep 2012 Muthu Annamalai

Thanks, Yamaguchi! I like the idea of templates and the API - its a great add on for many types of projects; HTML generation, scripts etc. However my design would be different.

30 Oct 2011 Vicente

28 Jul 2011 1.1

Added screenshot

10 Aug 2011 1.2

Added automatic num2str conversion in render.
Added support for direct file export.
Added newline suppression syntax.

11 Aug 2011 1.3

Fixed incompatibility to Matlab R2010b;
Added syntax check;

17 Aug 2011 1.4

Added trim flag to suppress whitespaces;
Added workaround to suppress unnecessary syntax warning;

22 Dec 2011 1.5

Fixed a bug that deletes the last line in a template;
Fixed a bug not escaping a '%' character in a template

Contact us