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(:));
Related
In MATLAB, quiver will plot a vector field on the x-y plane by default. Is there a way to rotate the image so that is lies on the x-z plane?
I have tried creating a 3D matrix and using commands like streamslice and quiver3. However I have a large number of data points that are irregularly spaced so this results in some inaccurate interpolation and matrices that are too big.
It seems the easiest option would be to use a command like rotate, but that doesn't seem to work with quiver.
You should just use quiver3 and add zero-valued (or whatever constant y you want) inputs in the dimensions that you don't care about
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
Q = quiver3(x, zeros(size(x)), y, u, zeros(size(u)), v);
axis equal
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.
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)
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);
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);