0

The syntax for nlfilter in MATLAB is:

B = nlfilter(A, [m n], fun)

I am considering creating a M-File with several subfunctions to be called using test function here; i.e., I wanted a choice such that each time I can choose what subfunction gets called under fun.

% Main Function
function test
B = nlfilter(A, [m n], fun)

% Subfunction 1
function sub1
.......

% Subfunction 2
function sub2
.......

% Subfunction 3
function sub3
.......

Will it be possible to generalize fun in such a way that I can call either sub1 or sub2 or sub3 from test.

EDIT

My function:

function funct(subfn)
clc;
I = rand(11,11);
ld = input('Enter the lag = ') % prompt for lag distance
fh = {@dirvar,@diagvar};
feval(fh{subfn});
A = nlfilter(I, [7 7], subfn);


% Subfunction
    function [h] = dirvar(I)
        c = (size(I)+1)/2
        EW = I(c(1),c(2):end)
        h = length(EW) - ld
    end

% Subfunction
    function [h] = diagvar(I)
        c = (size(I)+1)/2
        NE = diag(I(c(1):-1:1,c(2):end))
        h = length(NE) - ld
    end
end 

When I run funct(1) now this is the output with error:

Enter the lag = 1

ld =

     1

??? Input argument "I" is undefined.

Error in ==> funct>dirvar at 12
        c = (size(I)+1)/2

Error in ==> funct at 6
feval(fh{subfn});

I am puzzled as to what is the problem now?

Chethan S.
  • 558
  • 2
  • 8
  • 28
  • I am not really sure I understand your question - Wha't wrong with giving the desired function handle in each call: `B = nlfilter(A, [m n], @sub1)`, etc.. ? – Itamar Katz Apr 17 '11 at 09:34
  • If I just call `nlfilter` with necessary arguments what you say will work. But when I call `test` I don't really have an option to choose `sub1` or `sub2` or `sub3`. – Chethan S. Apr 17 '11 at 09:38
  • possible duplicate of [Iterating over functions in MATLAB](http://stackoverflow.com/questions/5669454/iterating-over-functions-in-matlab) – abcd Apr 17 '11 at 14:50
  • I request you not to close this question as "Exact duplicate" of [Iterating over functions in MATLAB](http://stackoverflow.com/questions/5669454/iterating-over-functions-in-matlab) since I am getting errors when I implemented suggestions similar to that discussed there. I have also edited my question with the errors. – Chethan S. Apr 17 '11 at 15:36
  • As the error message says, `Input argument "I" is undefined.` - You have to supply the input argument `I` to the function: `feval(fh{subfn},I);` – Itamar Katz Apr 17 '11 at 16:28

2 Answers2

1

If you know the name of the subfunction, you can use str2func:

Change the test function to accept a string which holds the subfunction name:

function test (subfunNm)

And call nlfilter like this:

B = nlfilter(A, [m n], str2func (subfunNm));

Now you can call test:

test ('sub1')

etc.

EDIT

In the case of nested functions, you can hold a cell array of the function handles, and pass in an index (instead of a string):

function test(fnInd)

fh = {@f1,@f2,@f3};
feval(fh{fnInd});

    function f1
        disp('f1')
    end

    function f2
        disp('f2')
    end

    function f3
        disp('f3')
    end
end

And call it using test (1) etc.

Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
  • Just discovered that "Nested functions are not accessible to str2func. To construct a function handle for a nested function, you must use the function handle constructor, @." from this [link](http://www.mathworks.com/help/techdoc/ref/str2func.html). Anyway I will try using function handles. – Chethan S. Apr 17 '11 at 13:38
  • Yes, you should use non-nested functions... I've assumed your sub-functions are not nested (as in the code in your question) – Itamar Katz Apr 17 '11 at 14:44
  • I've edited my answer to include the case of a nested function. – Itamar Katz Apr 17 '11 at 14:48
  • I have added the part of code I am working on and the error I get when I implemented your edits to my question. – Chethan S. Apr 17 '11 at 15:31
1

Take a look at str2func and/or function handles.

I'd personally stay away from strings to pass functions, but you might just need to use that.

Egon
  • 4,757
  • 1
  • 23
  • 38
  • Do you have any specific reason for staying away from strings to pass functions - disadvantages in terms of speed or anything else? – Chethan S. Apr 17 '11 at 11:25
  • 1
    Just aesthetics: strings are meant to store an array of characters, function handles are meant to point to functions. So a function handle screams "Hey, I am to be used as a function", while a string might be anything. – Egon Apr 17 '11 at 12:16