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.
Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.
Real feedback from students across top engineering universities worldwide.
“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!”
“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!”
Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.
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...