9

My Matlab script .m file is getting too big. I want to move functionality to multiple .m files my moving functions from the primary file to a several other .m files, each based on category of functionality.

How can the primary .m file 'call' functions in these other new .m files?

Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • 7
    Read [Programming Scripts and Functions](http://uk.mathworks.com/help/matlab/programming-and-data-types.html) to understand the difference between `script` and `function`. Then look more in detail at the `function` section to see how to write independent functions in independent files. – Hoki Jul 27 '15 at 15:47
  • If the file name is the same as the name of the function, and the file is in the active directory (or added to the path) then you can just call it like you would call any other function... – Dan Jul 27 '15 at 15:51
  • Dan: Understood. Does there have to be one .m file for EVERY function? I was hoping each .m file could contain multiple functions. – Doug Null Jul 27 '15 at 16:02
  • Hoki: Yes, I understand all that. But can't find anywhere how a .m file can have multiple functions that are each callable from other .m files. – Doug Null Jul 27 '15 at 16:07
  • 2
    [Local](http://www.mathworks.com/help/matlab/matlab_prog/local-functions.html) and [nested](http://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html) functions are not callable externally. Your options are to use a [class](http://www.mathworks.com/help/matlab/object-oriented-design-with-matlab.html) or a [package](http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html) – sco1 Jul 27 '15 at 16:51
  • @DougNull Yes, you have to have one .m file for each function. Furthermore, the name of each .m file must be the same as the name of the function it contains. – beaker Jul 27 '15 at 16:58
  • Have a look at this questions: [Is it possible to define more than one function per file in MATLAB?](http://stackoverflow.com/questions/3569933/is-it-possible-to-define-more-than-one-function-per-file-in-matlab/3570017#3570017). May be that was more your true question. It answers how to do what you were hoping, but there is no simple and easy way to just stuff multiple functions in one `m` file. – Hoki Jul 27 '15 at 20:13

1 Answers1

10

All the information provided in my answer can be found in Function Basics at MATHWORKS.

How to make a function in MATLAB? You use the following template. In addition the name of the function and the name of the file should be similar.

% ------------------- newFunc.m--------------------
function [out1,out2] = newFunc(in1,in2)
out1 = in1 + in2;
out2 = in1 - in2;
end
%--------------------------------------------------

For using multiple functions you can apply them either in separate m-files or using nested/local structures:

Separate m-files:

In this structure you put each function in a separate file and then you call them in the main file by their names:

%--------- main.m ----------------
% considering that you have written two functions calling `func1.m` and `func2.m`
[y1] = func1(x1,x2);
[y2] = func2(x1,y1);
% -------------------------------

local functions:

In this structure you have a single m-file and inside this file you can define multiple functions, such as:

% ------ main.m----------------
    function [y]=main(x)
    disp('call the local function');
    y=local1(x)
    end

    function [y1]=local1(x1)
    y1=x1*2;
    end
%---------------------------------------

Nested functions:

In this structure functions can be contained in another function, such as:

%------------------ parent.m -------------------
function parent
disp('This is the parent function')
nestedfx

   function nestedfx
      disp('This is the nested function')
   end

end
% -------------------------------------------

You cannot call nested function from outside of the m-files. To do so you have to use either separate m-files for each function, or using class structure.

NKN
  • 6,482
  • 6
  • 36
  • 55