Cartesian to Polar

2 次查看(过去 30 天)
Melissa
Melissa 2011-8-9
Good Afternoon All,
I have a data file of cartesian coordinates that make a cylinder. I wish to take each "layer" or "slice" of the cylinder and transform into polar coordinates. If there is no tilt I can sort the coordinates by height (z). However what if there is a tilt on the axis how can I possible sort the coordinates so I can correctly obtain the correct polar coordinates? If I sort just by height I get an incorrect order of cylinder points for a tilt. Please see picture below.
Also I have the code correct for no tilt but am unsure if the code is correct for an axis tilted. Any help is greatly appreciated.
function [profile, angles] = importbore1(layer,rm)
%IMPORTBORE Imports grid and displacement data from "grid.dat" and "3L_stopper_displaced.dat."
% [profile, angles] = importbore(layer,rm) imports deck (or layer) from above data set.
% rm = known radius of bore/ring
% profile = bore profile (radius as a function of angle)
% angles = angles for which profile is known
%load data files here (reverse ordering)
A=importdata('sorted_coordinates.dat');
A = flipud(A);
B=importdata('sorted_displacement.dat ');
B = flipud(B);
len = length(A);
%get grid locations and each component of displacement
x0 = A(1:len,2); y0 = A(1:len,3); z0 = A(1:len,4);
dx0 = B(1:len,2); dy0 = B(1:len,3); dz0= B(1:len,4);
%Bottom and Top deck centers
XCent = [0.004256, 0.004256]; YCent = [305.495, 305.495]; ZCent = [196, 74.25];
%the cylinder is sometimes titled in a V-engine, rotate s.t. parrallel with z-axis so center coordinates will vary
%[THETA,RHO] = cart2pol((x0 or y0),z0); %if axis is tilted
[TH,PHI,R] = cart2sph(dx0,dy0,dz0);
%tiltangle=atan((ZCent(2)-ZCent(1))/(XCent(or YCent)(2)-XCent (or YCent)(1))); %if tilted
%tiltangle=pi/4;
%THETA = THETA+tiltangle; %if tilted
%PHI = PHI + tiltangle; %if tilted
x=x0;y=y0;z=z0;
%[(x or y),z] = pol2cart(THETA,RHO); %if tilted
[dx,dy,dz]=sph2cart(TH,PHI,R);
%[THETA,RHO] = cart2pol((XCent or YCent),ZCent); %if tilted
%THETA = THETA+tiltangle; %if tilted
%[(XCent,YCent) ZCent] = pol2cart(THETA,RHO); %if tilted
%extract the deck: All decks have 72 nodes
SP = 73*(layer-1); num = 71;
%SP = (points + 1)*(layer-1); num = (points - 1);
if layer == 1
SP = SP+1; num = 71;
end
if layer> 2
SP = SP-(layer-2);
end
boreX = x(SP:SP+num); boreY = y(SP:SP+num); boreZ = z(SP:SP+num);
dispX = dx(SP:SP + num); dispY = dy(SP:SP + num); dispZ= dz(SP:SP + num);
centX = XCent(1); centY = YCent(1); centZ = mean(boreZ);
%calculate radial component, throw away the rest
for i=1:length(boreX)
rv = [boreX(i)-centX, boreY(i)-centY];
[angles1(i),grid(i)] = cart2pol(rv(1),rv(2));
deltar(i)=cos(angles1(i))*dispX(i) + sin(angles1(i))*dispY(i);
end
%generate the profile (make sure angles are increasing)
prof = ([angles1;rm + deltar])';
prof = sortrows(prof);
angles = (prof(:,1))';
profile = (prof(:,2))';
  1 个评论
Oleg Komarov
Oleg Komarov 2011-8-9
Please format the code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099

请先登录,再进行评论。

采纳的回答

Sean de Wolski
Sean de Wolski 2011-8-9
I think this might be what you want.
  1 个评论
Melissa
Melissa 2011-8-12
Thank you very much for always responding. Another way which worked nice was sorting by height then converting teh data to polar coordinates before sorting.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polar Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by