How to plot a 3D cube or a horizontal slice from the following 3D data?
11 views (last 30 days)
Show older comments
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
MarKf
on 2 Mar 2024
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);
Accepted Answer
MarKf
on 3 Mar 2024
Edited: MarKf
on 3 Mar 2024
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
MarKf
on 12 Mar 2024
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.
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!