problem in using meshgrid and how to show the variation of a parameter in the field
7 views (last 30 days)
Show older comments
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 Comments
Answers (1)
chicken vector
on 20 Apr 2023
Edited: chicken vector
on 20 Apr 2023
"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.
"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 Comments
chicken vector
on 21 Apr 2023
Edited: chicken vector
on 21 Apr 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.
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!