access (lot of small) functions residing in one MATLAB m file

Usually, you create one m-file for one MATLAB function. But what to do if you have many small functions? It’s odd to create a lot of files which just contain some lines of code. The other approach is to put the small functions just inside the m file where they are called from. This is a good solution, as long as you don’t have to access the same functions from multiple m-files!

So if you need to access many functions, which all reside in one m-file, just use the object oriented approach, by defining a Class and static methods:

classdef MyClass

    methods  (Static = true)
         %my functions to be called from outside this m-file
        function y=increment(x)
            y=x+1;
        end
        %a lot of more functions
        function y=decrement(x)
            y=x-1;
        end
    end
end

Now, wherever you need to call any function of MyClass, just use:

value = MyClass.increment(1);

Leave a comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.