Data visualization - surface with parametric colour - matlab

I have a set of points in 3D in cartesian coordinates X,Y,Z. To every such point I associate a value which is stored in a vector C. I want to be able to colour the surface given by X,Y,Z with colors given by C. Alternatively, if possible, I would like to associate to each point a color from a finite number of given colors. In Matlab this is possible with surf(X,Y,Z,C), but X,Y must be in grid form (generated by meshgrid), not in general form, like in my case.
I managed to do this in the case of the sphere, but the procedure is not pretty, and it uses heavily the parametrization of the sphere. Here's an example of what I want to do (in the case of the sphere).
Is there a way to do this type of surface coloring in Matlab? (if it helps, I can also provide a triangulation of the surfaces in addition to the points X,Y,Z)
Is there another piece of software which can do similar things and can interface in some way with Matlab?

I based this off of Matlab's Representing Data as a Surface. Does this work?
xlin = linspace(min(x),max(x),33);
ylin = linspace(min(y),max(y),33);
[X,Y] = meshgrid(xlin,ylin);
f = scatteredInterpolant(x,y,z);
Z = f(X,Y);
g = scatteredInterpolant(x,y,c);
C = g(X,Y);
surf(X, Y, Z, C)

Thanks Ander Biguri for suggesting patch. Here's what I've come up with, and it seems to work fine.
function patch_color_plot(struc)
% struc is a structure containing at least the following:
% points - the coordinate of the points in a 3xN matrix
% t - the triangulation matrix
% x_0s - the point values
fac = struc.t;
vert = struc.points;
fvc = struc.x_0s;
p = patch('Faces',fac,'Vertices',vert,'FaceVertexCData',...
fvc,'FaceColor','interp');
set(p,'EdgeColor','none')
axis equal
axis off`
Here's a sample result:
and another one with a torus:
here's another example:

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?

draw an ellipse or contour in matlab

I have two vectors a = [1 1]' and b = [1 -1]' which are linearly independent.
I want to draw a shape like an ellipse or a contour around these points, so I can see the area which is spanned by these two vectors.
The picture below shows what I want to get. One of the blue vectors belongs to a and one of the red to b (I drew the mirrored vectors also for demonstration purpose). The green circle is what I want to draw.
How can I do that?
From your question and your example it seems to me that you don't have an entirely clear idea what you are trying to do. In order for MATLAB to plot anything, it will need at least a collection of points or an equation according to which this ellipse you want can be plotted.
The cleanest way to do so in this case would be to find the equations defining the ellipsoid. Using the information seen here, we see that we can describe any 2D-ellipse centered on the origin by the following equations:
x = a*cos(t)
y = b*sin(t)
The question then is: what should the values for our parameters a and b be? Since we have two points given that should satisfy these equations: [1,1] and [1,-1] we can deduce that a and b are equal to the square root of 2. Then we can plot the contour:
syms t % our equation's parameter is t
a = sqrt(2); b = sqrt(2);
x = a*cos(t);
y = b*sin(t);
ezplot(x,y) % plot the symbolic equation

Plotting characteristics of antenna radiation in matlab

I need to plot this function
theta = (-pi:0.01:pi);
f = 3*10^9;
c = 299792458;
da = 2;
Here's my code, but I'm not sure it's correct. I dont know where exactly dot mark should be. How to set X-Axis in degress?
beta = (2*pi*f)/c;
const= (da*beta)/2;
j= (cos(theta)+1).*(besselj(1,const*sin(theta))./(const*sin(theta)));
My another question is how to plot this function in polar coordinates.
I made something like this.
polar(theta,j);
Is it possible to rotate that function(by y-axis) to get 3D plot?
Things are quite right to me, although I wouldn't use the symbol j as a variable because (as i does) it is the symbol for the imaginary unit (sqrt(-1)). Doing so you are overriding it, thus things will work until you don't need complex numbers.
You should use element-wise operations such as (.*) when you aim at combining arrays entries element by element, as you correctly did to obtain F(\theta). In fact, cos(theta) is the array of the cosines of the angles contained in theta and so on.
Finally, you can rotate the plot using the command Rotate 3D in the plot window. Nonetheless, you have a 2D curve (F(\theta)) therefore, you will keep on rotating a 2D graph obtaining some kind of perspective view of it, nothing more. To obtain genuine information you need an additional dependent variable (Or I misunderstood your question?).
EDIT: Now I see your point, you want the Surface of revolution around some axis, which I suppose by virtue of the symmetry therein to be theta=0. Well, revolution surfaces can be obtained by a bit of analytic geometry and plotted e.g. by using mesh. Check this out:
% // 2D polar coordinate radius (your j)
Rad= (cos(theta)+1).*(besselj(1,const*sin(theta))./(const*sin(theta)));
Rad = abs(Rad); % // We need its absolute value for sake of clarity
xv = Rad .* cos(theta); % // 2D Cartesian coordinates
yv = Rad .* sin(theta); % // 2D Cartesian coordinates
phi = -pi:.01:pi; % // 3D revolution angle around theta = 0
% // 3D points of the surface
xf = repmat(xv',size(phi));
yf = yv' * cos(phi);
zf = yv' * sin(phi);
mesh(xf,yf,zf)
You can also add graphics effects
this is done via
mesh(xf,yf,zf,'FaceColor','interp','FaceLighting','phong')
camlight right
and a finer angular discretization (1e-3).

Matlab 3D plot on Cylindrical Axes

I've run simulations which have given me data points corresponding to X number of different radii, and Y number of angles each one was evaluated at. This means that I have X times Y data points which I need to plot.
I am currently plotting it in an non-ideal fashion: I am using the x and y axes as the r and theta axes. This means that my data appears as a sinusoidal trend which increases with radius on a Cartesian grid, not the circle which it physically represents. This is how I am currently plotting my data:
surf(r_val, th_val, v_val);
What I wish to do is plot my data on a cylindrical axis, such like that of the function polar(), but in R3 space. I would rather not download a toolbox, or modify the existing polar function; if there is no other solution then I will obviously end up doing this anyways.
Thanks for your help!
G.
Also, I am using Matlab 2012a
EDIT:
r_val = 1x8 vector containing unique radii
th_val = 1x16 vector containing unique angles
v_val = 8x16 matrix containing voltages corresponding to each position
NOTE: (after answered)
The truly ideal solution does not exist to this problem, as Matlab currently supports no true polar axes methods. Resource found here.
You should transform your coordinates to Cartesian coordinates before plotting them. MATLAB has builtin functions for perfroming coordiante transformations. See, for example pol2cart, which transforms polar or cylindrical coordinates to Cartesian coordinates. In your case you would simply use something like:
[x, y] = pol2cart(th_val, r_val);
surf(x, y, v_val);
Edit: Given that th_val and r_val are vectors of differing lengths it is necessary to first create a grid of points before calling pol2cart, along the lines of:
[R, T] = meshgrid(r_val, th_val);
[x, y] = pol2cart(T, R);
surf(x, y, v_val);

How do I make a surf plot in MATLAB with irregularly spaced data?

I know I can create a 3D surface plot in MATLAB by doing:
x = linspace(1,10,100);
y = linspace(10,20,100);
[X Y] = meshgrid(x,y);
Z = X * Y;
surf(X,Y,Z);
But this requires that all the nodes for the height map generated line up. I have a set of data which has arbitrary points (x,y) and a height (z). Is there a simple way to plot a graph which will generate a surface between the points in a similar fashion to surf?
Appologies, after some hunting I managed to answer my own question:
You can use the trisurf function:
tri = delaunay(x,y);
trisurf(tri,x,y,z);
If you have dense data you will want to do shading interp (or another value, check doc shading) so you don't get a black blob due to the grid.
It looks like you've found your answer by using DELAUNAY and TRISURF to generate and plot a triangulated surface.
As an alternative, you could also fit a regularly-spaced grid to your nonuniformly-spaced points in order to generate a surface that can be plotted with the SURF command. I discuss how this can be done using the TriScatteredInterp class (or the deprecated function GRIDDATA) in my answer to this other question on SO.