输入参数未定义-MATLAB函数/子函数 [英] Input argument undefined - MATLAB function/subfunction
问题描述
我正在为MATLAB中的工作测试函数的一部分.我已经定义了一个函数和子函数,如下所示(仅用于测试):
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
当我在命令窗口中以funct
身份运行该函数时,出现以下错误:
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);
当明确定义ld
时,我无法确定错误和错误位置!
I am not able to make out what and where is the error when ld
is defined clearly!
推荐答案
Chethan是正确的,因为nlfilter()
仅需要一个参数-因此,您需要另一种方式为dirvar()
函数提供ld
参数.
Chethan is correct in that nlfilter()
expects one argument only -- so you need another way to provide the dirvar()
function with the ld
argument.
一种选择是将dirvar
函数定义为调用函数内部的嵌套函数.即
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
这篇关于输入参数未定义-MATLAB函数/子函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!