MATLAB Examples

Work around for defining functions and/or classes in scripts

Use INLINEDEF when you would like to create a function or class definition in a script.

Contents

DESCRIPTION

INLINEDEF gives the appearance of defining a function or class in a script (minus syntax highlighting), though a separate .m file containing the function or class definition is actually auto-generated and saved on the Matlab path - and is not more specifically scoped. Therefore, possibly the only advantage of using INLINEDEF over creating the .m file directly is centralisation of source code, that is, all source code that relates only to the script can, effectively, be stored in the script. This is useful if one's work is of quite an exploratory nature where functions often have limited use outside a specific, temporary context.

SYNTAX

INLINEDEF(path) gets the text that appears in the next comment block and writes it to an appropriately named file in the specified directory (which should be reserved for temporary files). The filename is determined by parsing the first line inside the comment block.

INLINEDEF(path,filename) allows the user to specify the filename as well as its path.

INLINEDEF or INLINEDEF([],filename) raises a GUI that prompts the user to select a directory for the temporary files.

WARNING

INLINEDEF will overwrite existing files without checks or warnings, so be sure to set up a directory specifically for storing files generated by INLINEDEF.

The call to INLINEDEF will result in the specified directory being placed at the top of the Matlab path. This may cause unexpected changes in function precedence order.

There is no automatic clean-up of functions or classes created by INLINEDEF, so attention must be given to contain files created in a single directory and the directory should be periodically wiped if storage is a concern

EXAMPLES

First, designate a directory for temporary files generated by inlinedef

path = [pwd,filesep,'temp'];

Define a function in script

inlinedef(path)
%{
function [x,y] = mytimes(a,b,c)
% doc-string
x = a.*b;
y = b.*c;
%}
[x,y] = mytimes(2,3,4)
x =

     6


y =

    12

Define a class in script

inlinedef(path)
%{
classdef myclass
    properties
        prop
    end
    methods
        function obj = myclass(val)
        % doc-string
            obj.prop = val;
        end
    end
end
%}
myclass(2)
ans = 

  myclass with properties:

    prop: 2

User the pop-up GUI to select the directory of temporary files (or the default if this feature has been edited)

inlinedef
%{
function [x,y] = mydivide(a,b,c)
% doc-string
x = a./b;
y = b./c;
%}
[x,y] = mydivide(2,3,4)
x =

      0.66667


y =

         0.75

Define (for the sake of example) some data in script

inlinedef(path,'tempdata.csv')
%{
id,stat1,stat2
1,0.1,5
2,0.37,2
3,0.24,3.1
%}
readtable(fullfile(path,'tempdata.csv'))
ans = 

    id    stat1    stat2
    __    _____    _____

    1      0.1       5  
    2     0.37       2  
    3     0.24     3.1  

NOTES

The block comment delimiters, %{ and %}, must appear on lines by themselves (optionally preceded by white space). If you want a final end-of-line character then leave a blank line before the %}.

If both comment delimiters are preceded by the same white space (same combination of spaces and tabs) then that white space will be deleted (if possible) from the beginning of each line of the commented text. This is so the whole block can be indented.

INLINEDEF can only be used in an m-file.

RECOMMENDATION

Though not necessary, new users should create a permanent directory for storing files generated by INLINEDEF. They should then replace the call to 'uigetdir' (on line 97 of the function M-file) with the new directory as the default. The function is then able to be called with no input arguments for function and class definitions.

ACKNOWLEDGEMENTS

This file builds on Doug Schwarz's VERBATIM found on the Matlab File Exchange here