1

Gave up on looking for this in the web... How can I do the following in Matlab:

%%%%%%%%%%%%%%%%%%%%%
% library.m
%%%%%%%%%%%%%%%%%%%%%

function do_this()
% ...
end

function do_that()
% ...
end

%%%%%%%%%%%%%%%%%%%%%
% foo.m
%%%%%%%%%%%%%%%%%%%%%

function foo()   

for i = 1:100
    do_this();
end
do_that();

end

My question again: how can I create a library of functions that are organized within one file, and that could be called from other functions/scripts?

Sam Hosseini
  • 813
  • 2
  • 9
  • 17
ToniAz
  • 430
  • 1
  • 6
  • 24
  • 4
    I don't think you can. Please check out the link [similar question](http://stackoverflow.com/questions/3569933/is-it-possible-to-define-more-than-one-function-per-file-in-matlab) which is similar to your question. One of the solutions is to create a class with the static methods that could be your `do_this` and `do_that`. – Geoff Jun 26 '14 at 20:30
  • @GeoffHayes is right. The only thing I can possibly think of is if you encapsulate these methods into a class. Then, you could create instances of this class and call those methods at will. – rayryeng Jun 26 '14 at 20:37
  • Side note: You can also look to [packages](http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html#brf3g8k), but that just about avoiding name collisions, not about having multiple functions in one file (@GeoffHayes solution using static methods is probably the only way). – CitizenInsane Jun 27 '14 at 09:14
  • Side note: You can have [nested functions](http://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html). But this would only allow you to call the main function from the outside. – Schorsch Jun 27 '14 at 12:53

0 Answers0