problem in using meshgrid and how to show the variation of a parameter in the field
4 次查看(过去 30 天)
显示 更早的评论
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+');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1361188/image.jpeg)
Any guidance is highly appreciated!
2 个评论
回答(1 个)
chicken vector
2023-4-20
编辑:chicken vector
2023-4-20
"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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1362488/image.png)
2 个评论
chicken vector
2023-4-21
编辑:chicken vector
2023-4-21
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!