MATLAB 3D volume visualization - matlab

I have a matrix M, 135*191*121 double and want to plot it in 3D volume by using those 121 slices. How can I do this? (I need a grayscale image)
Regards

Check out vol3d v2 , it an update to Joe Conti's vol3d function, allowing voxel colors and alpha values to be defined explicitly. In cases where voxels can be any RGB color, use:
vol3d('CData', cdata);
where cdata is an MxNxPx3 array, with RGB color along the 4th dimension. In cases where color and alpha values are highly independent, specify an MxNxP alphamatte as follows:
vol3d('CData', cdata, 'Alpha', alpha);

if you have 3 arrays, storing (x,y,z) coordinates of every point that you need to plot, then you can use function plot3
From matlab help
PLOT3 Plot lines and points in 3-D space.
PLOT3() is a three-dimensional analogue of PLOT().
PLOT3(x,y,z), where x, y and z are three vectors of the same length,
plots a line in 3-space through the points whose coordinates are the
elements of x, y and z.
PLOT3(X,Y,Z), where X, Y and Z are three matrices of the same size,
plots several lines obtained from the columns of X, Y and Z.
Various line types, plot symbols and colors may be obtained with
PLOT3(X,Y,Z,s) where s is a 1, 2 or 3 character string made from
the characters listed under the PLOT command.
PLOT3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...) combines the plots
defined by the (x,y,z,s) fourtuples, where the x's, y's and z's are
vectors or matrices and the s's are strings.
Example: A helix:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
PLOT3 returns a column vector of handles to lineseries objects, one
handle per line. The X,Y,Z triples, or X,Y,Z,S quads, can be
followed by parameter/value pairs to specify additional
properties of the lines.
for 3d plots you may also want to look into surf function

Related

How to make a 2 dimensional colormap for scatter point plot in matlab?

I'm trying to use scatter3 to plot n points with color encoded in Matlab. Each point has four parameters, x, y, z, intensity. Now I want to build a 2-dimensional color map which determines the color of each point by two parameters separately, z, and intensity. How should I make it?

Scatter plot with different size for each point

I have several thousands of points to plot (about 10k) and I would like to plot them with Matlab but deciding a diferent size for each of the points (and a different color if possible). I tried to make a scatter plot for each point, but it is extremely slow compared to a single scatter call for all the points. Is there a way to plot several points in Matlab with different properties for each point, that works in a reasonable amount of time?
In case it is not possible to do it with Matlab, is there a way to do it with gnuplot?
scatter(x, y, a, c) takes arguments x and y, and then a for size, and c for colour. a can either be a single scalar, or a vector with a size for each (x,y) point. c can be an RGB triplet, or a vector, the same size as x and y. For example:
x = 1:4;
scatter(x, x, 10*x, x);
results in
So in your case, perhaps
scatter(xData, yData, [], 1:10000)
will result in your data having a different colour determined by its position in the data array.
For gnuplot it's easy, suppose you write your datafile with 3 columns, all you have to do is
plot 'data.dat' u 1:2:3:3 with circles lc palette
HERE you can find some examples (for help type help circles).
If you want just what is called variable pointsize (pointsize is not related to the real axis) you can use:
plot 'data.dat' with points ps variable pt 7
HERE you can find some examples (for help type help pointsize).
For gnuplot you can combine pointsize variable and linecolor variable or linecolor palette:
set xrange [0:10]
set samples 21
plot '+' using 1:1:(0.2*$1):1 with point pointsize variable linecolor palette pt 7 notitle

MeshGrid for Triangle Elements

