Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add multiple functions in matlab

Tags:

matlab

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?

like image 805
Ted Flethuseo Avatar asked Oct 25 '10 20:10

Ted Flethuseo


People also ask

Can you put multiple functions in one MATLAB?

Program files can contain multiple functions. If the file contains only function definitions, the first function is the main function, and is the function that MATLAB associates with the file name.

Can you add functions in MATLAB?

Starting in R2016b, MATLAB® scripts, including live scripts, can contain code to define functions. These functions are called local functions. Local functions are useful if you want to reuse code within a script. By adding local functions, you can avoid creating and managing separate function files.


1 Answers

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();
like image 118
John Alexiou Avatar answered Sep 27 '22 02:09

John Alexiou