MATLAB内联函数错误

时间:2013-11-22 09:48:35

标签: matlab

我正在尝试运行这个简单的MATLAB例程。这将绘制一个窗口函数。

M = 26;
n = [0:(M-1)];
om = linspace(-pi, pi, 201); % for displaying frequency response  
oc = pi/6; % cutoff frequency
% desired impulse response:
hd = inline('1*(abs(om) < oc)', 'om', 'oc');
stem(n, hd, 'filled')
axis([0 M-1 -0.1, 0.3]), xlabel 'n', ylabel 'h[n]'

但是我收到以下错误

  

???使用==&gt;时出错inline.subsref at 14   没有足够的内联函数输入。

     

==&gt;中的错误xychk在80岁   if isvectorY,y = y(:);端

     

==&gt;中的错误干在43   [msg,x,y] = xychk(args {1:nargs},'plot');

我觉得内联功能有足够的输入。但错误说没有。 任何帮助将不胜感激。

编辑#1

所以我学会了如何使用匿名函数,并希望正确使用它,但现在我有另一个小错误。这是编辑过的代码。

 M = 26;
n = [0:(M-1)];
om = linspace(-pi, pi, 201); % for displaying frequency response  
oc = pi/6; % cutoff frequency
% desired impulse response:
hd = @(om,oc) 1*abs(om) < oc;
hn = hd(om,oc);
stem(n, hn, 'filled')
axis([0 M-1 -0.1, 0.3]), xlabel 'n', ylabel 'h[n]'

我得到错误X必须与词干中的Y相同。我明白了。但我不明白如何使n和hn等长。 n是从-pi到+ pi我相信。但也不是从-pi到+ pi。如果不是,也可以告诉如何从-pi到pi。

1 个答案:

答案 0 :(得分:3)

这里的问题是stem在尝试从内联函数中获取y值时,不知道ocom的值。

一般来说,最好使用匿名函数而不是内联(因为内联将来会过时):

hd = @(x,y) 1*abs(x)<y;

stem(n,hd(om,oc),'filled') %# this is also how you should call stem if you use the inline

@(...)部分定义了函数所需的输入数量;之后的部分说明了两个输入的功能。请注意,您可以在函数定义中显示其他变量。它们的值在定义匿名函数时是固定的。

输出功能类似于例如sin,可以这样调用。