3

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
4
  • 2
    I think the more "Matlab way" to do things, is to have a single file per function. It is much more natural. How many is a huge number? Commented Oct 8, 2015 at 12:27
  • 1
    @Jørgen this is not true. MATLAB fully supports local and nested functions. Commented Oct 8, 2015 at 12:29
  • @Jørgen: Each cpp file has about 10 functions. I have about 7 cpp file Commented Oct 8, 2015 at 12:30
  • @excaza I know that :) I just meant that calling func1(x)is more naturally in Matlab than doing perform('func1', x). Perhaps a package would be better? Commented Oct 8, 2015 at 12:35

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It looks like my code. The inconvenience of that way is when I call any function, I must do perform('f1',x)....
@user8430, I'm quite sure you want is easiest achieved using functions in different files. If you don't want to fill up your folder with 70 functions, create a sub-folder and place them there.
@user8430 I figured you were using this for like...flags or something. Using one function to call many other functions is silly, if this is the case I would recommend doing what others have already said.
2

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)

1 Comment

Wow -- what a convoluted way to do what's so simple in R (sad face for being stuck with matlab)
1

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.

2 Comments

Interesting! Never thought about doing it this way.
@excaza ... and you probably never should :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.