how to form a grid on a rectangle in matlab - matlab

i have four points with that i made a polygon using matlab
x = [2271 -3645 -2267 3645];
y = [-3635 -2259 3639 2254];
figure;
plot([x x(1)],[y y(1)],'r-');
i got a polygon, i need to generate the grid above the rectangle with 50*50 interval
i made a try with zgrid function , but its not giving the result.
i need a 2D grid on that polygon, i need to take the grid points into a file.
please help me to solve this
thanks in advance

To generate a point grid confined to the interior of your polygon you can do the following:
[X,Y]=meshgrid(linspace(min(x),max(x),round((max(x)-min(x))/50)),linspace(min(y),max(y),round((max(y)-min(y))/50)));
isin=inpolygon(X(:),Y(:),[x x(1)],[y y(1)]);
Xin = X(isin);
Yin = Y(isin);
Here [Xin, Yin] contains the coordinates of the grid vertices.

You can compute a grid with the meshgrid matlab's built-in function, and draw it with mesh for example. The mesh coordinates are stored in X and Y.
Here the polygon you defined is plotted on the top of the grid. The following code
x = [2271 -3645 -2267 3645];
y = [-3635 -2259 3639 2254];
outside = 100;
grid_val = 50;
figure('Color','w');
[X,Y] = meshgrid(min(x)-outside:grid_val:max(x)+outside, min(y)-outside:grid_val:max(y)+outside);
hold on;
hm = mesh(X,Y,X*0);
hp = plot([x x(1)],[y y(1)],'r-');
set(hm,'EdgeColor','k')
set(hp,'LineWidth',2)
set(gca,'Visible','off');
gives the following grid (full and zoom)

Related

Creating meshgrid of scattered Cartesian data for plotting on a sphere

I have a set of n=8000 cartesian coordinates X,Y and Z as vectors and also a vector V of same size which I want to use as values to create a heatmap on a sphere.
I saw this link (visualization of scattered data over a sphere surface MATLAB), but I don't understand how I convert this set of data into a meshgrid for plotting using surf.
Almost every example I saw uses meshgrids.
Right now, I am doing by plotting a sphere and then use scatter3 to plot my points as big balls and try to smooth them later. I looks like this:
I would like to get the figure as the plotting of the example in that link, where he uses:
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0; 0 1 1])
axis equal
EDIT:
(Sorry for taking so long to reply, the corona crises kept away from work)
What I am actually doing is:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
surf(aa,bb,cc,ones(m+1,m+1)*min(pop0n))
hold on
colormap jet;
scatter3(x,y,z,400,pop0n/norm(pop0n),'filled');
colorbar
shading interp
The array 'ori' is 8000x3, and contains the x, y and z coordinates of the points I want to plot and pop0n is a 8000 sized vector with the intensities of each coordinate.
My main question is how do I transform my x, y, z and pop0n, that are vectors, into 2D arrays (meshgrid) to use surf?
Because I cannot simply do surf(x,y,z,pop0n) if they are vectors.
Thanks in advance
As David suggested, griddata does the job.
What I did was:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
v = griddata(x,y,z,pop0n,aa,bb,cc,'nearest');
surf(aa,bb,cc,v)
colormap jet;
colorbar
shading interp

MATLAB several surface plots in 3D using matrix

I would like to show several spheres in my 3D spaces, each with a different center location and radius. So I followed the tutorial by Matlab and I get the following result. The only problem is that I might have hundreds of spheres to plot so it won't be possible to write down hundreds of lines. Is there a way to use the surf() function with a matrix input? Maybe surf(x,y,z) with x,y and z matrix? Thanks!
hold on;
[x,y,z] = sphere;
r = 50;
s1 = surf(x*r,y*r,z*r,'FaceAlpha',0.1);
s2 = surf((x+3)*r,(y-2)*r,z*r,'FaceAlpha',0.1); % centered at (3,-2,0)
s3 = surf(x*r,(y+1)*r,(z-3)*r,'FaceAlpha',0.1); % centered at (0,1,-3)
s1.EdgeColor = 'none';
s2.EdgeColor = 'none';
s3.EdgeColor = 'none';
Write it in generic form.... You are almost there:
s = surf((x-cx)*r,(y-cy)*r,(z-cz)*r,'FaceAlpha',0.1);
Now just change cx,cy,cz with loops

