Import Data with Headers and Plot on a Plane
The standard way to load tabular files (CSV, TXT, Excel) containing column headers in MATLAB is using readtable(). This automatically preserves header names as variable properties.
1. Sample Data Creation (for Testing)
Run this snippet to create a test file named sensor_data.csv with header labels:
% Create sample table with headers
Time_sec = (0:0.1:10)';
Position_X = 2 * sin(Time_sec);
Position_Y = 3 * cos(Time_sec);
T_sample = table(Time_sec, Position_X, Position_Y);
writetable(T_sample, 'sensor_data.csv');
2. Import Data and Plot on a 2D Plane (X-Y Plane)
Read the table and reference columns directly by their header names with dot notation (data.HeaderName).
% Import CSV or Excel file with headers
data = readtable('sensor_data.csv');
% Display the first 5 rows and column names
head(data, 5)
% Plot Position_X vs Position_Y on a 2D plane
figure('Color', 'w');
plot(data.Position_X, data.Position_Y, 'b-o', ...
'LineWidth', 1.5, ...
'MarkerSize', 4, ...
'MarkerFaceColor', [0.2 0.6 0.9]);
% Set labels dynamically using table variable names
xlabel(data.Properties.VariableNames{2}, 'Interpreter', 'none');
ylabel(data.Properties.VariableNames{3}, 'Interpreter', 'none');
title('2D Plane Trajectory (Position_X vs Position_Y)');
grid on;
axis equal; % Maintain true aspect ratio
3. Plotting 3D Data Projected onto a Plane
To plot 3D coordinates and project them onto a 2D plane, use plot3() with view() or add drop lines to the baseline plane.
figure('Color', 'w');
% 3D trajectory
plot3(data.Time_sec, data.Position_X, data.Position_Y, 'r-', 'LineWidth', 1.5);
hold on;
% Add reference shadow projection on the bottom plane (Z = min)
z_floor = min(data.Position_Y) * ones(size(data.Position_Y));
plot3(data.Time_sec, data.Position_X, z_floor, 'Color', [0.7 0.7 0.7], 'LineStyle', '--');
xlabel('Time (s)');
ylabel('Position X');
zlabel('Position Y');
title('Trajectory with Plane Projection');
grid on;
view(45, 30); % Set 3D perspective
4. Import Options: Converting Headers to Matrix Directly
If you prefer numeric arrays rather than tables, skip the header row during import with readmatrix():
% Reads data starting below row 1
M = readmatrix('sensor_data.csv', 'NumHeaderLines', 1);
posX = M(:, 2);
posY = M(:, 3);
figure('Color', 'w');
scatter(posX, posY, 25, 'filled');
xlabel('X Position');
ylabel('Y Position');
grid on;
Tips:
readtable('filename.csv')works for.csv,.txt,.xls, and.xlsxfiles.- Use
'Interpreter', 'none'insidexlabel()andylabel()so underscores in header names (e.g.Time_sec) display normally without turning into subscripts. - Use
axis equalwhen plotting spatial 2D coordinates so circles and distances do not distort.
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: