How to Use stem() Function in MATLAB to Plot Discrete-Time Signals

MATLAB Illustration

The stem() function in MATLAB is built specifically for visualizing discrete-time signals and sequences. While plot() connects data points with continuous lines, doing so on sampled or digital data is mathematically misleading. stem() fixes this by drawing a vertical line from the baseline to each data point topped with a marker—matching how discrete signals appear in DSP textbooks and engineering papers.

When Should You Use stem()?

  • Use plot() for continuous functions and smooth physical signals.
  • Use stem() for discrete-time, digital, or sampled data.
  • It highlights exact sample locations and amplitudes without falsely implying data exists between samples.
  • It is the standard choice in digital signal processing, control systems, and digital filter design.

Basic Syntax Options

MATLAB
h = stem(Y)               
% Plots Y values against their index numbers h = stem(X, Y)            
% Plots Y values at custom X coordinates h = stem(..., 'filled')   % Fills the top markers h = stem(ax, ...)         % Targets a specific axes object h = stem(..., Name, Value)% Customizes colors, line widths, and marker types

Practical Examples

1. Plotting a Basic Discrete Sequence

MATLAB
% Define a sequence x[n] from n = 0 to 10 n = 0:10;
x = [0 1 2 3 2 1 0 -1 -2 -1 0];
stem(n, x, 'filled', 'MarkerFaceColor', 'r', 'LineWidth', 1.5);
title('Basic Discrete Sequence x[n]');
xlabel('Sample index n');
ylabel('Amplitude');
grid on;

2. Generating Unit Impulse & Unit Step Signals

MATLAB
n = -5:10;
delta = (n == 0);
% Kronecker delta δ[n] step  = (n >= 0);
% Unit step u[n]  sub
plot(2,1,1);
stem(n, delta, 'filled', 'MarkerSize', 8);
title('Unit Impulse δ[n]');
grid on;
sub
plot(2,1,2);
stem(n, step, 'filled', 'Color', 'm');
title('Unit Step u[n]');
grid on;

3. 3D Discrete Data Visualization using stem3()

MATLAB
[X, Y] = meshgrid(-5:1:5, -5:1:5);
Z = X .* exp(-X.^2 - Y.^2);
% 3D Gaussian surface points  stem3(X, Y, Z, 'filled', 'MarkerSize', 6);
title('3D Discrete Surface using stem3()');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;

4. Plotting the Impulse Response of an IIR Filter

MATLAB
b = [1 0.5];
a = [1 -0.9];
% IIR filter coefficients [h, n] = impz(b, a, 30);
% Calculate impulse response  stem(n, h, 'filled', 'MarkerFaceColor', 'g');
title('Impulse Response of H(z) = (1 + 0.5z^{-1}) / (1 - 0.9z^{-1})');
xlabel('n');
ylabel('h[n]');
grid on;

Quick Comparison: stem() vs. plot()

Feature stem() plot()
Discrete Data Representation Accurate and clear Misleading (implies continuous values between points)
Signal Processing Standard Yes (DSP & digital filters) No
Visual Marker & Vertical Line Yes No
Baseline Control Yes (via BaseLine property) No

Whenever you are working with sequences, digital filter responses, DFT/FFT outputs, or discrete sampled data, use stem() to keep your plots mathematically accurate.

Need Expert Guidance with MATLAB or Simulink?

Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

Solving 6-DOF Aircraft & Quadrotor Flight Dynamics in MATLAB: ode45 vs. ode15s and Quaternion Kinematics

Simulating a 6-Degree-of-Freedom (6-DOF) flight vehicle in MATLAB looks straightforward on paper. You set up Newton-Euler equations of motion, calculate aerodynamic...