Jame
Jame

Reputation: 3854

How to create multiple functions in single matlab file

I have a C++ file that has some functions in single .cpp file such as

 int func1(x)
 {
  return 1;
 }
 void func2(x)
 {
  return 1;
 }
 int func3(x)
 {
  return 1;
 }

Now, I want to write all above functions in a single matlab file. (If I added an own matlab file for each function, my folder would grow very huge.) Could you suggest to me an easy as well as very clear way to do this in MATLAB?

Currently, I am doing it this way:

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

Upvotes: 3

Views: 3151

Answers (3)

Carl Witthoft
Carl Witthoft

Reputation: 21502

You can, call local functions externally if you set up a bunch of function handles. Not that I recommend this, but I got this setup from a colleague. This code is not robust -- there are almost certainly cases where it'll fail or do bad things.

% code to create callable handles for all subfuncs in some file
%
% first, in the file containing subfuncs:
% this is the "setup" main func 
function [p]=To_Call_Subfuncs()
[p,n]=update_function_list();
end

% this creates all the handles by pseudogrepping the subfunction names
function [p,function_names]=update_function_list()
function_names={};
% making p empty allows one to call, e.g.  funclist.firstfunc() 
p=[];   
f=fopen([mfilename() '.m'],'r');
while ~feof(f),
  line=fgetl(f);
  s= strfind( strtrim(line),'function ') ;
  if length(s) && s(1)==1,
    s0=find(line=='=');
    s1=find(line=='(');
    if length(s0)==0,s0=9;end;
    if(length(s1)==0), s1 = numel(line)+1;end; %to handle the -1 later on
    function_name= strtrim( [line(s0(1)+1:s1(1)-1)] );
    if length(function_name),
      function_names{end+1}=function_name;
      eval( sprintf('p.%s=@%s;', function_name, function_name) );
    end;
  end;
end;
end
%
%follow this with the  functions we actually want to call
function a = firstfunc(x)
a=x;
end

function b = secondfunc(x)
b = x^2;
end

function cnot = thirdfunc
cnot= 17;
end

%
%
% next, in the m-file where some function is calling these subfuncs,
% set up the handles with:
%  run(fullfile(dirpath,'the_mfile.m'))
%  the_mfile=ans; 
% because I don't  think run() returns a value --
%%    only the called mfile does.
% now you can call the_mfile.firstfunc()   and so on.

Upvotes: 1

Jørgen
Jørgen

Reputation: 863

You could organize the functions in package, see Packages Create Namespaces, one for each cpp file as

+module1
    func1.m
    func2.m
+module2
    func1.m

You can then call the functions as

module1.func1(x)
module2.func1(x)

Upvotes: 2

sco1
sco1

Reputation: 12214

See local or nested functions. Local functions are most analagous to other programming language syntax.

Local functions, for example:

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

function func1(x)
disp(sprintf('func1: %u\n', x))
end

function func2(x)
disp(sprintf('func2: %u\n', x))
end

function func3(x)
disp(sprintf('func3: %u\n', x))
end

Note that neither local nor nested functions are externally callable.

Upvotes: 2

Related Questions