2

I have an algorithm written in one m file and i have several functions that i created in another .m file. I want to call these several functions i created in a separated file from the main algorithm .m file. I know how to call one function from a file to another, but here i want to be calling different functions i created in a separate file from my mail algorithm file. I have searched here, but the answers i got does not help and are not talking about what i want.

Here is a little illustration of what i am talking about:

main algo file
N = 30;
x = -10 + 20rand(1,N)
for j = 1 to N
  c = f1(x) % here i need to call different functions from another file
end

Functions with several variable- this is a separate file

Function perform

%% Function F1
f = f1(x)
 statements
end

%% Function F2
f = f2(x)
 statements
end

%% Function F3
f = f3(x)
 statements
end

%% Function F4
f = f4(x)
 statements
end

%% Function F5
f = f5(x)
 statements
end

end Perform

I want to be calling the F1 to F4 in the main algo .m file. How can you do this. Also it will be better if each time i run the main algo .m file, it prompts me to choose which of the F1 to F4 function i want to call and one i inputs and indicate the function in a dailog box, it calls that particular function. Any idea on how to do this please?

Shai
  • 111,146
  • 38
  • 238
  • 371
kumba
  • 31
  • 1
  • 2
  • 6
  • 1
    @Shai Not sure it's a duplicate. In your question you didn't want to accept a solution that changes the public function, while here the OP _implements_ it, so I guess he's open for suggestions... – Eitan T Jul 03 '13 at 11:09
  • @EitanT if he is willing to to change the implementation, why not write each private function in its own m-file and be done with it? If he wants more challenges to make his life more complicated he might as well try and implement it with 10 books balanced on top of his head... – Shai Jul 03 '13 at 11:11
  • @Shai I'd do the same (regarding the m-files, not the books), but I can understand why one wants to avoid a clutter of m-files. – Eitan T Jul 03 '13 at 11:14
  • 1
    @EitanT you should try the books as well;) – Shai Jul 03 '13 at 11:15
  • @Shai, i am trying to avoid having lots of m file. I know it is easier that way. – kumba Jul 03 '13 at 11:30
  • Possible duplicate http://stackoverflow.com/questions/3569933/is-it-possible-to-define-more-than-one-function-per-file-in-matlab – sancho.s ReinstateMonicaCellio Mar 23 '16 at 06:27

3 Answers3

3

The MATLAB documentation states:

MATLAB® program files can contain code for more than one function. The first function in the file (the main function) is visible to functions in other files, or you can call it from the command line. Additional functions within the file are called local functions. Local functions are only visible to other functions in the same file.

So in fact, the only function you can called outside this m-file is the first function (which is perform in your example), while the functions f1, ..., f5 can only be invoked inside the m-file, as they are local.

My suggestion is to stick with the recommended practice and define each function in its own m-file. However, if you don't want ending up with a multitude of m-files, and insist on having all functions implemented in the same m-file, you can work around this by passing additional arguments to the main function as follows:

function f = perform(func, x);
    switch(func)
        case 'f1'
            f = f1(x);
        case 'f2'
            f = f2(x);
        case 'f3'
            f = f3(x);
        case 'f4'
            f = f4(x);
        case 'f5'
            f = f5(x);
        otherwise
            error(['Unknown function ', func]);
    end

%// ... next follows the implementation of f1 through f5

and then invoke each of the functions f1, ..., f5 by calling perform with the appropriate function name string. For example:

perform('f1', some_variable)
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • So you are basically saying i cannot call function in Functions perform file from the main algo file, right? BTW, i am using matlab version R2008b. – kumba Jul 03 '13 at 11:05
  • @kumba Strictly speaking, no. But I've suggested a workaround for this. Please see my edit. – Eitan T Jul 03 '13 at 11:06
  • Oh, the main algorithm and all the several F1 to F5 functions have to be in one file. Does e case statement and the F1 to F5 functions have to be implemented after the main algorithm or before? – kumba Jul 03 '13 at 11:24
  • @kumba I'm sorry, you got me confused. [In the first comment](http://stackoverflow.com/questions/17445995/17446118?noredirect=1#comment25345020_17446118) I got the impression that the "main algo file" is a different file than this one, in which `perform` and `f1` through `f5` are implemented. Now you tell me it's the same file? From within an m-file you can invoke all functions that are implemented inside it. – Eitan T Jul 03 '13 at 11:31
  • they are actually different file. I though you suggesting i have them in one file, that why i ask. Yes, the main algo is a differnt .m file and the perform which has the f1 to f5 functions is also another file. – kumba Jul 03 '13 at 11:41
  • @kumba, Okay so if you do what I suggested, you can then invoke, for example, function `f1` from the main algo file with: `perform('f1', some_variable)`... – Eitan T Jul 03 '13 at 11:48
  • How can i automatic the perform('f1', some_variable) part instead of entering f1 to f5 one by one by one. – kumba Jul 03 '13 at 12:00
  • @kumba What do you mean? – Eitan T Jul 03 '13 at 12:04
  • i sorry i meant to say the switch case, should it be at the very beginning of the perform function or what? – kumba Jul 03 '13 at 12:23
  • @kumba Moreover, it should be the _only_ code in `perform`. If you want to add additional functionality to perform, you'd rather put it as an extra function (say, `f6`)... – Eitan T Jul 03 '13 at 12:51
  • this perform is the main function of the algo. If you like you can call it the main. There are bunch of codes implementing the algorithm. Just need to call the f1 to f5 function at just one line in the code. – kumba Jul 03 '13 at 12:56
  • @kumba Again, confusion. [You said](http://stackoverflow.com/questions/17445995/17446118#comment25346175_17446118) that the main algo is in a different file. The bottom line which you need to understand is: if you're calling the "f" functions from _any_ function in that _same_ file, there's no problem. If you're calling an "f" function (not `perform`) from _another_ file, it **won't work** and you'll need either my or Rody's workaround, or do it the canonical way and implement each "f" function in a separate m-file. – Eitan T Jul 03 '13 at 14:03
1

Another workaround is to create a class with static methods corresponding to your original functions. Then everything is bundled into the one .m class file.

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53
0

Just to complement Eitan T's answer; there's another workaround that follows the "include files" analogy found in most C-family languages (and quite a few others, actually). It works like so:

Main algo file:

function main

    %// "include" the function definitions 
    f = declareFunctions;

    %// Call desired function like so
    for jj = 1:N
        c1 = f{1}(x)  %// call f1(x)
        c2 = f{2}(x)  %// call f2(x)
        %// ...
        cN = f{N}(x)  %// call fN(x)
    end

end

Function definitions file:

%// "Declare" all subfunctions in a caller's workspace
function fs = declareFunctions

    fs = {...
        @f1,...
        @f2,...
        @f3,...
        %//... etc.
        @fN
    };

end

%// first function 
function y = f1(x)
   %// f1's code
end


%// Second function 
function y = f2(x)
   %// f2's code
end

%// ...

%// Nth function 
function y = fN(x)
   %// fN code
end
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • if you say % "include" the function definitions , do you mean the sytax? #include f1; #include f2; etc? – kumba Jul 03 '13 at 12:49
  • @kumba: More like hwo you'd use `#include "functions.h"` in C/C++, where `functions.h` contains *all* your function definitions. The idea is more or less the same, but of course, completely different syntax (there's no need for a `functions.cpp` file, for instance) – Rody Oldenhuis Jul 03 '13 at 12:52
  • 1
    @kumba The `% include` part is nothing but a comment to show you the similarity between what Rody implemented and the functionality of `#include` in C. In practice, Rody's code creates a cell array of function handles in the main workspace, and you invoke each function by accessing the appropriate cell, _e.g_ call `f1(x)` like so: `f{1}(x)`. – Eitan T Jul 03 '13 at 12:53
  • The include syntax in matlab is what am not getting. Never used it before, wanna try it this time, but have no clue of the syntax. – kumba Jul 03 '13 at 13:00
  • @kumba Again, there is no include syntax in MATLAB. There is what Rody wrote, which acts in a way that reminds #include in C, meaning it creates an array of function handles which you can use to call those functions. – Eitan T Jul 03 '13 at 13:07
  • @kumba: There is no include syntax in MATLAB; what I wrote is as close to is as you can come. – Rody Oldenhuis Jul 03 '13 at 13:26