You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
3D plotting of boxes using coordinates in CSV file
10 views (last 30 days)
Show older comments
hi,
i need help on how to plot the coordinates stored in csv file in a 3D visual.
input CSV file:

expected visual:

the code im working on which is not giving th eexpected output:
Accepted Answer
Voss
on 19 Dec 2023
solutionData = [ ...
4 0 0 0 0 15 0 0 0 20 0 15 20 25 0 0 25 15 0 25 0 20 25 15 20; ...
2 25 0 0 25 18 0 25 0 12 25 18 12 33 0 0 33 18 0 33 0 12 33 18 12; ...
3 25 18 0 25 23 0 25 18 14 25 23 14 35 18 0 35 23 0 35 18 14 35 23 14; ...
5 25 0 12 25 12 12 25 0 30 25 12 30 47 0 12 47 12 12 47 0 30 47 12 30; ...
1 25 12 12 25 32 12 25 12 22 25 32 22 40 12 12 40 32 12 40 12 22 40 32 22];
coordinates = solutionData(:, 2:end);
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
figure
F = [1 2 4 3; 5 6 8 7; 1 2 6 5; 4 3 7 8; 2 6 8 4; 1 5 7 3];
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).';
patch( ...
'Vertices',C, ...
'Faces',F, ...
'FaceColor','flat', ...
'FaceVertexCData',colors(ii,:), ...
'FaceAlpha',0.5);
end
view(3)
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Visualization of Boxes');
grid on;
24 Comments
Thulasy Chandran
on 20 Dec 2023
is there any way to attach the csv file directly rather than listing the coordinates?
Voss
on 20 Dec 2023
To attach the csv file here, use the paperclip button.
To read the file in your code, use readmatrix, readcell, or readtable.
I only listed the coordinates because I didn't have the file.
Thulasy Chandran
on 20 Dec 2023
i have attached the file here. is it possible to do a bounding box for this?
Voss
on 20 Dec 2023
Yes, you would calculate the coordinates of the bounding box and then plot it like the individual boxes are plotted.
Thulasy Chandran
on 22 Dec 2023
if i want to plot the bounding box according to the box arrangement, how do i do that?
For example, something like this?

