> > I am simulation a solid object that is contained within a 3D matrix.
> > The shape of the object is defined by a logical array, while I would
> > typically want to plot the temparature contained in a second matrix of
> > the same shape.
>
> > As an example, suppose you I have the data defined by
> > [X,Y,Z] = meshgrid(10:10);
> > R = sqrt(X.^2+Y.^2+Z.^2);
> > inside = R < 8;
> > C = rand(size(X));
> > In this case I would want to plot a solid sphere defined by inside,
> > built up of tiny cubes that each have a color given by C.
On Sep 22, 8:40 am, "Gwendolyn Fischer"
<gwendolyn.fisc...@nowhere.com> wrote:
> Take a look at isosurface, it might not be exactly what you where looking for, but it comes quite close and is ready to use:
> isosurface(X,Y,Z,R,8,C)
Thanks, that seems to give a result in the given example of a perfect
square. I am, however, interested in the more general case of a
complex shape described by a logical matrix as I described before and
I don't think that isosurface would work then. I ended up writing my
own function, which was easier than I thought using patch. I am
posting it below in the hope that someone finds it useful. To plot the
example given above use:
x=10.5:1:10.5; y=x; z=x; plotCubes(x,y,z,inside,C)
Bas
function plotCubes(x,y,z,V,C)
%function plotCubes(x,y,z,V,C)
%makes 3D plot of shape defined by boolean 3dmatrix V, with the
color
%the small cubes given by the similar shaped matrix C
%x, y and z are vectors indicating the edge locations, so their
lengths
%should be one larger than the corresponding dimension of the matrices
[nx,ny,nz] = size(V);
if length(x) ~= nx + 1  length(y) ~= ny + 1  length(z) ~= nz + 1
error('Length of x, y and z must be one larger than size of V and
C')
end
dc = 0.02 * (max(C(V))  min(C(V))); %small color difference to create
3D effect
%X
ilin = find(cat(1, V(1,:,:), ~V(1:end1,:,:) & V(2:end,:,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = repmat(x(ix), 4, 1);
py = [y(iy); y(iy); y(iy+1); y(iy+1)];
pz = [z(iz); z(iz+1); z(iz+1); z(iz)];
c = C(ilin);
%Y
ilin = find(cat(2, V(:,1,:), ~V(:,1:end1,:) & V(:,2:end,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, repmat(y(iy), 4, 1)];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin) + dc];
%Z
ilin = find(cat(3, V(:,:,1), ~V(:,:,1:end1) & V(:,:,2:end)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, [y(iy); y(iy+1); y(iy+1); y(iy)]];
pz = [pz, repmat(z(iz), 4, 1)];
c = [c, C(ilin)  dc];
%+X
ilin = find(cat(1, V(1:end1,:,:) & ~V(2:end,:,:), V(end,:,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, repmat(x(ix+1), 4, 1)];
py = [py, [y(iy); y(iy); y(iy+1); y(iy+1)]];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin)];
%+Y
ilin = find(cat(2, V(:,1:end1,:) & ~V(:,2:end,:), V(:,end,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, repmat(y(iy+1), 4, 1)];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin) + dc];
%+Z
ilin = find(cat(3, V(:,:,1:end1) & ~V(:,:,2:end), V(:,:,end)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, [y(iy); y(iy+1); y(iy+1); y(iy)]];
pz = [pz, repmat(z(iz+1), 4, 1)];
c = [c, C(ilin)  dc];
figure;
patch(px,py,pz,c)
axis equal vis3d
shading flat; %dc can be set to zero when using faceted
