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.
plot() for continuous functions and smooth physical signals.stem() for discrete-time, digital, or sampled data.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% 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;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;[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;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;| 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.
“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.
One of the most fascinating and sought-after projects for engineering students is autonomous vehicles. One of the main challenges in the development of self-driving cars is path planning, which is the process of determining a safe, collision-free route from start to o
The Battery Management System (BMS) serves as the intelligent brain of an electric vehicle (EV) battery pack. It ensures safety, maximizes performance, extends battery life, and optimizes energy usage in real-world driving conditions. As EVs become mainst