Matlab Code Templates

Version 1.1 (6.61 KB) by David Legland
A collection of functions for quickly generating pre-edited matlab files for specific use cases.
82 Downloads
Updated 19 Jul 2024

matlab-templates

Matlab-Templates is a collection of functions for quickly generating pre-edited matlab files for specific use cases: creating new classes, new tests... It is based on the 'tedit' function, originally written by Peter Bodin.

The main functions are:

  • newClass: create a new file containing a class definition template
  • newTest: create a new file containing a minimal implementation for running unit tests
  • newEnum: create a new file containing a minimal implementation for an enumeration class

Each function has to be called with the name of the item to create. This creates a new file (based on the item name), and opens the editor with a pre-edited header and code structure. In particular, author name, copyright information, date of creation... are automatically populated based on the templates defined in the 'newXXX' functions.

Installation

The library is composed of a single directory containing all the necessary functions. The installation can be performed by simply cloning (or extracting the archive), and adding the path to the library to the Matlab path list.

Creating a new class

The newClass function can be used to create a new file containing a class definition.

newClass('MyClassName') 

The new file contains the following code patterns:

  • a header with pre-edited information
  • property definition
  • a constructor stub
  • space for specific methods

You need to edit the different files for updating user-specific information (author name, contact email, copyright notice...).

Class pattern example

The following is the result of the creation of a new file called SampleClass.m, defining the class SampleClass, using the command newClass('SampleClass'):

    classdef SampleClass < handle
    % One-line description here, please.
    %
    %   Class SampleClass
    %
    %   Example
    %   SampleClass
    %
    %   See also
    %
    
    % ------
    % Author: David Legland
    % e-mail: david.legland@inrae.fr
    % Created: 2020-12-23,    using Matlab 9.8.0.1323502 (R2020a)
    % Copyright 2020 INRAE - BIA-BIBS.
   
   
    %% Properties
    properties
    end % end properties
    
    
    %% Constructor
    methods
        function obj = SampleClass(varargin)
            % Constructor for SampleClass class.
    
        end
    
    end % end constructors
    
    
    %% Methods
    methods
    end % end methods
    
    end % end classdef

Creating new test

The newTest function can be used to create a new file containing a minimal implementation for running unit tests.

newTest(FUNCTIONTOTEST) 

The following is the result of the command newTest('foo'):

    function tests = test_foo
    % Test suite for the file foo.
    %
    %   Test suite for the file foo
    %
    %   Example
    %   test_foo
    %
    %   See also
    %     foo
    
    % ------
    % Author: David Legland
    % e-mail: david.legland@inrae.fr
    % Created: 2021-09-10,    using Matlab 9.10.0.1684407 (R2021a) Update 3
    % Copyright 2021 INRAE - BIA-BIBS.
    
    tests = functiontests(localfunctions);
    
    function test_Simple(testCase) %#ok<*DEFNU>
    % Test call of function without argument.
    foo();
    value = 10;
    assertEqual(testCase, value, 10);

Customization

To change author name, affilation, or the general layout of templates, simply edit the template files.

Cite As

David Legland (2024). Matlab Code Templates (https://github.com/mattools/matlab-templates/releases/tag/v1.1), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1

See release notes for this release on GitHub: https://github.com/mattools/matlab-templates/releases/tag/v1.1

1.0

0.9.0.0

See release notes for this release on GitHub: https://github.com/mattools/matlab-templates/releases/tag/v0.9

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.