I have written this MATLAB function to plot the impulse response. But it doesn't give me the desired output (impulse function). I am new to MATLAB. Can anyone tell me where i went wrong with the code?

   function [] = impulse() 
     title('impulse response');

2 Answers 2


In your code there is no end statement for the loop.There should be an end statement after the end for the if statement.Also x=1 inside the if condition converts the x vector into a vector of length 1.It should be changed as x(i+11)=1.Then the code works to give you a plot of a delta function


If you write zeros(length(t)) you are creating a matrix with length(t) rows and length(t) columns. Instead, you should write zeros(1, length(t)) in order to have an array. Also you don't need a for loop. Try this:

   t = -10:1:10;
   x = zeros(1, length(t));
   x(t==0) = 1;

However, an end is missing in your code, because both if and for need it, and you should write x(i) in order to insert 1 in that specific position.

  • $\begingroup$ Any chance to elaborate, instead of simply pasting the code? $\endgroup$
    – jojek
    Jul 8, 2015 at 11:30
  • $\begingroup$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. $\endgroup$
    – jojek
    Jul 8, 2015 at 11:30
  • $\begingroup$ I edited my answer. $\endgroup$
    – firion
    Jul 8, 2015 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.