Borrar filtros
Borrar filtros

problem in using meshgrid and how to show the variation of a parameter in the field

4 visualizaciones (últimos 30 días)
Hello
my code considers a mesh using gridmesh command:
% Define the vertices
v1 = [0, 0];
v2 = [20, 0];
v3 = [30, -10];
v4 = [50, -10];
v5 = [50, -20];
v6 = [0, -20];
% Create a matrix with the vertices as rows
vertices = [v1; v2; v3; v4; v5; v6; v1];
x1=0;
x2=50;
y1=0;
y2=-20;
npoints_x=51;
npoints_y=21;
% Create a grid of points using the meshgrid function
x_grid = linspace(x1, x2, npoints_x);
y_grid = linspace(y1, y2, npoints_y);
[X, Y] = meshgrid(x_grid, y_grid); %node coordinates surrounding the elements
ZZ = table2array(readtable('c.xlsx','Range','A1:A1000'));
B=(reshape(ZZ,[50,20]))';
Z= flip(B,1);
idx = inpolygon(X,Y,vertices(:,1),vertices(:,2)) ;
idx(10,:)=1;
idx(:,1)=[];
idx(21,:)=[];
Z(~idx) = NaN ;
X(:,1)=[];
X(1,:)=[];
Y(1,:)=[];
Y(:,1)=[];
%N=Z(~isnan(Z));
h = pcolor(X,Y,Z);
h.EdgeColor = 'none' ;
colorbar
%draw the slip surface
hold on
P = table2array(readtable('new.xlsx','Range','A1:B112')); %enter the coordinates of the surface Pj
plot(P(:,1),P(:,2),'r+');
I need this grid of mesh to assign values to the elements between the grids. In fact, Z values (20*50 matrice) belong to the elements while X and Y (both 21*51) are grid coordinates. How can I change this code so it assign elemental values to elements and draw the picture I want?as the picture shows the uppermost layer is missing!
Any guidance is highly appreciated!
  2 comentarios
KSSV
KSSV el 20 de Abr. de 2023
What you want to do with the black points? Question is not quite clear for me.
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor el 20 de Abr. de 2023
Editada: Pooneh Shah Malekpoor el 20 de Abr. de 2023
Please ignore the black curve in the previous figure. let me clearify. Firstly, I generate a grid of mesh: 21*51 grid points which is equivalent to 1000 elements between the grid points (a rectangular plate of 20*50 elements). Among this 1000 elements, I aim to assign values to 755 elements and exculde some of the elements by inpolygon command so the resulting meshed figure be like the one I attached in the question. At the moment, my code misses a row and a column of elements at one end of the meshed figure while there is an extra column of elements at another end as I donot know how to consider elements instead of grid points. The resulting meshed figure should be like the one I attach here:
P.S. the elements are 1 m*1 m . In addition, drawing the red curve is not my issue here. totally, I want to show the variation of a parameter (matrice Z) in the field.
Thanks

Iniciar sesión para comentar.

Respuestas (1)

chicken vector
chicken vector el 20 de Abr. de 2023
Editada: chicken vector el 20 de Abr. de 2023
Your problem is related to the definition itself of pcolor. To quote the documentation:
"The color of each face depends on the color at one of its four surrounding vertices."
This means that an array Z of size 50x20 will produces an image with 49x19 faces.
My suggestion is to use image instead.
Also, be careful when you define the vertices because inpolygon returns the
"Points located inside or on edge of polygonal region"
To compensate for this, you have to add one (or substract when the coordinate is negative) to the coordinates that are not on the boundary, i.e. v3(1), v3(2) and v4(2).
% Define the vertices:
v1 = [0, 0];
v2 = [20, 0];
v3 = [31, -11];
v4 = [50, -11];
v5 = [50, -20];
v6 = [0, -20];
% Create a matrix with the vertices as rows:
vertices = [v1; v2; v3; v4; v5; v6; v1];
% Limits:
x1 = 0;
x2 = 50;
y1 = -20;
y2 = 0;
% Define image data:
Z = flip(reshape(readmatrix('c.xlsx'),50,20)',1);
[X,Y] = meshgrid(1:size(Z,2),-size(Z,1):-1);
% Setting to NaN points out of boundary:
idx = inpolygon(X,Y,vertices(:,1),vertices(:,2));
Z(~idx) = NaN;
% Initialise figure:
figure;
hold on;
% Creates square faces:
axis equal;
% Display image setting NaN as white:
im = image([x1+.5,x2-.5], [y1+.5,y2-.5], Z, 'AlphaData', ~isnan(Z), 'CDataMapping', 'scaled');
% Add colorbar:
colorbar
% Load and plot red curve:
P = readmatrix('new.xlsx');
plot(P(:,1),P(:,2),'r+');
% Set axis limits:
xlim([x1,x2])
ylim([y1,y2])
Result:
  2 comentarios
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor el 21 de Abr. de 2023
Thanks! Is there any way to view the value at each element just by hovering the mouse over it?
chicken vector
chicken vector el 21 de Abr. de 2023
Editada: chicken vector el 21 de Abr. de 2023
You should use uifigure and uiaxes instead.
For scattering the cursor hoovering event you can use:
set(gcf, 'WindowButtonMotionFcn', @(varargin) fcn);
Then you have to retrieve the coordinates on the axis with:
point = ceil(get(gca,'CurrentPoint'));
Finally you can use either text or annotation to show the value.
If you want more help or dig deeper in the subject I suggest you to open a new question because this is already deviating from the original one and it will be more helpful for people having your same problem.
Tag me in there if you want my help.

Iniciar sesión para comentar.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by