How to plot a 3D cube or a horizontal slice from the following 3D data?

I have a 3D data (unable to attach here because of size limits) with dimension e.g., far_trc = 139 * 48 * 176.
139 are the number vertical layers
48 are the number of lines or verical traces
176 are the numbers of traces in each line (each of 48 lines have 176 traces).
How can I plot in matlab as a 3D cube just like below? and If I want take a small cube out from big cube how can I do it?

2 Comments

Handling voxels is tricky and there is specialised software or matlab toolboxes. There are a few file-exchange submissions that are very nice, but none can carve out a cube automatically.
As for just plotting though, without transparencies or slicing, if you have enough resolution (it sounds like you do, otherwise in case you could interpolate, but that's frown upon), you could just use scatter3
[xx, yy, zz] = meshgrid(1:100);
vv = cos(xx).*sin(yy).*abs(asin(zz)).^2;
scatter3(xx(:),yy(:),zz(:),5,vv(:));
colormap(jet);
colorbar;
You could remove the cube yourself, though it's not straightforward
vv(1:10,1:10,end-10:end) = nan;
scatter3(xx(:),yy(:),zz(:),5,vv(:));
colormap(jet);
@MarKf I am getting error with my data. How I can do with following dimensions:
176 - y axis
139 - x axis
48 - z axis

Sign in to comment.

 Accepted Answer

Alright let's write an actual answer with isocaps and patch then, the suggestion to use scatter3 in the comment above is valid but lazy. This is how fieldtrip and the other toolboxes mentioned above do it, tho simplified.
sizes = [139,176,48]; % matlab online has no issue handling 139x176x48 mats so you might as well have uploaded your data
trc = rand(sizes); % this is an example 3D data matrix like the one you have
fvc = isocaps(trc,1e-4); % meshgrid(1:sizes) by default, not like you care about coordinates
patch(fvc,'FaceColor','interp','EdgeColor','none') %interp
colormap(jet)
colorbar;
view(-40,30)
daspect([1 1 1])
colormap(jet)
camlight(20,40) %your example image has lighting
camlight(-20,10) % without, you can't see the edges as nicely
lighting gouraud %especially with the sliced cube (also look at light options or material in case)
Carving out the the cube ourselves as above.
trc (1:20,1:20,end-20:end) = nan;
figure
fvc = isocaps(trc,1e-4);
patch(fvc,'FaceColor','interp','EdgeColor','none')
colormap(jet)
view(-40,30)
daspect([1 1 1])
colormap(jet)
camlight(10,20)
camlight(60,120)
lighting gouraud
Consider accepting the answer if that's what you were looking for or if it helped.

6 Comments

Oh you did upload your data. It's actually 139x48x176. You can shift those dimensions around if you want.
It's also mostly (values close to) 0s, so you might want to avoid plotting those just to have a green rectangle masking most of them. Or you might need to normalize your data (there is a "bacon strip" of abs high values that renders all the other values comparably noise).
Well then.
DAT = importdata("DAT.mat");
minDAT = min(min(min(DAT)));
fvc = isocaps(abs(DAT),1e-4); % first example without green rectangle of low values
patch(fvc,'FaceColor','interp','EdgeColor','none') %interp
colormap(jet)
colorbar;
view(-40,30)
daspect([1 1 1])
colormap(jet)
% camlight(20,40)
%careved out rectangle of 0s
DAT (1:20,1:20,end-20:end) = nan;
figure
fvc = isocaps(DAT,minDAT);
patch(fvc,'FaceColor','interp','EdgeColor','none')
colormap(jet)
colorbar;
view(-40,30)
daspect([1 1 1])
colormap(jet)
camlight(10,20)
camlight(60,120)
lighting gouraud
@MarKf thanks, how to invert x and y axis, I mean 0 at top and 200 bottom in y axis, on x axis, 0 on left side.
Thank you for accepting the answer.
If you want to change your data, you can rearrange dimensions around (making x into y or z or whatever, like tipping your standing rectancle into the lying one of my first answer) look into permute. If you want to reverse the data, you can use flip.
If instead you mean you just want to change the viewpoint of the plot, you can change it with view (I used view(-40,30) similar to your example image). You can also rotate it manually with rotate3d or enable rotate mode in the ui image controls (then take a note of the azimuth and elevation angles that appear on the plot on the bottom left to plug them into view(az,el) in case) If you want to move the axes around, look into 'XAxisLocation'/'XRuler'/'XAxis' properties of the axes.
@MarKf When I am using following code fvc = isocaps(trc,1e-4); the problem is arises with 1e-4, for example, if my data ranges 0 - 1, if I select 1e-4 as zero it will not display the part data having zero values, how to solve it
fvc = isocaps(trc,1e-4); % meshgrid(1:sizes) by default, not like you care about coordinates
patch(fvc,'FaceColor','interp','EdgeColor','none') %interp
colormap(jet)
colorbar;
Did you notice the difference in the latest code above, between the figure with and the one without the rectangle of zeros?
minDAT = min(min(min(DAT)));
fvc = isocaps(abs(DAT),1e-4); % first example without green rectangle of low values
fvc = isocaps(DAT,minDAT); % 2nd example with green rectangle of low values (actually 0s)
with minDAT obtained as the lowest value of all (min(DAT(:)) actually below 0, negative numbers accounted as well, unlike abs(DAT) which makes them positive). The isovalue (the 1e-4 or minDAT) determines the threshold above which the surfaced is computed, ie which elements are taken into account to create it.
Do look into those functions to learn their behaviour and also carefully at each step, it'll help, make you a better coder and is generally faster than asking.

Sign in to comment.

More Answers (0)

Asked:

on 2 Mar 2024

Commented:

on 12 Mar 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!