# Special plot3 command in Matlab

43 views (last 30 days)
mr mo on 2 Dec 2017
Commented: mr mo on 10 Dec 2017
Hi. I have a matrix named S, e.g.
S =
[25 4 14
26 4 14
27 4 14
28 4 14
29 4 14
30 4 14
31 4 14
32 4 14
33 4 14];
Every row in matrix S indicates a point in 3D space. I want to use plot3 command to plot these points.
plot3(S(:,1),S(:,2),S(:,3),'s')
This code plots these points with squares, but I want to have cubes instead of squares. How can I do that?
Thanks.

Jan on 2 Dec 2017
As you can find the in the documentation of plot3, or to be exact at the link to "line properties", cubes are not a recognized marker type. Then you have to draw them by your own.
It matters if you want the wireframe of the cube or filled patches.
See e.g. https://www.mathworks.com/matlabcentral/fileexchange/15161-plotcube or http://jialunliu.com/how-to-use-matlab-to-plot-3d-cubes/ (I magically found these links by asking an internet search engine for the terms "Matlab draw cube" - you can do this by your own also.)

mr mo on 9 Dec 2017
@Jan Simon: Do you have any idea how to add legend to this function? Thanks a lot.
Jan on 10 Dec 2017
@mr mo: The link in your former comment is dead now. For which function are you searching a legend? Did you try to provide one handle per legend entry? The plotcube command would need to add an output of the handles. But I would take this code as example only, how to call patch. E.g.:
side = 5;
verts = ([0 0 0;0 1 0;1 1 0;1 0 0;0 0 1;0 1 1;1 1 1;1 0 1] - 0.5) .* side;
face = [1 2 3 4;5 6 7 8;3 4 8 7;1 2 6 5;2 3 7 6;1 4 8 5];
h1 = patch('Faces',face,'Vertices',verts,'FaceColor','b', ...
'EdgeColor','w');
h2 = patch('Faces',face,'Vertices',verts + 8,'FaceColor','g', ...
'EdgeColor','k');
legend([h1, h2], {'1', '2'})
view(45, 30)
grid on
mr mo on 10 Dec 2017
Thanks a lot. I want to add legend to plotcube function.