convert x and y vector to meshgrid - matlab

I have an x, y, and z vector that I am currently using to create a 3-D scatter plot. Is it possible to create a mesh plot using these three vectors? I would prefer to only use these vectors and not alter any of my previous code.

I'm a bit confused by your terminology, but I'm assuming that you have unstructured surface data - z is the surface height for a set of positions x, y.
If you want to form a "mesh" for this data you could triangulate (via a Delauany triangulation of the positions):
t = delaunayn([x, y]);
If you want to visualise the "meshed" surface you can use trimesh/trisurf:
figure;
trimesh(t, x, y, z);

Related

How can i create a 3D surface Plot in MATLAB?

I need to plot 3 -D plots in MATLAB. Here is one example;
x=0:1:10
for y=1:1:100
Z=1-(1-qfunc(sqrt(2*10.^(x/10)))).^y
end
I need to plot x y and Z in three axes, where Z is function of x and y.
How can i create a surface plot of Z along the x and y axis in MATLAB?
You can use surf and/or mesh functions as follows:
[X,Y] = meshgrid(0:10,1:100);
Z=1-(1-qfunc(sqrt(2*10.^(X/10)))).^Y;
mesh(X,Y,Z)
where creates a mesh, and:
surf(X,Y,Z)
creates a surface. For more options check this page.
remember that the data should be converted to a meshgrid and then you will be able to use those functions.

3D plot matlab with 3 equally sized vectors

I have three vectors, X, Y, and Z. All of equal length (20000,1). I want to plot all three in a 3d plot. I have tried using surf and plot3 but to no avail as they require Z to be of size (20000,20000). Can anybody help?
TIA
X = DAT(3,:);
Y = DAT(4,:);
Z = DAT(11,:);
[x,y] = meshgrid(X,Y);
surf(x,y,Z);
Have you tried griddata or TriScatteredInterp to create an interpolated surface?
NO! plot3 does NOT require that of Z. If all you wish is to plot a point set, then plot3 does EXACTLY what you want.
plot3(X,Y,Z,'.')
The point is, there is NO need to use meshgrid for plot3. In fact, there is no need to use meshgrid as you have tried in order to use surf. (If you will be calling griddata, then meshgrid would be necessary, but for a SMALLER mesh.)
IF you need a surface plot, then you need to create a surface. If the points are scattered, then your basic options are tools like triscatteredinter, griddata, or gridfit, the last from the file exchange.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311767 gives the code snippet may help you.
X=rand(1,30);
Y=rand(1,30);
Z=rand(1,30);
[XI YI ZI] = griddata(X,Y,Z,linspace(0,1),linspace(0,1)');
figure
subplot(1,2,1)
trisurf(delaunay(X,Y),X,Y,Z)
subplot(1,2,2);
surf(XI,YI,ZI)

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);

Repeating x and y vectors for use with scatter3()

Currently I have a working 3D mesh() plot. However, the vertical axis lends itself better to an angular representation, so I'm constructing a coordinate transform to cylindrical space, after which I'll plot everything with scatter3().
Currently I have one-dimensional vectors containing all of the possible x and y values; however, they do not repeat (and they need to, to work in scatter3()). I have to flatten my two-dimensional z-matrix using z(:). Is there a quick method to repeat x and y to also be scatter3-compatible?
Thanks...
Use meshgrid and then flatten:
[X,Y] = meshgrid(x,y);
scatter3(X(:), Y(:), z(:));

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.