Functions > Graphing > Example: Converting a Spherical Data Set into Cartesian Coordinates
Example: Converting a Spherical Data Set into Cartesian Coordinates
3D plots only accept Cartesian coordinates. To plot spherical data sets, you must first convert each point to Cartesian coordinates. Using various functions, you can convert data between Spherical, Cartesian, and Cylindrical coordinate systems.
1. Define a spherical data set.
Each row represents a point in 3D space where the element of the first column is the radius, the element of the second column is the horizontal angle and the element of the third column is the vertical angle.
2. Call sph2xyz to convert the first point to Cartesian coordinates. You can insert the Spherical coordinates one after the other, or you can insert them as a column vector.
 The sph2xyz function performs the following calculations: x r sin ψ cos θ y r sin ψ sin θ z r cos ψ
3. Write a short program to convert all the points from Spherical coordinates to Cartesian coordinates.
4. Plot the data set in a 3D plot.
You can use a similar method to plot Cylindrical coordinates in a 3D plot. Replace the call to sph2xyz with a call to cyl2xyz.
Converting the Coordinates Back to Spherical Data Set
1. Use xyz2sph to convert the Rectangular coordinates to Spherical coordinates.
In this case, the parameter for xyz2sph is a 3-element vector. You can also use xyz2sph with three explicit coordinates, for example:
 You can convert Cartesian coordinates to Cylindrical coordinates using xyz2cyl with either explicit coordinates or a 3-element vector. xyz2cyl 0 0 2 xyz2cyl 0 0 2