I am struggling with the concepts behind plotting a surface polar plot.
I am trying to plot the values measured by a sensor at a combination of different angles over a hemisphere.
I have an array containing the following information:
A(:,1) = azimuth values from 0 to 360º
A(:,2) = zenith values from 0 to 90º
A(:,3) = values measured at the combination of angles of A(:,1) and A(:,2)
For example, here is a snippet:
0 15 0.489502132167206
0 30 0.452957556748497
0 45 0.468147850273115
0 60 0.471115818950192
0 65 0.352532182508945
30 15 0.424997863795610
30 30 0.477814980942155
30 45 0.383999653859467
30 60 0.509625464595446
30 75 0.440940431784788
60 15 0.445028058361392
60 30 0.522388502880219
60 45 0.428092266657885
60 60 0.429315072676194
60 75 0.358172892912138
90 15 0.493704001125912
90 30 0.508762762699997
90 45 0.450598496609200
90 58 0.468523071441297
120 15 0.501619699042408
120 30 0.561755273071577
120 45 0.489660355057938
120 60 0.475478615354648
120 75 0.482572226928475
150 15 0.423716506205776
150 30 0.426735372570756
150 45 0.448548968227972
150 60 0.478055144126694
150 75 0.437389584937356
To clarify, here is a piece of code that shows the measurement points on a polar plot.
th = A(:,1)*pi/180
polar(th,A(:,2))
view([180 90])
This gives me the following plot:
I would like now to plot the same thing, but instead of the points, use the values of these points stored in A(:,3). Then, I would like to interpolate the data to get a colored surface.
After some research, I found that I need to interpolate my values over a grid, then translate to Cartesian coordinates. From there I do not know how to proceed. Could someone point me in the right direction?
I have trouble getting the concept of the interpolation, but this is what I have attempted:
x1 = linspace(0,2*pi,100)
x2 = linspace(0,90,100)
[XX,YY] = meshgrid(x1,x2)
[x,y] = pol2cart(th,A(:,2))
gr=griddata(x,y,A(:,3),XX,YY,'linear')
With this piece of code, your example data points are converted into cartesian coords, and then plotted as "lines". The two tips of a line are one data point and the origin.
az = bsxfun(#times, A(:,1), pi/180);
el = bsxfun(#times, A(:,2), pi/180);
r = A(:,3);
[x,y,z] = sph2cart(az,el,r);
cx = 0; % center of the sphere
cy = 0;
cz = 0;
X = [repmat(cx,1,length(x));x'];
Y = [repmat(cy,1,length(y));y'];
Z = [repmat(cz,1,length(z));z'];
Still thinking how to interpolate the data so you can draw a sphere. See my comments to your question.
Related
i have this problem i want to make a color of errobars different from the color of my graph
there is the code i have tried
pp=errorbar(x,testMatriceFluxSortie/ValeurFluxSortie(1,1),err)
pp.Color=[255 0 1]./255;
But it gives me this all in red
my graph
you can always use hold on and plot only the x,y data after the errorbar was plotted, for example:
x = 1:10:100;
y = [20 30 45 40 60 65 80 75 95 90];
err = 8*ones(size(y));
errorbar(x,y,err,'or'); hold on
plot(x,y,'b');
For example, using the following code, I have a coordinate matrix with 3 cubical objects defined by 8 corners each, for a total of 24 coordinates. I apply a rotation to my coordinates, then delete the y coordinate to obtain a projection in the x-z plane. How do I calculate the area of these cubes in the x-z plane, ignoring gaps and accounting for overlap? I have tried using polyarea, but this doesn't seem to work.
clear all
clc
A=[-100 -40 50
-100 -40 0
-120 -40 50
-120 -40 0
-100 5 0
-100 5 50
-120 5 50
-120 5 0
-100 0 52
-100 0 52
20 0 5
20 0 5
-100 50 5
-100 50 5
20 50 52
20 50 52
-30 70 53
-30 70 0
5 70 0
5 70 53
-30 120 53
-30 120 0
5 120 53
5 120 0]; %3 Buildings Coordinate Matrix
theta=60; %Angle
rota = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %Rotation matrix
R=A*rota; %rotates the matrix
R(:,2)=[];%deletes the y column
The first step will be to use convhull (as yar suggests) to get an outline of each projected polygonal region. It should be noted that a convex hull is appropriate to use here since you are dealing with cuboids, which are convex objects. I think you have an error in the coordinates for your second cuboid (located in A(9:16, :)), so I modified your code to the following:
A = [-100 -40 50
-100 -40 0
-120 -40 50
-120 -40 0
-100 5 0
-100 5 50
-120 5 50
-120 5 0
-100 0 52
-100 0 5
20 0 52
20 0 5
-100 50 5
-100 50 52
20 50 5
20 50 52
-30 70 53
-30 70 0
5 70 0
5 70 53
-30 120 53
-30 120 0
5 120 53
5 120 0];
theta = 60;
rota = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1];
R = A*rota;
And you can generate the polygonal outlines and visualize them like so:
nPerPoly = 8;
nPoly = size(R, 1)/nPerPoly;
xPoly = mat2cell(R(:, 1), nPerPoly.*ones(1, nPoly));
zPoly = mat2cell(R(:, 3), nPerPoly.*ones(1, nPoly));
C = cell(1, nPoly);
for iPoly = 1:nPoly
P = convhull(xPoly{iPoly}, zPoly{iPoly});
xPoly{iPoly} = xPoly{iPoly}(P);
zPoly{iPoly} = zPoly{iPoly}(P);
C{iPoly} = P([1:end-1; 2:end].')+nPerPoly.*(iPoly-1); % Constrained edges, needed later
end
figure();
colorOrder = get(gca, 'ColorOrder');
nColors = size(colorOrder, 1);
for iPoly = 1:nPoly
faceColor = colorOrder(rem(iPoly-1, nColors)+1, :);
patch(xPoly{iPoly}, zPoly{iPoly}, faceColor, 'EdgeColor', faceColor, 'FaceAlpha', 0.6);
hold on;
end
axis equal;
axis off;
And here's the plot:
If you wanted to calculate the area of each polygonal projection and add them up it would be very easy: just change the above loop to capture and sum the second output from the calls to convexhull:
totalArea = 0;
for iPoly = 1:nPoly
[~, cuboidArea] = convhull(xPoly{iPoly}, zPoly{iPoly});
totalArea = totalArea+cuboidArea;
end
However, if you want the area of the union of the polygons, you have to account for the overlap. You have a few alternatives. If you have the Mapping Toolbox then you could use the function polybool to get the outline, then use polyarea to compute its area. There are also utilities you can find on the MathWorks File Exchange (such as this and this). I'll show you another alternative here that uses delaunayTriangulation. First we can take the edge constraints C created above to use when creating a triangulation of the projected points:
oldState = warning('off', 'all');
DT = delaunayTriangulation(R(:, [1 3]), vertcat(C{:}));
warning(oldState);
This will automatically create new vertices where the constrained edges intersect. Unfortunately, it will also perform the triangulation on the convex hull of all the points, filling in spots that we don't want filled. Here's what the triangulation looks like:
figure();
triplot(DT, 'Color', 'k');
axis equal;
axis off;
We now have to identify the extra triangles we don't want and remove them. We can do this by finding the centroids of each triangle and using inpolygon to test if they are outside all 3 of our individual cuboid projections. We can then compute the areas of the remaining triangles and sum them up using polyarea, giving us the total area of the projection:
dtFaces = DT.ConnectivityList;
dtVertices = DT.Points;
meanX = mean(reshape(dtVertices(dtFaces, 1), size(dtFaces)), 2);
meanZ = mean(reshape(dtVertices(dtFaces, 2), size(dtFaces)), 2);
index = inpolygon(meanX, meanZ, xPoly{1}, zPoly{1});
for iPoly = 2:nPoly
index = index | inpolygon(meanX, meanZ, xPoly{iPoly}, zPoly{iPoly});
end
dtFaces = dtFaces(index, :);
xUnion = reshape(dtVertices(dtFaces, 1), size(dtFaces)).';
yUnion = reshape(dtVertices(dtFaces, 2), size(dtFaces)).';
totalArea = sum(polyarea(xUnion, yUnion));
And the total area for this example is:
totalArea =
9.970392341143055e+03
NOTE: The above code has been generalized for an arbitrary number of cuboids.
polyarea is the right way to go, but you need to call it on the convex hull of each projection. If not, you will have points in the centers of your projections and the result is not a "simple" polygon.
Using matlab, I want to apply transform contain of rotate and translate to 2d points.
for example my points are:
points.x=[1 5 7 100 52];
points.y=[42 96 71 3 17];
points.angle=[2 6 7 9 4];
the value of rotate is:30 degree
the value of x_translate is 5.
the value of y_translate is 54.
can any body help me to write matlab code for apply this transform to my points and calculate new coordinate of points after transform?
I don't know what you mean by points.angle since the angle of the points with respect to origin (in a trigonometric sense) is already defined by atand2(y,x)
Here is the code:
clear;clc
oldCoord = [1 5 7 100 52;42 96 71 3 17];
newCoord = zeros(size(oldCoord));
theta = 30 * pi/180;
T = #(theta) [cos(theta), -sin(theta); sin(theta) , cos(theta)];
trans = [5;54];
for m = 1:size(oldCoord,2)
newCoord(:,m) = T(theta) * oldCoord(:,m) + trans;
end
Result:
oldCoord =
1 5 7 100 52
42 96 71 3 17
newCoord =
-15.1340 -38.6699 -24.4378 90.1025 41.5333
90.8731 139.6384 118.9878 106.5981 94.7224
I have a series of ordered points (X, Y, Z) and I want to plot a 3D histogram, any suggestions?
I'm trying to do it by this tutorial http://www.mathworks.com/help/stats/hist3.html , but points are random here and presented as a function. My example is easier, since i already know the points.
Furthermore, depending on the number value of Z coordinate, i'd like to colour it differently. E.g. Max value - green, min value - red. Similar as in this case Conditional coloring of histogram graph in MATLAB, only in 3D.
So, if I have a series of points:
X = [32 64 32 12 56 76 65]
Y = [160 80 70 48 90 80 70]
Z = [80 70 90 20 45 60 12]
Can you help me with the code for 3D histogram with conditional coloring?
So far the code looks like this:
X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));
figure;
h = bar3(A); % get handle to graphics
for k=1:numel(h),
z=get(h(k),'ZData'); % old data - need for its NaN pattern
nn = isnan(z);
nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point
nz(nn) = NaN; % used saved NaN pattern for transparent faces
set(h(k),'CData', nz); % set the new colors
end
colorbar;
Now I just have to clear the lines and design the chart to make it look useful. But how would it be possible to make a bar3 without the entire mesh on 0 level?
Based on this answer, all you need to do is rearrange your data to match the Z format of that answer. After than you might need to remove edgelines and possibly clear the zero height bars.
% Step 1: rearrange your data
X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));
% Step 2: Use the code from the link to plot the 3D histogram
figure;
h = bar3(A); % get handle to graphics
set(h,'edgecolor','none'); % Hopefully this will remove the lines (from https://www.mathworks.com/matlabcentral/newsreader/view_thread/281581)
for k=1:numel(h),
z=get(h(k),'ZData'); % old data - need for its NaN pattern
nn = isnan(z);
nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point
nz(nn) = NaN; % used saved NaN pattern for transparent faces
nz(nz==0) = NaN; % This bit makes all the zero height bars have no colour
set(h(k),'CData', nz); % set the new colors. Note in later versions you can do h(k).CData = nz
end
colorbar;
I have to interpolate wind directions. The data is given for every 1000 [ft]. for example:
%winddata input in feet en degrees
x=0:1000:10000;
grad=[340 350 360 1 10 20 30 35 34 36 38];
The interpolation works great, i use the function interp1. (please see code below.) However, the step from 360 degrees to 1 degrees is a problem. I want Matlab to interpolate from 360 to 1 degree directly (clockwise), instead of anticlockwise with the values degreasing from 360-359-358-...3-2-1. That doesnt make sense when you interpolate wind directions.
How can i command Matlab to repeate the x-axis and values every 360 degrees?
clear all;clc;
h=2000;
%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 1 10 20 30 35 34 36 38];
%conversion to SI:
x=0.3048*x;
u=0:1:max(x);
yinterp1 = interp1(x,degrees,u,'linear');
figure(3)
plot(degrees,x,'bo',yinterp1,u,'-r')
xlabel('wind direction [degrees]')
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'
The problem is that Matlab, as smart as it is, doesn't realise that you're working with degrees, so therefore it sees no reason that 360 = 0. Therefore I believe your problem isn't with finding a way to repeat the plot every 360 degrees, but rather with the data you are feeding in to your interp1 function, as currently you are telling it there is a straight line between the points (0, 950) and (360, 750).
The easiest, but ugliest, method would be to just add 360 to your lower values, so your degrees vector read:
degrees = [340 350 360 361 370 380 390 395 394 396 398];
and then subtract 360 from your degrees and yinterp1 vectors:
clear all;clc;
h=2000;
%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 361 370 380 390 395 394 396 398];
%conversion to SI:
x=0.3048*x;
u=0:1:max(x);
yinterp1 = interp1(x,degrees,u,'linear');
figure(3)
plot(degrees-360,x,'bo',yinterp1-360,u,'-r')
xlabel('wind direction [degrees]')
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'
xlim([-180 180]);
The obvious problem with this is it isn't able to be applied for all cases, but if you just need a one off, then it works well.
For a more generic solution you could have it so you manually enter a point below which values have 360 added to them:
clear all;clc;
h=2000;
% --------------------- Manual cutoff for rectification -------------------
limitDegrees = 180;
% -------------------------------------------------------------------------
%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 1 10 20 30 35 34 36 38];
%conversion to SI:
x=0.3048*x;
u=0:1:max(x);
indecesTooSmall = find(degrees <= limitDegrees);
oneVec = zeros(size(degrees));
oneVec(indecesTooSmall) = 1;
vecToAdd = 360*ones(size(degrees));
vecToAdd = vecToAdd .* oneVec;
newDegrees = degrees + vecToAdd;
yinterp1 = interp1(x,newDegrees,u,'linear');
figure(3)
plot(newDegrees-360,x,'bo',yinterp1-360,u,'-r')
xlabel('wind direction [degrees]')
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'
xlim([-180 180]);
Both of the above solutions give the following:
EDIT: Substantially easier solution, just use rad2deg(unwrap(deg2rad(degrees))), or try to find an unwrap which works for degrees.