Error while using 'patch' function in matlab - matlab

I am trying to plot quadrilateral patches of a surface mesh of a 3d object.. I have x,y,z which are 4Xn where 4 is for each vertex of the quadrilateral and n is the number of quadrilaterals,c which is a 3Xn RGB matrix. when use patch( x, y, z,c);
I get the error
`Error using patch`
`Size of C must match sizes of X Y [Z]`
But this works for x,y,z which are 3Xn for triangular patches and c a 3Xn RGB vector. Please correct where i am going wrong.

Instead of giving a single RGB value for each patch, you'd need to give patch a value for each vertex you are drawing (due to the format of your data).
So with your data and this line:
cQuad=permute(repmat(cQuad,[1 1 4]),[3 2 1]);
patch(vertQuad.x,vertQuad.y,vertQuad.z,cQuad)
Works fine. What I do is repeat the colours 4 times using repmat and then order the dimensions in the format patch likes using permute

Related

Matlab Surface Plot

I have a set of data points, x, y, and z contained in a matrix, record.
In record, each row is a data-point where the first value is the x-coordinate, the second is the y-coordinate, and the third is the z-coordinate. I would like to represent this as a surface plot. I tried:
surf([record(:,1), record(:,2)], record(:,3))
But the results were not what I expected. Any advice?
Try this code for instance.
[x,y,z]=sphere(n);
surf(x,y,z);
axis equal
This code plots with 3 parameters surf the surface of a sphere. As far as I understood from your code you want to utilize the 2 parameters surf for your application.
According to surf help when utilizing 2 parameters surf:
surf(Z) and surf(Z,C) use x = 1:n and y = 1:m. In this case,
the height, Z, is a single-valued function, defined over a
geometrically rectangular grid.
where:
The color scaling is determined
by the range of C
It just doesn't look like you want to utilize C as the color scaling parameter. For better understanding, can you send the contents of record for reference?

Matlab - plot image gradients using vector representation

I have computed the gradients from every pixel location of a grayscale image, both on X and Y axis and this can result in a vector representation for each pixel location. I want to obtain a plot figure similar to the one illustrated bellow:
My image has 1000 x 1002 dimensions and I have computed the gradients for each pixel on X and Y directions so I have 2 matrices, each one having 1000 x 1002 dimensions.
I am interested in obtaining a plot similar to the one illustrated in the image above, where I show basically the direction of each vector obtained from the computed gradients. I do not care about the magnitude of the vector, so basically each arrow can have the same length.
Do you know how can I obtain something similar to this?
It works in my case:
[DX,DY] = imgradient(imageIn);
%show gradient
figure;
[x,y]=meshgrid(1:1:500);
figure
quiver(x,y,DX,DY)
hold off

MATLAB 3D volume visualization

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

Simplest way to draw filled triangles in matlab

I have a set of triangles in the standard format used in matlab, i.e a 3XN list of indinces, and two vectors of length N, called X and Y for the x and y coordinates.
I have a scalar value associated with each triangle, in a vector C. I want to draw filled triangles where the color of each tri is determined by the vector C. I know I can use patch, but that would mean I need to iterate over all tris and call patch for each one, right?
Is there a better way?
Actually if you check the documentation for patch you'll notice it says
Create one or more filled polygons.
It also says
If X and Y are m-by-n matrices, MATLAB draws n polygons with m vertices.
C determines the color of the patch.
patch may actually be exactly what you want. You just need to put the x and y coordinates into 3xN matrices.

3d plot with given 2d data

I want to understand how the 2d data is related to z axis to get the 3d plots
let us say that i have x=[-1:0.1:1], vector
and y=[1 2 3 4 5 4 3 2 1 0]
a plot of y Vs x will have peak of 5 and slope down to both sides at x=0.5
how to relate these data in 3d to get the bell shape surface, with similar characteristics.
You can view a line/curve plot as a function of a single variable, y=f(x), and typically, x and y are both vectors. For e.g., you can plot the Gaussian bell curve as
x=linspace(-3,3,1000);
y=exp(-x.^2/2);
plot(x,y)
A surface plot, on the other hand, is a function of two variables, z=f(x,y) where x and y can be either vectors or matrices and z is a matrix. meshgrid is a very handy function that generates 2D x and y arrays from 1D vectors by proper replication.
It is the z matrix that you plot either as a 2D image (values of z are represented by colors) or a 3D plot (values of z are represented as heights along the z-axis). For e.g., a 3D Gaussian bell curve can be plotted as
x=linspace(-3,3,1000);y=x'; %'
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2)/2);
surf(x,y,z);shading interp
This is how the respective plots should look like