1

I am testing a part of a function for my work in MATLAB. I have defined a function and subfunction as follows(just for testing):

function funct
clear all;
clc;
I = rand(11,11);
ld = input('Enter the lag = ') % prompt for lag distance
A = nlfilter(I, [7 7], @dirvar);

% Subfunction
function [h] = dirvar(I, ld) %tried with function [h] = dirvar(I) as well, 
                             %but resulted in same error
c = (size(I)+1)/2
EW = I(c(1),c(2):end)
h = length(EW) - ld

When I run the function in command window as funct I get the following error:

Enter the lag = 1

ld =

     1


c =

     4     4


EW =

    0.0700    0.4073    0.9869    0.5470

??? Input argument "ld" is undefined.

Error in ==> funct>dirvar at 14
h = length(EW) - ld
Error in ==> nlfilter at 61
b = mkconstarray(class(feval(fun,aa(1+rows,1+cols),params{:})), 0, size(a));

Error in ==> funct at 6
A = nlfilter(I, [7 7], @dirvar);

I am not able to make out what and where is the error when ld is defined clearly!

Chethan S.
  • 558
  • 2
  • 8
  • 28

2 Answers2

1

Chethan is correct in that nlfilter() expects one argument only -- so you need another way to provide the dirvar() function with the ld argument.

One option is to define the dirvar function as a nested function inside the calling function. I.e.,

function funct
% ...
ld = input('Enter the lag = ') % prompt for lag distance
A = nlfilter(I, [7 7], @dirvar);

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

end
nimrodm
  • 23,081
  • 7
  • 58
  • 59
0

I don't have the image processing toolbox, so I can't check this myself, but it looks like nlfilter expects a function of just one argument. Try changing the call to nlfilter like this:

A = nlfilter(I, [7 7], @(x) dirvar(x,ld));
DGrady
  • 1,065
  • 1
  • 13
  • 23
  • I get the same error again! `??? Undefined function or variable 'ld'. Error in ==> funct>dirvar at 14 h = length(EW) - ld Error in ==> nlfilter at 61 b = mkconstarray(class(feval(fun,aa(1+rows,1+cols),params{:})), 0, size(a)); Error in ==> funct at 6 A = nlfilter(I, [7 7], @dirvar); ` – Chethan S. Apr 17 '11 at 06:08