File Exchange

image thumbnail

PLOTCUBE

version 1.0.0.0 (1.74 KB) by Olivier
Plot a 3D-cube

135 Downloads

Updated 01 Jun 2007

View License

PLOTCUBE - Display a 3D-cube in the current axes

PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes
with the following properties:
* EDGES : 3-elements vector that defines the length of cube edges
* ORIGIN: 3-elements vector that defines the start point of the cube
* ALPHA : scalar that defines the transparency of the cube faces (from 0
to 1)
* COLOR : 3-elements vector that defines the faces color of the cube

Example:
>> plotcube([5 5 5],[ 2 2 2],.8,[1 0 0]);
>> plotcube([5 5 5],[10 10 10],.8,[0 1 0]);
>> plotcube([5 5 5],[20 20 20],.8,[0 0 1]);

Comments and Ratings (30)

JP Schnyder

>> plotcube([.5 .5 .5],[.2 .2 .2],0.1,[0 0 1])
Unrecognized function or variable 'plotcube'.

How can I avoid to get this error ? I am using Matlab R2019b on Windows 10. The same problem occurs in the Matlab Android app !

Brad Atkins

Adam Danz

Simple and intuitive! I would recommend adding an axis handle input, replacing mat2cell with num2cell, and returning a vector of object handles for each side of the cube. It would look something like this.

function h = PLOTCUBE(varargin)
inArgs = { ...
[10 56 100] , ... % Default edge sizes (x,y and z)
[10 10 10] , ... % Default coordinates of the origin point of the cube
.7 , ... % Default alpha value for the cube's faces
[1 0 0] , ... % Default Color for the cube
gca ... % default axis
};
% Replace default input arguments by input values
inArgs(1:nargin) = varargin;
% Create all variables
[edges,origin,alpha,clr,axh] = deal(inArgs{:});
XYZ = { ...
[0 0 0 0] [0 0 1 1] [0 1 1 0] ; ...
[1 1 1 1] [0 0 1 1] [0 1 1 0] ; ...
[0 1 1 0] [0 0 0 0] [0 0 1 1] ; ...
[0 1 1 0] [1 1 1 1] [0 0 1 1] ; ...
[0 1 1 0] [0 0 1 1] [0 0 0 0] ; ...
[0 1 1 0] [0 0 1 1] [1 1 1 1] ...
};
XYZ = mat2cell(...
cellfun( @(x,y,z) x*y+z , ...
XYZ , ...
repmat(num2cell(edges),6,1) , ...
repmat(num2cell(origin),6,1) , ...
'UniformOutput',false), ...
6,[1 1 1]);
h = cellfun(@patch,repmat({axh},6,1),...
XYZ{1},XYZ{2},XYZ{3},...
repmat({clr},6,1),...
repmat({'FaceAlpha'},6,1),...
repmat({alpha},6,1)...
);
view(3);

Adam Danz

Grace Lee

Ido Golan

Amy Lowe

Sunita Saha

After plotting the cubes, i ant to store the coordinates of the vertices of the cubes. how to do it?

mr mo

@Osman Akar: Do you have any idea how to add legend to this function ?

mr mo

Are you have any idea how to add legend to this function ?

Osman Akar

Nice code but kind of slow

Thanks for the code Olivier.
I agree with the proposal of Mike to optimise the code for large datasets.
Here is an example of application for 3D visualization of uint8 images (which becomes heavy with large data):

figure(1);colormap gray
for i=1:Nx
for j=1:Ny
for k=1:Nz
figure(1); plotcube([1 1 1],[i-1 j-1 k-1],.8,I(i,j,k)); hold on
end
end
end
set(gca,'DataAspectRatio',[100 100 100]);

where I is a 3D image of size (Nx,Ny,Nz).

Mike

Hi Oliver,
Great code, thanks :)
However, is there any way to change the edge transparency on the cube?
e.g. something like adding "EdgeAlpha" into the code, such as a line below the current line with "FaceAlpha":
repmat({'FaceAlpha'},6,1),...

Also, is there any way to optimise this code for very large datasets? (e.g. several hundred thousand blocks!)
e.g. changing the data type to uint8 or uint16 wherever possible, somehow?
(If I try to feed in data in uint8 format there is an error in 'cellfun':

Error in plotcube>@(x,y,z)x*y+z (line 41)
cellfun( @(x,y,z) x*y+z , ...

Cheers again!
Mike

Kazawar

Lee

THANKS VERY MUCH

Xindi Li

Thanks :)

Thouraya

THANX

Ahmed

thanks really helpful

Panintorn

It's very nice

Vicente

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: rppd