I want to build a contourf plot of a certain aspect in my Plate. The plate is divided in triangle elements, which I have the coordinates (x,y) of each knot of the triangle.
So, How can I make a meshgrid for my knots so I can make my contourf plot?? I have the coordinates of everything and have the value of my function Z in each knot. (I'm a beginner in Matlab, sorry for this "basic" question)
If your goal is just to visualise the triangles then there is another way that's probably simpler and more robust (see the end of this post).
If you definitely need to generate contours then you will need to interpolate your triangular mesh over a grid. You can use the scatteredInterpolant class for this (documentation here). It takes the X and Y arguments or your triangular vertices (knots), as well as the Z values for each one and creates a 'function' that you can use to evaluate other points. Then you create a grid, interpolate your triangular mesh over the grid and you can use the results for the countour plot.
The inputs to the scatteredInterpolanthave to be linear column vectors, so you will probably need to reshape them using the(:)`notation.
So let's assume you have triangular data like this
X = [1 4; 8 9];
Y = [2 3; 4 5];
Z = [0.3 42; 16 8];
you would work out the upper and lower limits of your range first
xlimits = minmax(X(:));
ylimits = minmax(Y(:));
where the (:) notation serves to line up all the elements of X in a single column.
Then you can create a meshgrid that spans that range. You need to decide how fine that grid should be.
spacing = 1;
xqlinear = xlimits(1):spacing:xlimits(2);
yqlinear = ylimits(1):spacing:ylimits(2);
where linspace makes a vector of values starting at the first one (xlimits(1)) and ending at the third one (xlimits(2)) and separated by spacing. Experiment with this and look at the results, you'll see how it works.
These two vectors specify the grid positions in each dimension. To make an actual meshgrid-style grid you then call meshgrid on them
[XQ, YQ] = meshgrid(xqlinear, yqlinear);
this will produce two matrices of points. XQ holds the x-coordinates of every points in the grid, arranged in the same grid. YQ holds the y-coordinates. The two need to go together. Again experiment with this and look at the results, you'll see how it works.
Then you can put them all together into the interpolation:
F = scatteredInterpolant(X(:), Y(:), Z(:));
ZQ = F(XQ, YQ);
to get the interpolated values ZQ at each of your grid points. You can then send those data to contourf
contourf(XQ, YQ, ZQ);
If the contour is too blocky you will probably need to make the spacing value smaller, which will create more points in your interpolant. If you have lots of data this might cause memory issues, so be aware of that.
If your goal is just to view the triangular mesh then you might find trimesh does what you want or, depending on how your data is already represented, scatter. These will both produce 3D plots with wireframes or point clouds though so if you need contours the interpolation is the way to go.

Contour plot using matlab fails

I am trying to make a contour plot using the following matlab code:
x=linspace(-10,10);
y=linspace(-10,10);
[X,Y]=meshgrid(x,y);
Z=X.^3-Y.^3;
figure
[c,h]=contour(X,Y,Z,[3]);
clabel(c,h)
This gives me the wrong picture actually:
I really don't understand what goes wrong here, because when I do [c,h]=contour(X,Y,Z,[3 0]) for example, it does give me the correct contour plots for the levels 3 and 0, I need help.
If you only give a single number to contour there, it interprets it as the number of contour lines you want and picks the levels automatically. From the docs:
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k]). Specifying the vector v sets the LevelListMode property to manual.
e.g. to get a single contour at "3", you need to do it this way instead:
figure
[c,h]=contour(X,Y,Z,[3,3]);
clabel(c,h)
The fourth argument of contour can be two things.
If it is an array of numbers (more than 1) then its the contour value you want to show. Else, if its a single value, its the amount of contour lines you want to show.
Example:
x=linspace(-10,10);
y=linspace(-10,10);
[X,Y]=meshgrid(x,y);
Z=X.^3-Y.^3;
figure
subplot(121)
[c,h]=contour(X,Y,Z,[10]);
clabel(c,h)
subplot(122)
[c,h]=contour(X,Y,Z,[1000 -1000 50 -70 3 0]);
clabel(c,h)

Using different colors in Matlab

In a 3D scatter graph of MATLAB I have 15 different clusters of data that I want to highlight. I can see MATLAB has 8 specific colors. Is there any other way I could use 7 more colors just to distinguish the clusters?
Thanks
I would recommend to use this File Exchange submission - Generate maximally perceptually-distinct colors
It allows you to create a colormap with very distinguished colors and apply them with COLORMAP function. See help for this submission for more options.
colors = distinguishable_colors(n_colors);
For 3D scatter you can use this colors as an argument (C) in SCATTER3:
scatter3(X,Y,Z,[],colors)
To use this colors for different lines set them as default color order either for current figure:
set(gcf,'DefaultAxesColorOrder',colors)
or all figures:
set(0,'DefaultAxesColorOrder',colors
You can use the color property using set. You must first get a handle h to the drawing objects and set(h,'color',[0.2 0.3 0.9]). The color is rgb ranging from 0 to 1 for each channel.
From the Matlab documentation:
scatter(X,Y,S,C) displays colored circles at the locations specified
by the vectors X and Y (which must be the same size).
S determines the area of each marker (specified in points^2). S can be
a vector the same length as X and Y or a scalar. If S is a scalar,
MATLAB draws all the markers the same size. If S is empty, the default
size is used.
C determines the color of each marker. When C is a vector the same
length as X and Y, the values in C are linearly mapped to the colors
in the current colormap. When C is a 1-by-3 matrix, it specifies the
colors of the markers as RGB values. If you have 3 points in the
scatter plot and wish to have the colors be indices into the colormap,
C should be a 3-by-1 matrix. C can also be a color string (see
ColorSpec for a list of color string specifiers).
So, for example, say that your clusters are given by the columns of the matrices X and Y, with the k'th column being the k'th cluster, X being the X coordinates, and Y being the Y coordinates. We can do what you want as follows:
% make some random data in clusters:
n = 15;
m = 42;
X = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
Y = 0.2*rand(m,n) + repmat(rand(1,n),m,1);
% lets change the colour map:
colormap(jet);
% now plot each, one at a time, and each with a different colour:
hold on;
for k=1:n
scatter(X(:,k),Y(:,k),40,k/n*ones(m,1));
end
If you don't like these colours, you can change the colormap, and if you don't like the color maps, you can, as the other answer points out, insert any RGB values you want.