File Exchange

## PLOTCUBE

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

Updated 01 Jun 2007

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]);

### Cite As

Olivier (2020). PLOTCUBE (https://www.mathworks.com/matlabcentral/fileexchange/15161-plotcube), MATLAB Central File Exchange. Retrieved .

Navayo Erlangga

function [ ] = quaderplot( st,a,b,c,far)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
a=a+st;
plot3([st a],[0 0],[0 0],far)
hold on;
plot3([st st],[0 b],[0 0],far)
plot3([a a],[0 b],[0 0],far)
plot3([st a],[b b],[0 0],far)
plot3([st st],[0 0],[0 c],far)
plot3([st st],[b b],[0 c],far)
plot3([a a],[0 0],[0 c],far)
plot3([a a],[b b],[0 c],far)
plot3([st a],[0 0],[c c],far)
plot3([st st],[0 b],[c c],far)
plot3([a a],[0 b],[c c],far)
plot3([st a],[b b],[c c],far)
hold on;
end

yang yang

dralp

Thanks a lot, Olivier. It works fine. Would it be possible to color a group of cubes in a gradient?

Bruno Strano

hong yi

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 !

Jacob Stephens

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);

Grace Lee

Pouria Tajvar

Ido Golan

Amy Lowe

Daniel Green

Calebe Nunes

Anders Hansen

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

Stefan Schmohl

Elena Syerko

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

UNIVERSITY LAJHDAR

GOOD

Ahmed

Panintorn

It's very nice

Vicente

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