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
Related
Given some function z = f(x,y), I'm interested in creating a (1D) line plot along an arbitrary cutting plane in x,y,z. How do I do this in Matlab? Slice, for example, provides a higher dimensional version (colormap of density data) but this is not what I'm looking for.
E.g.:
z = peaks(50);
surf(z);
%->plot z along some defined plane in x,y,z...
This has been asked before, e.g. here, but this is the answer given is for reducing 3D data to 2D data, and there is no obvious answer on googling. Thanks.
If the normal vector of the plane you want to slice your surface will always lay in the xy plane, then you can interpolate the data over your surface along the x,y coordinates that are in the slicing line, for example, let the plane be defined as going from the point (0,15) to the point (50,35)
% Create Data
z=peaks(50);
% Create x,y coordinates of the data
[x,y]=meshgrid(1:50);
% Plot Data and the slicing plane
surf(z);
hold on
patch([0,0,50,50],[15,15,35,35],[10,-10,-10,10],'w','FaceAlpha',0.7);
% Plot an arbitrary origin axis for the slicing plane, this will be relevant later
plot3([0,0],[15,15],[-10,10],'r','linewidth',3);
Since it is a plane, is relatively easy to obtain the x,y coordinates alogn the slicing plane with linspace, I'll get 100 points, and then interpolate those 100 points into the original data.
% Create x and y over the slicing plane
xq=linspace(0,50,100);
yq=linspace(15,35,100);
% Interpolate over the surface
zq=interp2(x,y,z,xq,yq);
Now that we have the values of z, we need against what to plot them against, that's where you need to define an arbitrary origin axis for your splicing plane, I defined mine at (0,15) for convenience sake, then calculate the distance of every x,y pair to this axis, and then we can plot the obtained z against this distance.
dq=sqrt((xq-0).^2 + (yq-15).^2);
plot(dq,zq)
axis([min(dq),max(dq),-10,10]) % to mantain a good perspective
I have 3D matrix (100*50*10) and I want to plot one specific point in all slices. Let us say point (10*6*:). The plot should be in 2D plane
Example (I have this coordinate for point that I want to plot)
x (10*6*1)
x (10*6*2)
x (10*6*3)
x (10*6*4)
x (10*6*5)
x (10*6*6)
x (10*6*7)
x (10*6*8)
x (10*6*9)
x (10*6*10)
I tried plot (x(10,6,:)) but I got error
plot(squeeze(x(10,6,:)))
see: https://www.mathworks.com/help/matlab/ref/squeeze.html
x(10,6,:) is still a 3D matrix, and needs to be reduced to a 1D form before plotting it. This is where the squeeze function comes in.
I have generated a rectangular matrix with the azimouth angle changing with rows and the radius changing as you change column. These are meant to represent the relative velocities experienced by a rotating helicopter blade. This produces a matrix called Vmat. I want to plot this to appears in a circle (representing the rotation of the blade)
So far I have tried
[R,T] = meshgrid(r,az);
[x,y] = pol2cart(T,R);
surf(x,y,Vmat(r,az));
which should produce a contoured surface showing velocity as it changes with azimouth angle and radius but it comes up with dimension errors.
I don't mind if it is a 2d contour plot or 3d plot i guess both would be written in a similar way.
Thanks
James
The error is in writing Vmat(r,az), presuming that these are actual values of radius and azimuth, not indexes into your radius and azimuth. If you want to take only a subset of Vmat that's a slightly different matter, but this should work:
[R,T] = meshgrid(r,az); % creates a grid in polar coordinates
[x,y] = pol2cart(T,R); % changes those to cartesian for surf
surf(x,y,Vmat);
Alternatively you could do a contour plot:
h = polar([0 2*pi], [0 max(r)]); % set up polar axes with right scale
delete(h) % remove line
hold on
contour(x,y,Vmat);
This question entails rotating an image given as a 3D matrix where the first dimension is width, 2nd dimension is height, and 3rd dimension contains x,y,z coordinates.
Currently I am plotting a surface using the following code
Fig.sub1im=surf(ToFparam.ROI.XYZ(:,:,1),ToFparam.ROI.XYZ(:,:,2),ToFparam.ROI.XYZ(:,:,3),zeros(ToFparam.ROI.height,ToFparam.ROI.width,3));
Now, I have a 3-D matrix where it's a 100x50x3. All x data is in the first...page or layer of the 3rd dimension, y is the second layer...z the third. Now I need to apply a 3x3 rotation matrix on the x y and z data. I know how to reshape a matrix to do this I think....just put it into a 3 row by...50000 column matrix then apply the matrix.
Next I need to update my plot in a loop. I was going to then do the following, where I have also included my new matrix calculation.
ToFparam.ROI.XYZ_Vector = ToFparam.ROI.XYZ;
ToFparam.ROI.XYZ_Vector = reshape(ToFparam.ROI.XYZ, [size(ToFparam.ROI.XYZ,1)*size(ToFparam.ROI.XYZ,2),3]);
ToFparam.ROI.XYZ_Vector = ToFparam.ROI.XYZ_Vector';
ToFparam.ROI.XYZ_DICOM = inv(DICOMparam.calib.navi2dicom(1:3,1:3))*inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*ToFparam.ROI.XYZ_Vector;
%refresh plot standard cuts
set(Fig.sub1im,'CData', Color);
set(Fig.sub1im, 'XData', ToFparam.ROI.XYZ_DICOM(1,:) + DICOMPos(1)/Fig.sub2samp);
set(Fig.sub1im, 'YData', ToFparam.ROI.XYZ_DICOM(2,:) + DICOMPos(2)/Fig.sub2samp);
set(Fig.sub1im, 'ZData', ToFparam.ROI.XYZ_DICOM(3,:) + DICOMPos(3)/Fig.sub2samp);
When I update my plot, I get no errors, but it doesn't look like it's plotting it correctly. It seems like it makes a huge offset on my data and positions it somewhere I don't want it to be positioned. I wouldn't expect that a rotation matrix effects the scaling, just the orientation. Let me know if there are any faster/better ways to accomplish this surf plot, thanks!
For anyone interested, I have found the solution.
If you ever want to rotate a 3-D matrix where the x,y,z data is located in the 3rd dimension, use the following for maximum speed and efficiency.
[m,n,z]=size(inMatrix);
outMatrix=reshape((A*(reshape(double(inMatrix),[m*n 3]))')',[m n 3]);
Where inMatrix is your initial 3d matrix, outMatrix is your output 3d matrix, and A is your rotation matrix. Can be extremely useful when wanting to code rotations in images, since images will have 2 dimensions along their width and height, and the 3rd dimension will be the x, y and z coordinates. This will allow you to easily plot an initial data set, rotate it, then replot it.
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);