Plotting a 3d matrix in slices - MATLAB - matlab

I would like to plot each slice of my 3d matrix to show differences across the third dimension. However I can only manage to plot them besides each other, and I would like a 3d plot where it is clear that the slices of the matrix are in fact stacked. My code for two layers so far is
visualmatrix=zeros(10);
visualmatrix(1:5,1:5)=1;
visualmatrix2=zeros(10);
visualmatrix2(1:8,1:8)=1;
subplot(1,2,1)
[r,c] = size(visualmatrix); %# Get the matrix size
imagesc((1:c)+0.5,(1:r)+0.5,-visualmatrix); %# Plot the image
colormap(gray); %# Use a gray colormap
axis equal %# Make axes grid sizes equal
set(gca,'XTick',1:(c+1),'YTick',1:(r+1),... %# Change some axes properties
'XLim',[1 c+1],'YLim',[1 r+1],...
'GridLineStyle','-','XGrid','on','YGrid','on');
subplot(1,2,2)
[r,c] = size(visualmatrix2); %# Get the matrix size
imagesc((1:c)+0.5,(1:r)+0.5,-visualmatrix2); %# Plot the image
colormap(gray); %# Use a gray colormap
axis equal %# Make axes grid sizes equal
set(gca,'XTick',1:(c+1),'YTick',1:(r+1),... %# Change some axes properties
'XLim',[1 c+1],'YLim',[1 r+1],...
'GridLineStyle','-','XGrid','on','YGrid','on');
colorbar
colorbar('Ticks',[-1,0],...
'TickLabels',{'Equal','Different'})
suptitle('Illustration of the concept')
Which leads to the following image
Is there a simple way to make it visualize this in a 3d plot with i.e. 5 layers?
Thank you in advance.

There is a nice function for that in Matlab.
It is called slice.
It plots things like:

Related

MATLAB- Plotting points to 3D vector plot

I currently have this plot:
The goal is to get a plot like this:
I plotted the vectors using
for idx = 1:size(starts,1)
q=quiver3(starts(idx,1), starts(idx,2), starts(idx,3), ends(idx,1), ends(idx,2), ends(idx,3), ...
'Color', RGBofStainVector(idx,:));
hold on
end
I have a 12x3 matrix with X,Y,Z points and would want to plot them in the same graph.
How can I add the different 3D points with their respective color as seen in picture 2? I tried using plot3 and scatter3 functions but had no luck.

Missing row/column when plotting a surface in MATLAB

Have a look at the following MATLAB code and the resulting surface plot. Maybe I am doing a stupid mistake, but there is actually a row and column missing. The variable z is a 10x10 matrix, but the plot shows only 9x9 elements. How to plot the whole 10x10 matrix?
z = randn(10,10);
t = 1:10;
x = 1:10;
figure;
surf(t,x,abs(z),'EdgeColor','none');
axis xy; axis tight; colormap(jet); view(0,90);
I think this is a misunderstanding about what surf does, i.e. what a surface plot is:
What you seem to be wanting is an actual image instead of a surface plot, where for the former pixels correspond to the underlying values. What you get with surf is a graphical representation of lines at a certain height (abs(z) in your case), i.e. between your desired image pixels. Note that there are 10x10 lines in your 9x9 plot.
What you want can be achieved visually e.g. by:
z = randn(10,10);
t = 1:10;
x = 1:10;
figure
imshow(abs(z),[]),
axis on, colormap(gca,jet)
colorbar
hope this helps!

Matlab - Layers of individual plots

I work with MATLAB.
I have four matrices of size 10x3, and I need to plot them by using the area plot into three different layers, i.e. something similar to the contourslice example (https://uk.mathworks.com/help/matlab/ref/contourslice.html) where on the x-axis and y-axis I would have the 2-d area plot and on the z-axis the four matrices.
For example, I have the following three matrices with an area plot for each individual variable:
A=ones(10,3);
B=2*ones(10,3);
C=5*ones(10,3);
D=10*ones(10,3);
figure(); area(A,'DisplayName','HS')
figure(); area(B,'DisplayName','HS')
figure(); area(C,'DisplayName','HS')
figure(); area(D,'DisplayName','HS')
How can I layer there three area plots into one plot, as shown by this picture for example?
Thank you, Mat

MATLAB - Plotting a smooth volume from 3D scatter plot data

So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.

Need help in 3D plot using MATLAB (X,Y,Z,V)

I need to ask help in plotting a 3D volume in MATLAB. My data set includes the X, Y, Z coordinate and corresponding intensity value, V.
I was using pcolor (X,Y,V) and shading interp while i was doing the 2D images but then I got stuck when I am about to create the 3D images. I have tried scatter3, smooth3 and slice but it seems it does not fit the function I need.
My goal is to plot the 3D grid with the corresponding intensity value per coordinate and apply shading interp between these points.
I am really new in 3D plotting and I would really appreciate any help in achieving my goal. Thank you so much!
Here are some example of images that I am trying to create
(source: ndt.net)
(source: www.bam.de)
I have a solution for your first example, in which you show three cross-sections of the volume data. With this solution you can essentially plot several pcolor with different orientations, in one same 3D figure.
First, you need the data for the different slices. This is exactly what you had when you did pcolor(X,Y,V) to plot the cross section in 2D, where X, Y and V are two dimensional matrices. You will also need to create a matrix Z with the z-position of the slice (e.g. Z = zeros(size(X)) + z0). Then you use the command surface(X,Y,Z,V) to plot the cross section image in 3D, where X,Y,Z are the positions, and V is the color (value of the function).
Repeat this procedure for all the other slices, using appropriate X,Y,Z,V matrices for each slice. For slices oriented on other axes you will have to define X,Y,Z accordingly. Remember to use "hold on" after the first surface command, so that all the slices are plotted.
Example:
surface(X1,Y1,Z1,V1); shading interp; hold on
surface(X2,Y2,Z2,V2); shading interp;
surface(X3,Y3,Z3,V3); shading interp;
colormap(jet(2048)); caxis([0,3]); % color axis colormap and scaling
axis equal; xlabel('X'); ylabel('Y'); zlabel('Z') % X,Y,Z scaling and label
Result figure here: