user1166251
user1166251

Reputation: 115

creating a 2D circular mesh in Matlab

I have some data on a horizontal line (x,y,data) (2991*3) with y=0. Instead of plotting the line in a Cartesian coordinate system, I would like to transform that line into a quarter circle and plot the data in a range of color.

I'm able to create a mesh

rho = x
theta = (0:0.5:90)*pi/180
[th, r] = meshgrid(theta,rho);

The problem I'm having is to plot the circle in 2D and link the data to the mesh. Any tips would be very welcome!

enter image description here

Upvotes: 1

Views: 14098

Answers (1)

knedlsepp
knedlsepp

Reputation: 6084

Explanation:

Your call to meshgrid is exactly what you want. You just have to generate the data matrix that corresponds to the entries of th and r. Your data depends on the values of r, and as this is a matrix of increasing values in the rows,

r = [rho(1), rho(1), ..., rho(1);
     rho(2), rho(2), ..., rho(2);
     rho(3), rho(3), ..., rho(3);
     ...                        ];

you will need to transform your colors data, so they correspond to this matrix. This can be done via repmat(data(:), 1, size(r,2)), which generates a matrix, so that the ith row only contains the value data(i):

[data(1), data(1), ..., data(1);
 data(2), data(2), ..., data(2);
 data(3), data(3), ..., data(3);
 ...                           ];

This way the values correspond to the radius given by the rows of r.

Final result:

Your final script will look like this:

%% Example data
x = (2:0.1:8);
data = sin(x);

%% Shift the values towards the center, so you get a circle instead of an annulus.
rho = x-min(x);

%% Your meshgrid generation code.
theta = (0:0.5:90)*pi/180;
[th, r] = meshgrid(theta, rho);

%% Plotting the values
surf(min(x)+r.*cos(th), ...
     min(x)+r.*sin(th), ...
     repmat(data(:), 1, size(th,2)), ...
     'linestyle', 'none'); 
view(2);
axis equal tight;
colorbar;

and the output will look like this:Output from function

Upvotes: 2

Related Questions