Legend in bar3 in MATLAB - matlab

I can't figure out how to create the legends (for colors) in the attached code?
test1=[5 10 7;
1 100 0;
1 3 2];
test2=[10 15 10;
10 80 10;
5 5 15];
test3=[10 10 10;
20 200 20;
30 10 30];
core=bar3(test1);
set(core,'FaceColor',[1 0 0]); %red
for i=1:length(core)
zz=get(core(i),'Zdata');
k=1;
for j= 0:6:(6*length(core)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);
k=k+1;
end
set(core(i),'Zdata',zz);
end
hold on
core=bar3(test2);
set(core,'FaceColor',[0 1 1]);%cyan
hold off
for i=1:length(core)
zz=get(core(i),'Zdata');
k=1;
for j= 0:6:(6*length(core)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test3(k,i);
k=k+1;
end
set(core(i),'Zdata',zz);
end
hold on
core=bar3(test3);
set(core,'FaceColor',[1 1 0]);%yellow
hold off

First, I would rename all cores to core1, core2 and core3 to correspond to test1, test2 and test3, and also not to override the handles. Then, if you check the size of each of these using e.g. size(core1), you will see that each of these contains 3 handles to 3 plots. All 3 have the same color, so you can just pick one handle and supply it to the legend as a first argument. So at the end you just need to add
legend([core1(1) core2(1) core3(1)], {'A', 'B', 'C'});
The full code
test1=[5 10 7;
1 100 0;
1 3 2];
test2=[10 15 10;
10 80 10;
5 5 15];
test3=[10 10 10;
20 200 20;
30 10 30];
core1=bar3(test1);
set(core1,'FaceColor',[1 0 0]); %red
for i=1:length(core1)
zz=get(core1(i),'Zdata');
k=1;
for j= 0:6:(6*length(core1)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);
k=k+1;
end
set(core1(i),'Zdata',zz);
end
legend({'A'});
hold on
core2=bar3(test2);
set(core2,'FaceColor',[0 1 1]);%cyan
hold off
for i=1:length(core2)
zz=get(core2(i),'Zdata');
k=1;
for j= 0:6:(6*length(core2)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test3(k,i);
k=k+1;
end
set(core2(i),'Zdata',zz);
end
hold on
core3=bar3(test3);
set(core3,'FaceColor',[1 1 0]);%yellow
hold off
legend([core1(1) core2(1) core3(1)], {'A', 'B', 'C'});

Related

Radar plot Area Fill - Matlab

This is a radar plot script for MATLAB I copied from Google Search. I was hoping to use radar plot with different scales with 4 or 5 axes.
How would I fill the graph of this radar plot? Should I use patch or fill? I know this shouldn't take a long time to do this. My math isn't that great.
Thanks in advance,
Gary
function [f, ca, o] = spider(data,tle,rng,lbl,leg,f)
% create a spider plot for ranking the data
% function [f, ca, o] = spider(data,tle,rng,lbl,leg,f)
%
% inputs 6 - 5 optional
% data input data (NxM) (# axes (M) x # data sets (N)) class real
% tle spider plot title class char
% rng peak range of the data (Mx1 or Mx2) class real
% lbl cell vector axes names (Mxq) in [name unit] pairs class cell
% leg data set legend identification (1xN) class cell
% f figure handle or plot handle class real
%
% outptus 3 - 3 optional
% f figure handle class integer
% x axes handle class real
% o series object handles class real
%
% michael arant - jan 30, 2008
%
% to skip any parameter, enter null []
%
% examples
%
% spider([1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15;16 17 18; ...
% 19 20 21; 22 23 24; 25 26 27]','test plot');
%
% spider([1 2 3 4; 4 5 6 7; 7 8 9 10; 10 11 12 13; 13 14 15 16; ...
% 16 17 18 19; 19 20 21 22; 22 23 24 25; 25 26 27 28],'test plot', ...
% [[0:3:24]' [5:3:29]'],[],{'Case 1' 'Case 2' 'Case 3' 'Case 4'});
%
% spider([1 2 3 4; 4 5 6 7; 7 8 9 10; 10 11 12 13; 13 14 15 16; ...
% 16 17 18 19; 19 20 21 22; 22 23 24 25; 25 26 27 28],'test plot', ...
% [],[],{'Case 1' 'Case 2' 'Case 3' 'Case 4'});
%
% figure; clf; set(gcf,'color','w'); s = zeros(1,4);
% for ii = 1:4; s(ii) = subplot(2,2,ii); end
%
% spider([1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15;16 17 18; ...
% 19 20 21; 22 23 24; 25 26 27]','test plot 1',[],[],[],s(1));
%
% spider([1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15;16 17 18; ...
% 19 20 21; 22 23 24; 25 26 27],'test plot 2',[0 30],[],[],s(2));
%
% spider([1 2 3 4; 4 5 6 7; 7 8 9 10; 10 11 12 13; 13 14 15 16; ...
% 16 17 18 19; 19 20 21 22; 22 23 24 25; 25 26 27 28]','test plot 3', ...
% [0 30],{'Label 1' 'Unit 1'; 'Label 2' 'Unit 2'; 'Label 3' 'Unit 3'; ...
% 'Label 4' 'Unit 4'},{'Case 1' 'Case 2' 'Case 3' 'Case 4' 'Case 5' ...
% 'Case 6' 'Case 7' 'Case 8' 'Case 9'},s(3));
%
% spider([1 2 3 4; 4 5 6 7; 7 8 9 10; 10 11 12 13; 13 14 15 16; ...
% 16 17 18 19; 19 20 21 22; 22 23 24 25; 25 26 27 28],'test plot 4', ...
% [[0:3:24]' [5:3:29]'],[],{'Case 1' 'Case 2' 'Case 3' 'Case 4'},s(4));
% data check
if nargin < 1; help spider; error('Need data to plot'); end
% size segments and number of cases
[r c] = size(data);
% exit for too few axes
if r < 3
errordlg('Must have at least three measuremnt axes')
error('Program Termination: Must have a minimum of three axes')
end
% title
if ~exist('tle','var') || isempty(tle) || ~ischar(tle)
tle = 'Spider Plot';
end
% check for maximum range
if ~exist('rng','var') || isempty(rng) || ~isreal(rng)
% no range given or range is in improper format
% define new range
rng = [min([min(data,[],2) zeros(r,1)],[],2) max(data,[],2)];
% check for negative minimum values
if ~isempty(ismember(-1,sign(data)))
% negative value found - adjust minimum range
for ii = 1:r
% negative range for axis ii - set new minimum
if min(data(ii,:)) < 0
rng(ii,1) = min(data(ii,:)) - ...
0.25 * (max(data(ii,:)) - min(data(ii,:)));
end
end
end
elseif size(rng,1) ~= r
if size(rng,1) == 1
% assume that all axes have commom scale
rng = ones(r,1) * rng;
else
% insuffent range definition
uiwait(msgbox(char('Range size must be Mx1 - number of axes x 1', ...
sprintf('%g axis ranges defined, %g axes exist',size(rng,1),r))))
error(sprintf('%g axis ranges defined, %g axes exist',size(rng,1),r))
end
elseif size(rng,2) == 1
% assume range is a maximum range - define minimum
rng = sort([min([zeros(r,1) min(data,[],2) - ...
0.25 * (max(data,[],2) - min(data,[],2))],[],2) rng],2);
end
% check for axis labels
if ~exist('lbl','var') || isempty(lbl)
% no labels given - define a default lable
lbl = cell(r,1); for ii = 1:r; lbl(ii) = cellstr(sprintf('Axis %g',ii)); end
elseif size(lbl,1) ~= r
if size(lbl,2) == r
lbl = lbl';
else
uiwait(msgbox(char('Axis labels must be Mx1 - number of axes x 1', ...
sprintf('%g axis labels defined, %g axes exist',size(lbl,1),r))))
error(sprintf('%g axis labels defined, %g axes exist',size(lbl,1),r))
end
elseif ischar(lbl)
% check for charater labels
lbl = cellstr(lbl);
end
if ~exist('leg','var') || isempty(leg)
% no data legend - define default legend
leg = cell(1,c); for ii = 1:c; leg(ii) = cellstr(sprintf('Set %g',ii)); end
elseif numel(leg) ~= c
uiwait(msgbox(char('Data set label must be 1XN - 1 x number of sets', ...
sprintf('%g data sets labeled, %g exist',numel(leg),c))))
error(sprintf('%g data sets labeled, %g exist',numel(leg),c))
end
% check for figure or axes
if ~exist('f','var')
% no figure or axes requested - generate new ones
f = figure; ca = gca(f); cla(ca); hold on; set(f,'color','w')
elseif ismember(f,get(0,'children')')
% existing figure - clear and set up
ca = gca(f); hold on;
elseif isint(f)
% generating a new figure
figure(f); ca = gca(f); cla(ca); hold on
else
% may be an axes - may be garbage
try
%is this an axes?
if ismember(get(f,'parent'),get(0,'children')')
% existing figure axes - use
ca = f; f = get(f,'parent'); hold on
end
catch
% make new figure and axes
disp(sprintf('Invalid axes handle %g passed. Generating new figure',f))
f = figure; ca = gca(f); cla(ca); hold on
end
end
% set the axes to the current text axes
axes(ca)
% set to add plot
set(ca,'nextplot','add');
% clear figure and set limits
set(ca,'visible','off'); set(f,'color','w')
set(ca,'xlim',[-1.25 1.25],'ylim',[-1.25 1.25]); axis(ca,'equal','manual')
% title
text(0,1.3,tle,'horizontalalignment','center','fontweight','bold');
% define data case colors
col = color_index(c);
% scale by range
angw = linspace(0,2*pi,r+1)';
mag = (data - rng(:,1) * ones(1,c)) ./ (diff(rng,[],2) * ones(1,c));
% scale trimming
mag(mag < 0) = 0; mag(mag > 1) = 1;
% wrap data (close the last axis to the first)
ang = angw(1:end-1); angwv = angw * ones(1,c); magw = [mag; mag(1,:)];
% make the plot
% define the axis locations
start = [zeros(1,r); cos(ang')]; stop = [zeros(1,r); sin(ang')];
% plot the axes
plot(ca,start,stop,'color','k','linestyle','-'); axis equal
% plot axes markers
inc = 0.25:.25:1; mk = .025 * ones(1,4); tx = 4 * mk; tl = 0:.25:1;
% loop each axis ang plot the line markers and labels
% add axes
for ii = 1:r
% plot tick marks
tm = plot(ca,[[cos(ang(ii)) * inc + sin(ang(ii)) * mk]; ...
[cos(ang(ii)) * inc - sin(ang(ii)) * mk]], ...
[[sin(ang(ii)) * inc - cos(ang(ii)) * mk] ;
[sin(ang(ii)) * inc + cos(ang(ii)) * mk]],'color','k');
% label the tick marks
for jj = 1:4
% temp = text([cos(ang(ii)) * inc(jj) + sin(ang(ii)) * tx(jj)], ...
% [sin(ang(ii)) * inc(jj) - cos(ang(ii)) * tx(jj)], ...
% num2str(chop(rng(ii,1) + inc(jj)*diff(rng(ii,:)),2)), ...
% 'fontsize',8);
temp = text([cos(ang(ii)) * inc(jj) + sin(ang(ii)) * tx(jj)], ...
[sin(ang(ii)) * inc(jj) - cos(ang(ii)) * tx(jj)], ...
num2str(rd(rng(ii,1) + inc(jj)*diff(rng(ii,:)),-2)), ...
'fontsize',8);
% flip the text alignment for lower axes
if ang(ii) >= pi
set(temp,'HorizontalAlignment','right')
end
end
% label each axis
temp = text([cos(ang(ii)) * 1.1 + sin(ang(ii)) * 0], ...
[sin(ang(ii)) * 1.1 - cos(ang(ii)) * 0], ...
char(lbl(ii,:)));
% flip the text alignment for right side axes
if ang(ii) > pi/2 && ang(ii) < 3*pi/2
set(temp,'HorizontalAlignment','right')
end
end
% plot the data
o = polar(ca,angw*ones(1,c),magw);
% set color of the lines
for ii = 1:c; set(o(ii),'color',col(ii,:),'linewidth',1.5); end
% apply the legend
temp = legend(o,leg,'location','best');
return
function [v] = rd(v,dec)
% quick round function (to specified decimal)
% function [v] = rd(v,dec)
%
% inputs 2 - 1 optional
% v number to round class real
% dec decimal loaction class integer
%
% outputs 1
% v result class real
%
% positive dec shifts rounding location to the right (larger number)
% negative dec shifts rounding location to the left (smaller number)
%
% michael arant
% Michelin Maericas Research and Development Corp
if nargin < 1; help rd; error('I/O error'); end
if nargin == 1; dec = 0; end
v = v / 10^dec;
v = round(v);
v = v * 10^dec;
function [val] = color_index(len)
% get unique colors for each item to plot
% function [val] = color_index(len)
%
% inputs 1
% len number of objects class integer
%
% outputs 1
% val color vector class real
%
% michael arant
if nargin < 1 || nargout < 1; help color_index; error('I / O error'); end
if len == 1
val = [0 0 0];
else
% initial color posibilities (no white)
% default color scale
col = [ 0 0 0
0 0 1
0 1 1
0 1 0
1 1 0
1 0 1
1 0 0];
% reduce if fewer than 6 items are needed (no interpolation needed)
switch len
case 1, col([2 3 4 5 6 7],:) = [];
case 2, col([2 3 4 5 6],:) = [];
case 3, col([3 4 5 6],:) = [];
case 4, col([3 5 6],:) = [];
case 5, col([5 6],:) = [];
case 6, col(6,:) = [];
end
% number of requested colors
val = zeros(len,3); val(:,3) = linspace(0,1,len)';
% interpolate to fill in colors
val(:,1) = interp1q(linspace(0,1,size(col,1))',col(:,1),val(:,3));
val(:,2) = interp1q(linspace(0,1,size(col,1))',col(:,2),val(:,3));
val(:,3) = interp1q(linspace(0,1,size(col,1))',col(:,3),val(:,3));
end
function [res] = isint(val)
% determines if value is an integer
% function [res] = isint(val)
%
% inputs 1
% val value to be checked class real
%
% outputs 1
% res result (1 is integer, 0 is not) class integer
%
% michael arant may 15, 2004
if nargin < 1; help isint; error('I / O error'); end
% numeric?
if ~isnumeric(val); error('Must be numeric'); end
% check for real number
if isreal(val) & isnumeric(val)
% check for integer
if round(val) == val
res = 1;
else
res = 0;
end
else
res = 0;
end

Assigning Values to the neighbors same value in MATLAB

I am having a small issue but I am clueless where I am at fault. Can someone please guide me the right way? Thanks in advance.
What I have done.
My codes finds local maxima’s.
Bring down from local maxima to a certain point.
Assign the neighbors that are greater than the downsized value, the value of downsized point.
Small Example
X = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];
Local maxima’s are 5, 7, 8, and 4
Go down to certain point. Like 4, 6, 7, 3.
Assign neighbors that have values greater than 4,6,7,3 same values.
Output will be like
X = [1 0 1 4 4 4 4 4 0 0 0 2 6 6 6 6 6 6 6 1 2 3 3 2];
As this was a simple example so did not have much difference when I applied my code on it.
I have provided another example with my code. Please have a look at that.
t = 50;
X = sin(2*pi*10*(1:t)/t) + 0.5*sin(2*pi*5*(1:50)/t) - linspace(0,3,50);
% plot(X)
A= X;
% A1 = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 6 6 6 6 6 6 6 6 6 1 2 3 4 2];
[pks,locs] = findpeaks(A);
idx = A(locs)-0.3 ; % will bring local maxima to the point where we want
for b = 1: numel(idx) % loop for checking idx
for c = 1:numel(A) % loop for checking neighbourhoods
if (A(locs(1,b)-c)> idx(1,b) && A(locs(1,b)+c)> idx(1,b))
else
d= c-1;
z(b)= d;
break
end
end
end
for i = 1:numel(idx)
for n = 1:z(1,i)
idx_1 = A(locs(i) - n); % will find what is in neighbourhood of local maxima
idx_2 = A(locs(i) + n);
% idx_1 = A(locs - n); % will find what is in neighbourhood of local maxima
% idx_2 = A(locs + n);
if (idx_1>idx(1,i) && (idx_2>idx(1,i))) % Check and assign the same value to neighbourhood of local maxima
A(locs(1,i)) = idx(i);
A(locs(1,i)-n) = idx(i);
A(locs(1,i)+n) = idx(i);
% A(i)) = A(idx(i))
else if (idx_1 < idx(1,i) && (idx_2>idx(1,i)))
A(locs(1,i)) = idx(i);
A(locs(1,i)+n) = idx(i);
else if (idx_1 > idx(1,i) && (idx_2<idx(1,i)))
A(locs(1,i)) = idx(i);
A(locs(1,i)-n) = idx(i);
else
A(locs(1,i)) = idx(i);
A(locs(1,i)-n) = idx_1(i);
A(locs(1,i)+n) = idx_1(i);
end
end
end
end
A(locs(1,i)) = idx(i);
end
figure(1)
hold on
plot(X,'r')
plot(A,'b')
hold off
Expected Output be like Black line in the picture not blue lines that are formed from code

Matching values from a matrix in a 3-D array

I am trying to match RGB values in an image.
% R G B
RGBset = [ 3 9 12;
4 8 13;
11 13 13;
8 3 2]
img(:,:,1) = [1 2 3
6 5 4
7 9 8
10 11 12];
img(:,:,2) = [3 4 8;
6 7 8;
11 10 9;
12 13 14];
img(:,:,3)= [3 7 2;
4 9 10;
5 11 12;
6 13 14]
In this image, only one RGB value matches from the RGBset which is [11,13,13], so the expected output is:
[0 0 0;
0 0 0;
0 0 0;
0 1 0]; % reshape(img(4,2,:),1,3) = [11, 13 13] is available in RGBset
% no other RGB value is present in the image
I have made this code but it is very slow for larger images.
matched= zeros(img(:,:,1));
for r=1:size(img(:,:,1),1)
for c=1:size(img(:,:,2),2)
matched(r,c)=ismember(reshape(img(r,c,:),1,3),RGBset,'rows');
end
end
What will be the solution with more speed?
We can reduce each RGB triplet into a scalar each and we will do this for both RGBset and img. This would reduce them to 2D and 1D matrices respectively. We are calling this as dimensionality-reduction. With this reduced data, we are achieving memory efficiency and that hopefully should lead to performance boost.
Thus, a solution with those bases covered would look something like this -
% Scaling array for dim reduction
s = [256^2, 256, 1].';
% Reduce dims for RGBset and img
RGBset1D = RGBset*s;
img1D = reshape(img,[],3)*s;
% Finally use find membership and reshape to 2D
out = reshape(ismember(img1D, RGBset1D), size(img,1), []);
Benchmarking for the vectorized solutions
Benchmarking code -
% R G B
RGBset = [ 3 9 12;
4 8 13;
11 13 13;
8 3 2]
% Setup inputs
img = randi(255, 2000, 2000, 3);
img(3,2,:) = RGBset(4,:);
% Luis's soln
disp('--------------------- Reshape + Permute ------------------')
tic
img2 = reshape(permute(img, [3 1 2]), 3, []).';
matched = ismember(img2, RGBset, 'rows');
matched = reshape(matched, size(img,1), []);
toc
% Proposed in this post
disp('--------------------- Dim reduction ------------------')
tic
s = [256^2, 256, 1].';
RGBset1D = RGBset*s;
img1D = reshape(img,[],3)*s;
out = reshape(ismember(img1D, RGBset1D), size(img,1), []);
toc
Benchmarking output -
--------------------- Reshape + Permute ------------------
Elapsed time is 3.101870 seconds.
--------------------- Dim reduction ------------------
Elapsed time is 0.031589 seconds.
You can replace the loops by permute and reshape:
img2 = reshape(permute(img, [3 1 2]), 3, []).';
matched = ismember(img2, RGBset, 'rows');
matched = reshape(matched, size(img,1), []);
This creates a 3-column matrix img2 in which each row corresponds to one pixel from img. That way ismember(..., 'rows') can be applied in a vectorized manner. The obtained result is then reshaped as needed.
You can just loop over the colours, which is much quicker than looping over each pixel.
% Set up your colours into the 3rd dimension, so they match along the same axis
RGB3D = reshape(RGBset,[],1,3);
% Loop over them
for ii = 1:size(RGB3D, 1)
% See if all 3 members of the colour match any pixel
matched = all(ismember(img, RGB3D(ii,:,:)),3)
if any(matched)
disp(matched)
disp(['matched color: ' num2str(ii)]);
% do something else with the matched pixels
end
end

How to change bar graph style

I'm trying to create a bar graph, and set every bar a different style (lines, dots, circles and ext...).
In this example:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
bar(x,y);
All 3 bars have the same style.
How can I change it and set 3 differents styles for the 3 bars ?
Use a handle output when calling bar
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
That gives an array h of bar objects (of length 3 in your example), and you can set their aspect independently. For example,
set(h(1), 'EdgeColor', 'r');
set(h(2), 'EdgeColor', 'g');
set(h(3), 'EdgeColor', 'b');
gives the following graph in R2015b (the aspect will vary in other versions).
Other properties you can change are 'BarWidth', 'LineStyle', etc. To see the list type get(h(1)).
I am adding some style changes to what #Luis given
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
set(h(1),'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
set(h(2),'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
set(h(3),'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
'FaceColor', 'w' -- Makes the color of bar white
'LineWidth', 1 -- Width of the border line of bar
'LineStyle',':' -- Dotted line
'LineStyle','--' -- Dashed line
'LineStyle','-.' -- Dash-dot line
For different style for each bar set
figure
hold on;
y = [2 2 3; 0 0 0; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z=bar(x,y);
ylim([0 15]);
set(z,'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
y = [0 0 0; 2 5 6; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z1=bar(x,y);
set(z1,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
y = [0 0 0;0 0 0; 2 8 9; 0 0 0];
x = [10; 20; 50; 90];
z2=bar(x,y);
set(z2,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-');
y = [0 0 0;0 0 0; 0 0 0; 2 11 12];
x = [10; 20; 50; 90];
z4=bar(x,y);
set(z4,'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
hold off;

Remove internal edges in 3D MATLAB plot using patch function

I am plotting a 3D object, say a cube, in MATLAB.
Node = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];
Elem = cell(1); Elem{1} = 1:8;
figure
for elm = 1:size(Elem,1)
X = Node(Elem{elm},:); K = convhulln(X); hold on;
patch('Faces',K,'Vertices',X,'FaceColor',rand(1,3),'FaceAlpha',1.0);
end
view(3); grid off; axis equal; cameramenu; axis off;
In the plot, how do I remove the internal diagonal lines? The plot should just show edges of cube. I am looking for a general solution which is applicable to any polyhedron.
the output of K=convhulln(X); is causing this, because convex hull will have triangular facets... (that's the default).
if instead you'd define K to be:
K= [1 2 3 4; ...
2 6 7 3; ...
4 3 7 8; ...
1 5 8 4; ...
1 2 6 5; ...
5 6 7 8];
You'll get it right.
Another option is to use geom3D from the FEX.