How can I write a MATLAB script to plot 3D and 2D knee joint data from a CSV file with customized axes and medial–lateral connections?
The CSV file contains the following data:
Sensor names: sensorNames = data.Sensor(1:6,1)
Sensor coordinates: sensorCoordinates = data{1:6, 2:4}
Source number: (:,5)
Source coordinates: sourceCoordinates = data{:, 6:end}
I want to create a 3D plot in MATLAB that includes the following:
Plot the sensor names and their coordinates.
Plot the source coordinates. The source coordinates are categorized into two groups:
Medial: sourceCoordinates = data{7:end, 6:end}
Lateral: sourceCoordinates = data{1:6, 6:end}
3. Set the axis directions depending on the knee side (see figure. 1 below), using S2 as the reference sensor (0,0,0):
Figure.1
Right knee: X-axis positive leftward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
Left knee: X-axis positive rightward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
4. Generate three types of plots (similar to the figure.2):
A 3D plot with all axes.
A 2D plot of X vs. Y.
A 2D plot of X vs. Z.
5. Add a line (like the green line in the figure.2 below) that connects the medial and lateral sources. This line should represent the knee joint range in all axis directions.
Figure.2
Knee joint range determination table.
I would like to write a MATLAB script that reads the CSV file, extracts the relevant data, and generates the 3D and 2D plots including the above details.
I wrote the following code:
%% Read and Plot 3D/2D Data from CSV File
% Load the CSV file
filename = ‘3DPlot_KLII_Mori.csv’;
data = readtable(filename);
%% Extract Sensor and Source Data
sensorNames = data.Sensor(1:6, 1); % Sensor names
sensorCoordinates = data{1:6, 2:4}; % Sensor coordinates (X, Y, Z)
sourceNumbers = data{:, 5}; % Source numbers
sourceCoordinates = data{:, 6:end}; % All source coordinates
% Categorize source coordinates
lateralSources = data{1:6, 6:end}; % Lateral sources
medialSources = data{7:end, 6:end}; % Medial sources
%% User input for knee side
kneeSide = "Right"; % Change to "Left" if left knee
% Adjust axis directions based on knee side
if strcmpi(kneeSide, "Right")
% Right knee: X positive leftward, Y downward, Z downward
sensorCoordinates(:,1) = -sensorCoordinates(:,1);
lateralSources(:,1) = -lateralSources(:,1);
medialSources(:,1) = -medialSources(:,1);
elseif strcmpi(kneeSide, "Left")
% Left knee: X positive rightward, Y downward, Z downward
% No inversion needed
else
error(‘Knee side must be either "Right" or "Left".’);
end
%% Plotting – 3D Plot
figure;
hold on;
% Plot sensor coordinates
scatter3(sensorCoordinates(:,1), sensorCoordinates(:,2), sensorCoordinates(:,3), …
80, ‘filled’, ‘MarkerFaceColor’, ‘b’);
% Add sensor name labels
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), sensorCoordinates(i,3), …
[‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
% Plot lateral sources
scatter3(lateralSources(:,1), lateralSources(:,2), lateralSources(:,3), …
60, ‘o’, ‘MarkerEdgeColor’, ‘r’, ‘DisplayName’, ‘Lateral Sources’);
% Plot medial sources
scatter3(medialSources(:,1), medialSources(:,2), medialSources(:,3), …
60, ‘s’, ‘MarkerEdgeColor’, ‘g’, ‘DisplayName’, ‘Medial Sources’);
%% Formatting for 3D plot
grid on;
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title([‘3D Plot of Sensors and Sources (‘ kneeSide ‘ Knee)’]);
legend;
view(3);
hold off;
%% Plotting – 2D Plot (X vs Y)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,2), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,2), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,2), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
title([‘2D Plot (X vs Y) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off;
%% Plotting – 2D Plot (X vs Z)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,3), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,3), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,3), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,3), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Z-axis’);
title([‘2D Plot (X vs Z) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off;The CSV file contains the following data:
Sensor names: sensorNames = data.Sensor(1:6,1)
Sensor coordinates: sensorCoordinates = data{1:6, 2:4}
Source number: (:,5)
Source coordinates: sourceCoordinates = data{:, 6:end}
I want to create a 3D plot in MATLAB that includes the following:
Plot the sensor names and their coordinates.
Plot the source coordinates. The source coordinates are categorized into two groups:
Medial: sourceCoordinates = data{7:end, 6:end}
Lateral: sourceCoordinates = data{1:6, 6:end}
3. Set the axis directions depending on the knee side (see figure. 1 below), using S2 as the reference sensor (0,0,0):
Figure.1
Right knee: X-axis positive leftward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
Left knee: X-axis positive rightward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
4. Generate three types of plots (similar to the figure.2):
A 3D plot with all axes.
A 2D plot of X vs. Y.
A 2D plot of X vs. Z.
5. Add a line (like the green line in the figure.2 below) that connects the medial and lateral sources. This line should represent the knee joint range in all axis directions.
Figure.2
Knee joint range determination table.
I would like to write a MATLAB script that reads the CSV file, extracts the relevant data, and generates the 3D and 2D plots including the above details.
I wrote the following code:
%% Read and Plot 3D/2D Data from CSV File
% Load the CSV file
filename = ‘3DPlot_KLII_Mori.csv’;
data = readtable(filename);
%% Extract Sensor and Source Data
sensorNames = data.Sensor(1:6, 1); % Sensor names
sensorCoordinates = data{1:6, 2:4}; % Sensor coordinates (X, Y, Z)
sourceNumbers = data{:, 5}; % Source numbers
sourceCoordinates = data{:, 6:end}; % All source coordinates
% Categorize source coordinates
lateralSources = data{1:6, 6:end}; % Lateral sources
medialSources = data{7:end, 6:end}; % Medial sources
%% User input for knee side
kneeSide = "Right"; % Change to "Left" if left knee
% Adjust axis directions based on knee side
if strcmpi(kneeSide, "Right")
% Right knee: X positive leftward, Y downward, Z downward
sensorCoordinates(:,1) = -sensorCoordinates(:,1);
lateralSources(:,1) = -lateralSources(:,1);
medialSources(:,1) = -medialSources(:,1);
elseif strcmpi(kneeSide, "Left")
% Left knee: X positive rightward, Y downward, Z downward
% No inversion needed
else
error(‘Knee side must be either "Right" or "Left".’);
end
%% Plotting – 3D Plot
figure;
hold on;
% Plot sensor coordinates
scatter3(sensorCoordinates(:,1), sensorCoordinates(:,2), sensorCoordinates(:,3), …
80, ‘filled’, ‘MarkerFaceColor’, ‘b’);
% Add sensor name labels
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), sensorCoordinates(i,3), …
[‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
% Plot lateral sources
scatter3(lateralSources(:,1), lateralSources(:,2), lateralSources(:,3), …
60, ‘o’, ‘MarkerEdgeColor’, ‘r’, ‘DisplayName’, ‘Lateral Sources’);
% Plot medial sources
scatter3(medialSources(:,1), medialSources(:,2), medialSources(:,3), …
60, ‘s’, ‘MarkerEdgeColor’, ‘g’, ‘DisplayName’, ‘Medial Sources’);
%% Formatting for 3D plot
grid on;
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title([‘3D Plot of Sensors and Sources (‘ kneeSide ‘ Knee)’]);
legend;
view(3);
hold off;
%% Plotting – 2D Plot (X vs Y)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,2), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,2), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,2), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
title([‘2D Plot (X vs Y) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off;
%% Plotting – 2D Plot (X vs Z)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,3), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,3), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,3), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,3), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Z-axis’);
title([‘2D Plot (X vs Z) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off; The CSV file contains the following data:
Sensor names: sensorNames = data.Sensor(1:6,1)
Sensor coordinates: sensorCoordinates = data{1:6, 2:4}
Source number: (:,5)
Source coordinates: sourceCoordinates = data{:, 6:end}
I want to create a 3D plot in MATLAB that includes the following:
Plot the sensor names and their coordinates.
Plot the source coordinates. The source coordinates are categorized into two groups:
Medial: sourceCoordinates = data{7:end, 6:end}
Lateral: sourceCoordinates = data{1:6, 6:end}
3. Set the axis directions depending on the knee side (see figure. 1 below), using S2 as the reference sensor (0,0,0):
Figure.1
Right knee: X-axis positive leftward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
Left knee: X-axis positive rightward, Y-axis positive downward, Z-axis positive perpendicular to the depth of the plane.
4. Generate three types of plots (similar to the figure.2):
A 3D plot with all axes.
A 2D plot of X vs. Y.
A 2D plot of X vs. Z.
5. Add a line (like the green line in the figure.2 below) that connects the medial and lateral sources. This line should represent the knee joint range in all axis directions.
Figure.2
Knee joint range determination table.
I would like to write a MATLAB script that reads the CSV file, extracts the relevant data, and generates the 3D and 2D plots including the above details.
I wrote the following code:
%% Read and Plot 3D/2D Data from CSV File
% Load the CSV file
filename = ‘3DPlot_KLII_Mori.csv’;
data = readtable(filename);
%% Extract Sensor and Source Data
sensorNames = data.Sensor(1:6, 1); % Sensor names
sensorCoordinates = data{1:6, 2:4}; % Sensor coordinates (X, Y, Z)
sourceNumbers = data{:, 5}; % Source numbers
sourceCoordinates = data{:, 6:end}; % All source coordinates
% Categorize source coordinates
lateralSources = data{1:6, 6:end}; % Lateral sources
medialSources = data{7:end, 6:end}; % Medial sources
%% User input for knee side
kneeSide = "Right"; % Change to "Left" if left knee
% Adjust axis directions based on knee side
if strcmpi(kneeSide, "Right")
% Right knee: X positive leftward, Y downward, Z downward
sensorCoordinates(:,1) = -sensorCoordinates(:,1);
lateralSources(:,1) = -lateralSources(:,1);
medialSources(:,1) = -medialSources(:,1);
elseif strcmpi(kneeSide, "Left")
% Left knee: X positive rightward, Y downward, Z downward
% No inversion needed
else
error(‘Knee side must be either "Right" or "Left".’);
end
%% Plotting – 3D Plot
figure;
hold on;
% Plot sensor coordinates
scatter3(sensorCoordinates(:,1), sensorCoordinates(:,2), sensorCoordinates(:,3), …
80, ‘filled’, ‘MarkerFaceColor’, ‘b’);
% Add sensor name labels
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), sensorCoordinates(i,3), …
[‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
% Plot lateral sources
scatter3(lateralSources(:,1), lateralSources(:,2), lateralSources(:,3), …
60, ‘o’, ‘MarkerEdgeColor’, ‘r’, ‘DisplayName’, ‘Lateral Sources’);
% Plot medial sources
scatter3(medialSources(:,1), medialSources(:,2), medialSources(:,3), …
60, ‘s’, ‘MarkerEdgeColor’, ‘g’, ‘DisplayName’, ‘Medial Sources’);
%% Formatting for 3D plot
grid on;
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
zlabel(‘Z-axis’);
title([‘3D Plot of Sensors and Sources (‘ kneeSide ‘ Knee)’]);
legend;
view(3);
hold off;
%% Plotting – 2D Plot (X vs Y)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,2), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,2), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,2), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,2), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Y-axis’);
title([‘2D Plot (X vs Y) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off;
%% Plotting – 2D Plot (X vs Z)
figure;
hold on;
scatter(sensorCoordinates(:,1), sensorCoordinates(:,3), 80, ‘filled’, ‘b’);
for i = 1:length(sensorNames)
text(sensorCoordinates(i,1), sensorCoordinates(i,3), [‘ ‘ char(sensorNames{i})], ‘FontSize’, 10, ‘Color’, ‘b’);
end
scatter(lateralSources(:,1), lateralSources(:,3), 60, ‘ro’, ‘DisplayName’, ‘Lateral Sources’);
scatter(medialSources(:,1), medialSources(:,3), 60, ‘gs’, ‘DisplayName’, ‘Medial Sources’);
xlabel(‘X-axis’);
ylabel(‘Z-axis’);
title([‘2D Plot (X vs Z) – ‘ kneeSide ‘ Knee’]);
legend;
grid on;
hold off; matlab, 3d-plot, customized axes MATLAB Answers — New Questions









