How to call multiple functions from a single .m matlab file

12,063

Solution 1

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)

Solution 2

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.

Share:
12,063
kumba
Author by

kumba

Updated on June 14, 2022

Comments

  • kumba
    kumba almost 2 years

    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?

  • kumba
    kumba almost 11 years
    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.
  • Eitan T
    Eitan T almost 11 years
    @kumba Strictly speaking, no. But I've suggested a workaround for this. Please see my edit.
  • kumba
    kumba almost 11 years
    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?
  • Eitan T
    Eitan T almost 11 years
    @kumba I'm sorry, you got me confused. In the first comment 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.
  • kumba
    kumba almost 11 years
    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.
  • Eitan T
    Eitan T almost 11 years
    @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)...
  • kumba
    kumba almost 11 years
    How can i automatic the perform('f1', some_variable) part instead of entering f1 to f5 one by one by one.
  • Eitan T
    Eitan T almost 11 years
    @kumba What do you mean?
  • kumba
    kumba almost 11 years
    i sorry i meant to say the switch case, should it be at the very beginning of the perform function or what?
  • kumba
    kumba almost 11 years
    if you say % "include" the function definitions , do you mean the sytax? #include f1; #include f2; etc?
  • Eitan T
    Eitan T almost 11 years
    @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)...
  • Rody Oldenhuis
    Rody Oldenhuis almost 11 years
    @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)
  • Eitan T
    Eitan T almost 11 years
    @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).
  • kumba
    kumba almost 11 years
    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
    kumba almost 11 years
    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.
  • Eitan T
    Eitan T almost 11 years
    @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.
  • Rody Oldenhuis
    Rody Oldenhuis almost 11 years
    @kumba: There is no include syntax in MATLAB; what I wrote is as close to is as you can come.
  • Eitan T
    Eitan T almost 11 years
    @kumba Again, confusion. You said 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.