How can i align these numbers to each cell

2 次查看(过去 30 天)
hey guys , i have tried to number each cell in my grid but i am struggling with the alignment. It wouldbe great if someone could help or give any on how to label the nodes . I am running it on GUI
%generate where each text will go
M=(app.GridWidthEditField.Value-app.XcoordinateEditField.Value)/app.LengthofcellEditField.Value;
[X,Y]=meshgrid(1:M,1:M);
%create the list of text
string = mat2cell(num2str((1:M*M)'),ones(M*M,1));
%insert the labels
hold (app.UIAxes, 'on')
text(app.UIAxes,Y(:),X(:),string,'HorizontalAlignment','center')
%calculte the grid lines
grid = app.XcoordinateEditField.Value:app.LengthofcellEditField.Value:app.GridWidthEditField.Value;
grid1 = [grid;grid];
grid2 = repmat([app.XcoordinateEditField.Value;app.GridWidthEditField.Value],1,length(grid));
%plot the grid lines
plot(app.UIAxes,grid2,grid1,"Color",'b',"Marker","O");
hold (app.UIAxes, 'on')
plot(app.UIAxes,grid1,grid2,"Color",'b',"Marker","O");
hold (app.UIAxes, 'on')

采纳的回答

Walter Roberson
Walter Roberson 2021-6-25
Your X and Y creation are wrong. You need to multiply the 1:M by the width of the cell (and possibly add an offset, especially if you want to center the number in the cell)
  30 个评论
Walter Roberson
Walter Roberson 2021-7-9
Oh, that's why the x and y were exchanged on the text()... and no wonder I was getting unexpected results with the labeling before...
if isunix()
%fake values for demonstration
platewidth = 200;
plateheight = 180;
ax = gca;
GridWidth = 160;
GridHeight = 150;
Xcoordinate = 20;
Ycoordinate = 10;
Lengthofcell = 10;
else
platewidth=app.WidthEditField.Value;
plateheight=app.HeightEditField.Value;
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
GridHeight = app.GridHeightEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value;
Ycoordinate = app.YcoordinateEditField.Value;
Lengthofcell = app.LengthofcellEditField.Value;
end
Gridx = Xcoordinate : Lengthofcell : GridWidth+Xcoordinate;
Gridy = Ycoordinate : Lengthofcell : GridHeight+Ycoordinate;
if Gridx(end) >= platewidth
error('Grid x (%d) + grid width (%d) >= plate width (%d)', Xcoordinate, GridWidth, platewidth);
end
if Gridy(end) >= plateheight
error('Grid y (%d) + grid height (%d) >= plate height (%d)', Ycoordinate, GridHeight, plateheight);
end
Mx = length(Gridx);
My = length(Gridy);
[x, y] = meshgrid(Gridx, Gridy);
%create the list of text
labels = reshape(reshape(string(1:Mx*My), Mx, My).', 1, []);
%insert the labels
scatter(ax, x, y, "O", "MarkerFaceColor", 'r')
hold(ax, 'on')
text(ax, x(:), y(:), labels, 'HorizontalAlignment', 'left', 'verticalalignment', 'bottom')
%calculte the grid lines
grid1x = [Gridx;Gridx];
grid1y = [Gridy(1); Gridy(end)];
grid2x = [Gridx(1); Gridx(end)];
grid2y = [Gridy; Gridy].';
plot(ax, grid1x, grid1y, "Color", 'b');
plot(ax, grid2x, grid2y, "Color", 'b');
rectangle(ax, 'Position', [0 0 platewidth plateheight],'LineWidth', 5);
box(ax, 'on')
grid(ax, 'on')
xlim([0 platewidth])
ylim([0 plateheight])
xticks(0:Xcoordinate:platewidth)
yticks(0:Ycoordinate:plateheight)
hold(ax, 'off')
Jeet Shetty
Jeet Shetty 2021-7-9
Oh cool cool , thanks. i trying to edit that ui table at the moment 😁

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by