In MATLAB, both the plot and stem functions are used to visualize data curves, but they represent data in fundamentally different formats:
plot function: Displays continuous data values by connecting data points with straight line segments. Think of drawing the graph of y = sin(x) smoothly with a pencil without lifting it from the paper.stem function: Displays discrete data values by drawing vertical stems extending from the baseline (x-axis) to each data point with a marker at the top. Think of plotting y = sin(x) for specific angles (such as 0°, 15°, 30°, 45°, 60°, and 90°) and drawing individual vertical lines from the x-axis to each point.Run the following MATLAB code to compare plot and stem side-by-side using subplots:
clc;
clear all;
close all;
% Generate 50 points linearly spaced between -pi and pi x = linspace(-pi, pi, 50);
y = sin(x);
% Subplot 1: Continuous representation using
plot() sub
plot(1, 2, 1);
plot(x, y, 'b-', 'LineWidth', 1.5);
xticks([-pi 0 pi]);
xticklabels({'-pi', '0', 'pi'});
title('Continuous Plot:
plot(x, y)');
xlabel('x (radians)');
ylabel('sin(x)');
grid on;
% Subplot 2: Discrete representation using stem() sub
plot(1, 2, 2);
stem(x, y, 'r', 'LineWidth', 1.2, 'MarkerFaceColor', 'r');
xticks([-pi 0 pi]);
xticklabels({'-pi', '0', 'pi'});
title('Discrete Stem Plot: stem(x, y)');
xlabel('x (radians)');
ylabel('sin(x)');
grid on;linspace(-pi, pi, 50) creates a vector of 50 evenly spaced points from -π to π.subplot(1, 2, 1) and subplot(1, 2, 2) divide the figure window into two side-by-side panels.xticks and xticklabels format the x-axis tick marks using LaTeX notation (-π, 0, π).grid on adds a background grid for better visual clarity.Conclusion: The plot panel presents a smooth, continuous waveform, whereas the stem panel highlights individual discrete sample points rooted at the baseline.
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.
Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...
Tracking complex trajectories with non-holonomic mobile robots requires handling physical constraints such as actuator saturation, wheel slip, and sharp cornering. Standard cont...