Urvah Shabbir
Urvah Shabbir

Reputation: 985

MATLAB inline function error

I am trying to run this simple MATLAB routine. which will plot a window function.

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]'

But i am getting the following error

??? Error using ==> inline.subsref at 14 Not enough inputs to inline function.

Error in ==> xychk at 80 if isvectorY, y = y(:); end

Error in ==> stem at 43 [msg,x,y] = xychk(args{1:nargs},'plot');

i feel inline function has enough inputs . but error says no. Any help would be appreciated.

EDIT # 1

so i learned how to use anonymous function and hopefully used it correctly but now i am having another little error. Here is the edited code.

 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]'

i get the error X must be same length as Y in stem. I get the point. But i cant understand how to make n and hn of equal length. n is from -pi to + pi i am sure. but isnt hd also from -pi to + pi. Also can you tell how to make it from -pi to pi if it isnt already.

Upvotes: 0

Views: 2031

Answers (1)

Jonas
Jonas

Reputation: 74940

The issue here is that stem doesn't know the value of oc and om when it tries to get y-values from your inline function.

In general, it is preferable to use anonymous functions instead of inlines (also since inlines will be obsolete in the future):

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

The @(...) part defines how many inputs the function takes; the part after than states the function of the two inputs. Note that you can have additional variables appearing in the function definition. Their values are fixed at the time the anonymous function is defined.

The output is a function like e.g. sin, and can be called as such.

Upvotes: 3

Related Questions