tags:

views:

56

answers:

2

Hi everyone,

I want to add several functions from a single .m file. Is this possible without actually having to create an individual m file for each function?

+2  A: 

For later versions of Matlab that support the classdef keyword, I recommend adding the functions as static methods to a class and then calling them from an instance of that class. It can all be done with one .m file:

classdef roof
  methods (Static)
    function res = f1(...)
        ...
    end
    function res = f2(...)
        ...
    end
  end
end

and you call them by

roof.f1();
roof.f2();
jalexiou
A: 

It's possible (at least at 2010a). You have just to finish each function by 'end' statement. Like:

function a
   ...
end
function b
   ...
end

Actually, MatLab supports (at least) 4 types of .m files :

  • script
  • one function per file (you don't have to end function's body by end statement)
  • multiple functions per file (you have to)
  • classes (see the answer above)
green_fr
this is wrong for several reasons. First, you don't have to end each functions body with an end statement. You just have to be consistent and either terminate each function with an "end" or non of them. Second, if you do this, you can only call the first function from outside the .m file, which is not what the OP asked for.
Marc

related questions