Voss
on 22 Dec 2023
solutionFile = '0 - GA (20-Dec-2023 06.09.07).csv';
solutionData = readmatrix(solutionFile);
% get the two bounding box corners (min(x),min(y),min(z)) and (max(x),max(y),max(z))
p1 = min(reshape(min(solutionData(:,2:end),[],1),3,[]).',[],1);
p8 = max(reshape(max(solutionData(:,2:end),[],1),3,[]).',[],1);
coordinates = solutionData(:, 2:end);
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p8,p1,p8,p8,p8];
p_bb([5 9 13]) = p8([2 3 1]);
p_bb([10 18 20]) = p1([1 3 2]);
coordinates(end+1,:) = p_bb;
N = size(coordinates,1);
colors = [0 1 0; 0 0 1; 1 0 0; 1 1 0; 1 0 1];
colors = repmat(colors,ceil(N/size(colors,1)),1);
% make the bounding box a special color:
colors(N,:) = [0.8 0.8 0.8];
figure
F = [1 2 4 3; 5 6 8 7; 1 2 6 5; 4 3 7 8; 2 6 8 4; 1 5 7 3];
for ii = 1:N
C = reshape(coordinates(ii, :), 3, []).';
patch( ...
'Vertices',C, ...
'Faces',F, ...
'FaceColor','flat', ...
'FaceVertexCData',colors(ii,:), ...
'FaceAlpha',0.5);
end
view(3)
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Visualization of Boxes');
grid on;

Thulasy Chandran
on 23 Dec 2023
how do i calculate the volume of the outer container and total boxes volume?
and how to calculate the middle point of container and middle point of the box as the formula below?
distance to outer middle = Ʃ (middle point of container – middle point of box)
Voss
on 23 Dec 2023
Volume is length times width times height, where length, width, and height are found by taking differences of coordinates. Middle point is average of coordinates.
Thulasy Chandran
on 26 Dec 2023
may i know how to point each row by separating x,y and z coordinates to get the average coordinates?
Voss
on 26 Dec 2023
solutionFile = '0 - GA (20-Dec-2023 06.09.07).csv';
T = readtable(solutionFile)
T = 5×25 table
PresentId x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4 x5 y5 z5 x6 y6 z6 x7 y7 z7 x8 y8 z8
_________ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
4 0 0 0 0 15 0 0 0 20 0 15 20 25 0 0 25 15 0 25 0 20 25 15 20
2 25 0 0 25 18 0 25 0 12 25 18 12 33 0 0 33 18 0 33 0 12 33 18 12
3 25 18 0 25 23 0 25 18 14 25 23 14 35 18 0 35 23 0 35 18 14 35 23 14
5 25 0 12 25 12 12 25 0 30 25 12 30 47 0 12 47 12 12 47 0 30 47 12 30
1 25 12 12 25 32 12 25 12 22 25 32 22 40 12 12 40 32 12 40 12 22 40 32 22
vars = T.Properties.VariableNames;
x = T{:,startsWith(vars,'x')}
x = 5×8
0 0 0 0 25 25 25 25
25 25 25 25 33 33 33 33
25 25 25 25 35 35 35 35
25 25 25 25 47 47 47 47
25 25 25 25 40 40 40 40
y = T{:,startsWith(vars,'y')}
y = 5×8
0 15 0 15 0 15 0 15
0 18 0 18 0 18 0 18
18 23 18 23 18 23 18 23
0 12 0 12 0 12 0 12
12 32 12 32 12 32 12 32
z = T{:,startsWith(vars,'z')}
z = 5×8
0 0 20 20 0 0 20 20
0 0 12 12 0 0 12 12
0 0 14 14 0 0 14 14
12 12 30 30 12 12 30 30
12 12 22 22 12 12 22 22
avg_x = mean(x,2)
avg_x = 5×1
12.5000
29.0000
30.0000
36.0000
32.5000
avg_y = mean(y,2)
avg_y = 5×1
7.5000
9.0000
20.5000
6.0000
22.0000
avg_z = mean(z,2)
avg_z = 5×1
10
6
7
21
17
Thulasy Chandran
on 26 Dec 2023
can you help me with the calculation of space left and distance to outer middle as well? im finding hard to do it.
Thulasy Chandran
on 27 Dec 2023
space left = outer container volume – total boxes volume
distance to outer middle = Ʃ (middle point of container – middle point of box)
Voss
on 28 Dec 2023
solutionFile = '0 - GA (20-Dec-2023 06.09.07).csv';
T = readtable(solutionFile)
T = 5×25 table
PresentId x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4 x5 y5 z5 x6 y6 z6 x7 y7 z7 x8 y8 z8
_________ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
4 0 0 0 0 15 0 0 0 20 0 15 20 25 0 0 25 15 0 25 0 20 25 15 20
2 25 0 0 25 18 0 25 0 12 25 18 12 33 0 0 33 18 0 33 0 12 33 18 12
3 25 18 0 25 23 0 25 18 14 25 23 14 35 18 0 35 23 0 35 18 14 35 23 14
5 25 0 12 25 12 12 25 0 30 25 12 30 47 0 12 47 12 12 47 0 30 47 12 30
1 25 12 12 25 32 12 25 12 22 25 32 22 40 12 12 40 32 12 40 12 22 40 32 22
vars = T.Properties.VariableNames;
x = T{:,startsWith(vars,'x')};
y = T{:,startsWith(vars,'y')};
z = T{:,startsWith(vars,'z')};
dx = max(x,[],2)-min(x,[],2);
dy = max(y,[],2)-min(y,[],2);
dz = max(z,[],2)-min(z,[],2);
DX = max(x(:))-min(x(:));
DY = max(y(:))-min(y(:));
DZ = max(z(:))-min(z(:));
v = dx.*dy.*dz;
V = DX*DY*DZ;
space_left = V-sum(v)
space_left = 27440
avg_x = (max(x,[],2)+min(x,[],2))/2;
avg_y = (max(y,[],2)+min(y,[],2))/2;
avg_z = (max(z,[],2)+min(z,[],2))/2;
AVG_X = (max(x(:))+min(x(:)))/2;
AVG_Y = (max(y(:))+min(y(:)))/2;
AVG_Z = (max(z(:))+min(z(:)))/2;
D = [avg_x avg_y avg_z]-[AVG_X AVG_Y AVG_Z];
distance_to_centers = sqrt(sum(D.^2,2))
distance_to_centers = 5×1
14.7733
12.6590
11.2472
17.0953
11.0000
Thulasy Chandran
on 2 Jan 2024
hi, i have question regarding the distance to centre.
avg_x avg_y avg_z - is the average coordinates of the points along the x, y, and z axes, respectively?
AVG_X AVG_Y AVG_Z - is the global center in 3D space or the centre of the outer container?
Voss
on 2 Jan 2024
Yes avg_* is the center of each small box and also the average of x, y, z for each box.
Yes AVG_* is the center of the outer box and also the average of x, y, z for the outer box.
Thulasy Chandran
on 2 Jan 2024
Edited: Thulasy Chandran
on 2 Jan 2024
for the distance to outer middle, need to find for a box and deduct it with the middle point of the outer container. This need to be done for all 5 boxes and then sum it all in the end.
distance to outer middle = Ʃ (middle point of container – middle point of box)
i tried to simulate the output for first and last generation of genetic algorithm. supposedly, the distance to centers should be lower for last generation based on the space left value but its higher.
First:
space_left =
27440
distance_to_centers =
14.7733
12.6590
11.2472
17.0953
11.0000
Last:
space_left =
24460
distance_to_centers =
19.2354
19.0066
11.6082
12.6293
22.3495
- based on the code provided, is it taking the middle point of all the boxes?
- is it possible to display the centre of the outer container and centre of one of the boxes in the visualization plot?
Voss
on 2 Jan 2024
1. My code is there; you can see what it does.
If you want to sum the distances, do this:
sum(distance_to_centers)
2. Yes, add some plot call(s) where you plot the points. I'm gonna let you figure that out.
Thulasy Chandran
on 8 Jul 2024
hi, how do we change this according to the datasets?
% append the bounding box coordinates to the end of the coordinates matrix:
p_bb = [p1,p1,p1,p8,p1,p8,p8,p8];
p_bb([5 9 13]) = p8([2 3 1]);
p_bb([10 18 20]) = p1([1 3 2]);
coordinates(end+1,:) = p_bb;
Thulasy Chandran
on 9 Jul 2024
i would like to expand my datasets. i would like to know how can change the boundary box plotting according to it.
dataset 1:
BoxID containerWidth containerLength containerHeight
1 26 34 45
2 28 32 40
3 30 36 44
4 25 35 42
5 32 33 41
6 27 38 46
7 29 39 48
8 31 37 47
9 34 40 43
10 33 31 49
dataset 2:
BoxID containerWidth containerLength containerHeight
1 51 60 70
2 55 62 65
3 53 68 72
4 50 61 66
5 59 64 67
6 57 65 71
7 56 69 74
8 60 66 73
9 54 63 68
10 52 70 75
11 61 67 64
12 58 59 69
13 62 71 63
14 63 72 61
15 64 73 62
More Answers (0)
See Also
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
