The stem() function in MATLAB is specifically designed for visualizing discrete-time signals and sequences. In continuous plotting (using `plot()`), data points are connected by lines — which is mathematically incorrect for discrete/data-sampled signals. stem() solves this by drawing a vertical line (stem) from the baseline (usually zero) to each data point and placing a marker at the top — exactly how discrete sequences are represented in textbooks and research papers.
Why stem() Exists ?
### Basic Syntax & Return Values
h = stem(Y) % Y vs its index
h = stem(X,Y) % Custom x-axis
h = stem(...,'filled') % Filled markers
h = stem(ax,...) % Target axes
h = stem(...,Name,Value) % Customization
1.Simple Discrete Sequence (Most Common Use)
% Theory: Plot x[n] = [0,1,2,3,2,1,0,-1,-2,-1,0] for 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. Unit Impulse & Unit Step (Core DSP Concepts)
n = -5:10;
delta = (n == 0); % Kronecker delta δ[n]
step = (n >= 0); % Unit step u[n]
subplot(2,1,1); stem(n, delta, 'filled', 'MarkerSize', 8);
title('Unit Impulse δ[n]'); grid on;
subplot(2,1,2); stem(n, step, 'filled', 'Color', 'm');
title('Unit Step u[n]'); grid on;
3. 3D Stem Plot using stem3()
[X,Y] = meshgrid(-5:1:5, -5:1:5);
Z = X.*exp(-X.^2 - Y.^2); % 3D Gaussian
stem3(X, Y, Z, 'filled', 'MarkerSize', 6);
title('3D Discrete Surface using stem3()');
xlabel('X'); ylabel('Y'); zlabel('Z');
colorbar;
4. Plotting Impulse Response of a Filter
b = [1 0.5]; a = [1 -0.9]; % Simple IIR filter
[h, n] = impz(b, a, 30); % 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;
| Feature | stem() | plot() |
|---|---|---|
| Discrete data | Correct representation | Misleading (implies continuity) |
| DSP & signal processing | Industry standard | Not used |
| Marker + vertical line | Yes | No |
| Baseline control | Yes (BaseLine property) | No |
The stem() function is not just cosmetic — it is the mathematically and academically correct way to visualize discrete-time signals in MATLAB. Always prefer stem() over plot() when working with sequences, digital filters, DFT/FFT results, or any sampled data.
“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”
“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”
Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.
Introduction Not long ago, AI governance was a topic reserved for policy rooms and ethics co
MATLAB and Simulink continue to be powerful tools for modeling, simulation, and system design across engineering domains. From electric vehicles to smart grids and AI-driven automation, MATLAB simulation is playing a critical role in modern research and industry ap