How do I calculate the area of a projection created by the command "view"? - matlab

How do I calculate the area of a projection? For example, using the following code, I get a projection on the X-Z plane.
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
view([0,0])
X-Z Plane Projection
I want to be able to determine the area of the projection of any surf plot that I create. Is there a command or function for this?

Short answer
polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
Explanation
The plot you want to calculate its area is,
In area calculation you need only the borders of the projection,
plot(x(1,:),max(z))
hold on
plot(x(1,:),min(z),'r')
and the output is,
The total area is the summation of both areas (upper border to x axis and lower border to x axis),
>> polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
>> 28.5947

If you want to get the projection area at an arbitrary view angle, you can use the viewmtx function to project the surface onto the viewing plane, and then use boundary and polyarea to extract the boundary and calculate the area. Something like this:
% draw your surface
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
axis equal;
%extract the viewing angle, and calculate the resulting transformation matrix
[az,el]=view(gca);
T= viewmtx(az,el);
% transform the surface points by this transformation matrix
pts= [x(:),y(:),z(:),ones(numel(x),1)]';
tpts= T*pts;
tpts=(tpts(1:3,:))';
% now "tpts" has the surface points projected onto the viewing plane
figure, plot( tpts(:,1), tpts(:,2), 'bo'); axis equal;
% calculate the border of this region, and calculate its area.
border = boundary(tpts(:,1), tpts(:,2));
projectedArea = polyarea(tpts(border,1), tpts(border,2));
This approach is based off of the help for viewmtx.

Related

How to place a geographical map underneath a surf plot in Matlab?

If there is an easier way to do this in general any suggestions would be appreciated.
I have imported a .nc file into Matlab and currently have a surface plot created from the interpolated matrix of data which corresponds to longitudes and latitudes. An alpha scale has been applied to add opacity and now I would like to place a map of said longs and lats beneath it so that it acts as an overlay.
here is the code used to plot where xq,yq is the mesh longitude and latitude grid and newMatrix is the interpolated version of the original matrix. Attached is the top view surf plot which I would like a map of long lat restricted x,y axis beneath (line style or topographic)
f = figure;
ax = axes('Parent',f);
h = surf(xq,yq,newMatrix,'Parent',ax);
set(h, 'edgecolor','none');
view(ax,[0,90]);
alpha color
alpha scaled
grid off
colorbar;
I'm aware of geomap but I'm not sure how it can be used in this way without causing the axis to misalign or just not be applied correctly. Before I was using a contour plot but found it easier to interpolate and plot with surf as per this post: Matlab how to make smooth contour plot?

2D fitting lines in 3D plot

I wish to put 2D curve fitting to different axis within a 3D plot. I am attaching an image for reference.
on this actual data set:
As you can see the curve fitting for x and y axis is at z=0 value, I need that at say z=10.
Further, when I try to do curve fitting for x and z data set or y and z data set, the fitted curve instead of appearing on the the X-Z plane or Y-Z plane, is appearing on the X-Y plane.
All help is appreciated.

Need help in 3D plot using MATLAB (X,Y,Z,V)

I need to ask help in plotting a 3D volume in MATLAB. My data set includes the X, Y, Z coordinate and corresponding intensity value, V.
I was using pcolor (X,Y,V) and shading interp while i was doing the 2D images but then I got stuck when I am about to create the 3D images. I have tried scatter3, smooth3 and slice but it seems it does not fit the function I need.
My goal is to plot the 3D grid with the corresponding intensity value per coordinate and apply shading interp between these points.
I am really new in 3D plotting and I would really appreciate any help in achieving my goal. Thank you so much!
Here are some example of images that I am trying to create
(source: ndt.net)
(source: www.bam.de)
I have a solution for your first example, in which you show three cross-sections of the volume data. With this solution you can essentially plot several pcolor with different orientations, in one same 3D figure.
First, you need the data for the different slices. This is exactly what you had when you did pcolor(X,Y,V) to plot the cross section in 2D, where X, Y and V are two dimensional matrices. You will also need to create a matrix Z with the z-position of the slice (e.g. Z = zeros(size(X)) + z0). Then you use the command surface(X,Y,Z,V) to plot the cross section image in 3D, where X,Y,Z are the positions, and V is the color (value of the function).
Repeat this procedure for all the other slices, using appropriate X,Y,Z,V matrices for each slice. For slices oriented on other axes you will have to define X,Y,Z accordingly. Remember to use "hold on" after the first surface command, so that all the slices are plotted.
Example:
surface(X1,Y1,Z1,V1); shading interp; hold on
surface(X2,Y2,Z2,V2); shading interp;
surface(X3,Y3,Z3,V3); shading interp;
colormap(jet(2048)); caxis([0,3]); % color axis colormap and scaling
axis equal; xlabel('X'); ylabel('Y'); zlabel('Z') % X,Y,Z scaling and label
Result figure here:

Plotting a rectangular matrix into a circle

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

How to plot data points on top of what was previously plotted in MATLAB?

How to plot data points on top of what was previously plotted in MATLAB?
For example I have a 2D plot of a plane and points, and I plot
the points with scatter3 after I plot the plane with pcolor,
and the plane covers the points that are below it.
In the first image you can see the points by themselves. But when I plot
them together with the plane, the plane covers the points below it.
THanks
The issue is that you are using scatter3 instead of scatter and it is plotting against the 3rd dimension linearly which is why the points disappear half way down. They are actually just behind your plane.
Use uistack to bring data points on top of the plot of the plane. Something like
p1 = plot(...); % plot of data points (need to be on top of plane)
hold on;
p2 = plot(...); % plot of the plane
uistack(p1); % to bring data points on top of the plane
Try hold on. See http://www.mathworks.co.uk/help/matlab/ref/hold.html for more information.