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?
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With