plot a 3D matrix of concentrations in matlab with slice

I have a 3D matrix C=51x51x11 dimensions, obtained from a function in a separate script, the x,y,z represent length, depth and height, and the value represent a concentration per x,y,z point. I want to create a slice crossing x and another crossing y showing the difference in concentration by color. I have tried using ngrid and meshgrid but didn't work. may i have some help with this please?
Use slice()
C = randi(1,[51,51,11]);
x= 25; y = 25; z = 5;
sl = slice(C,x,y,z);
Using slice inside a function to make it easy to view in 3d:
function eslice(V,sx,sy,sz)
slice(V,sx,sy,sz)
shading interp
axis equal
axis vis3d
end
This is from my personal library, enjoy.

Smooth Contour Plot in matlab

I want to plot smooth contour plot from X Y Z matrix.
sf = fit([X Y] Z, 'poly23');
plot(sf);
I have not enought smooth curve..
What I need?
You can use the functions such as griddata and csaps. Together they will lead you to the result as smooth as you wish. The first function adds additional points to your data matrix set. The second one makes the result smoother. The example of the code is below. In the example smoothing is done first in X direction and then in Y direction. Try to play around with the resolution and smoothing_parameter (the current set of these parameters should be OK though).
x = min_x:step_x:max_x;
y = min_y:step_y:max_y;
resolution = 10;
xg = min_x:(step_x/resolution):max_x;
yg = min_y:(step_y/resolution):max_y;
[X,Y] = meshgrid(x,y);
[XG,YG] = meshgrid(xg,yg);
smoothing_parameter = 0.02;
fitted = griddata(X,Y,Z,XG,YG,'cubic');
fitted_smoothed_x = csaps(xg,fitted,smoothing_parameter,xg);
fitted_smoothed_xy = csaps(yg,fitted_smoothed_x',smoothing_parameter,yg);
surf(XG,YG,fitted_smoothed_xy');
EDIT: If you want to get just a contour plot, you can do, for example, as presented below. As I don't have the real data, I will use build-in function peaks to generate some.
[X,Y,Z] = peaks(30);
figure
surfc(X,Y,Z)
view([0 90])
zlim([-10 -8])
Here you just look at your contour plot from above being below the surface.

A second matlab figure based on pixel coordinate data on the first figure

I have a imagesc image in which each pixel represent a data vector. The image itself is from a data cube squeezed into 2D matrix. I can use impixelinfo to navigate through the image and get pixel coordinates when inquiring the image. The code to execute this is below,
load data.mat; % data cube of size 512x256x12000
figure; imagesc(squeeze(mean(abs(data),3)))
axis equal; colormap jet;
impixelinfo
What I want to do is to be able to plot the underlying data vector (laying into the 3rd dimension) in a second figure using the pixel coordinates. This second figure should update automatically when I move the cursor in the image.
Any help is highly appreciated. Thank you in advance.
Thanks guys, I found a solution with ButtonDownFcn and posting it here for folks out there with a similar problem.
x = (-10:10); y = x; z = x;
[mx, my, mz] = ndgrid(x,y,z)
r = sqrt(mx.^2 + my.^2 + mz.^2);
figure;
imagesc(squeeze(r(:,:,1)),'ButtonDownFcn', {#test_func,r});
with the following function
function test_func(hObject, eventdata, r);
P = get(gca,'CurrentPoint');
X = round(P(1,1));Y = round(P(1,2));
figure;plot(squeeze(r(X,Y,:)));
end