How can i align these numbers to each cell
4 次查看(过去 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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/665260/image.jpeg)
采纳的回答
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
2021-6-30
[x, y] = meshgrid(((1:M)+1/2)*app.LengthofcellEditField.Value);
scatter(x, y, '.')
Jeet Shetty
2021-6-30
it worked kinda, but the program is not excuting the outer rectangle ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/670338/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/670338/image.jpeg)
rectangle(app.UIAxes,'Position',[0 0 width height],'LineWidth', 5);
Walter Roberson
2021-6-30
if isunix() && strcmp(getenv('USER'), 'mluser')
%fake values for demonstration
ax = gca;
GridWidth = 90;
Xcoordinate = 10;
Lengthofcell = 10;
else
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value
Lengthofcell = app.LengthofcellEditField.Value
end
M = (GridWidth-Xcoordinate)/Lengthofcell;
Grid = Xcoordinate : Lengthofcell : GridWidth;
[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(ax, 'on')
[x, y] = meshgrid(((1:M)+1/2)*Lengthofcell);
MarkerSize = 50;
scatter(x, y, MarkerSize, 'r.')
text(ax, y(:), x(:), string,'HorizontalAlignment','center')
%calculte the grid lines
grid1 = [Grid;Grid];
grid2 = repmat([Xcoordinate;GridWidth],1,length(Grid));
%plot the grid lines
plot(ax, grid2, grid1, "Color", 'b', "Marker", "O");
plot(ax, grid1, grid2, "Color", 'b', "Marker", "O");
width = GridWidth+Xcoordinate;
height = GridWidth+Xcoordinate;
rectangle(ax, 'Position', [0 0 width height],'LineWidth', 5);
box(ax, 'on')
grid(ax, 'on')
xlim([0 width])
ylim([0 height])
xticks(0:Xcoordinate:width)
yticks(0:Xcoordinate:height)
hold(ax, 'off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/670383/image.png)
Walter Roberson
2021-7-1
The if isunix() && strcmp(getenv('USER'), 'mluser') part is to detect whether the code is running on the online processor, and if so substitute hard-coded values instead of reading values out of the GUI that is not present for the demonstration.
I think the resulting code is cleaner anyhow, not having all those app.* references because shorter local variables were substituted.
Jeet Shetty
2021-7-1
yea thank you , it is much cleaner now and this bit "if isunix() && strcmp(getenv('USER'), 'mluser')" is not nessessary if i give values
Jeet Shetty
2021-7-1
when i plot other values for length of cell, it plots from the oside of the grid . like the coordinates for it are from (0,0)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/670958/image.jpeg)
Walter Roberson
2021-7-1
if isunix() && strcmp(getenv('USER'), 'mluser')
%fake values for demonstration
ax = gca;
GridWidth = 200;
Xcoordinate = 20;
Lengthofcell = 10;
else
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value
Lengthofcell = app.LengthofcellEditField.Value
end
M = (GridWidth-Xcoordinate)/Lengthofcell;
Grid = Xcoordinate : Lengthofcell : GridWidth;
[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(ax, 'on')
[x, y] = meshgrid(((1:M)-1/2)*Lengthofcell+Xcoordinate);
MarkerSize = 50;
scatter(x, y, MarkerSize, 'r.')
text(ax, y(:), x(:), string,'HorizontalAlignment','center')
%calculte the grid lines
grid1 = [Grid;Grid];
grid2 = repmat([Xcoordinate;GridWidth],1,length(Grid));
%plot the grid lines
plot(ax, grid2, grid1, "Color", 'b', "Marker", "O");
plot(ax, grid1, grid2, "Color", 'b', "Marker", "O");
width = GridWidth+Xcoordinate;
height = GridWidth+Xcoordinate;
rectangle(ax, 'Position', [0 0 width height],'LineWidth', 5);
box(ax, 'on')
grid(ax, 'on')
xlim([0 width])
ylim([0 height])
xticks(0:Xcoordinate:width)
yticks(0:Xcoordinate:height)
hold(ax, 'off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/670983/image.png)
Walter Roberson
2021-7-1
The if isunix() && strcmp(getenv('USER'), 'mluser') bit is there to allow me to supply hard-coded values for the purpose of online demonstration, without having to have all your code to builds app. It is not needed for your actual code, but it makes it easier for me to show you the output.
Jeet Shetty
2021-7-1
it is not working , the offset is wierd , im trying to get each node to be marked and numbered
width=app.WidthEditField.Value;
height=app.HeightEditField.Value;
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value;
Lengthofcell = app.LengthofcellEditField.Value;
M = GridWidth /Lengthofcell;
Grid = Xcoordinate : Lengthofcell : GridWidth;
[X, Y] = meshgrid(1:M,1:M);
[x, y] = meshgrid((1:M)*Lengthofcell+Xcoordinate);
%create the list of text
string = mat2cell(num2str((1:M*M)'),ones(M*M,1));
%insert the labels
hold(ax, 'on')
scatter(ax,x, y,"O","MarkerFaceColor",'r')
text(ax, y(:)+2, x(:)+2, string,'HorizontalAlignment','left')
%calculte the grid lines
grid1 = [Grid;Grid];
grid2 = repmat([Xcoordinate;GridWidth],1,length(Grid));
%plot the grid lines
plot(ax, grid2, grid1, "Color", 'b');
plot(ax, grid1, grid2, "Color", 'b');
rectangle(ax, 'Position', [0 0 width height],'LineWidth', 5);
box(ax, 'on')
grid(ax, 'on')
xlim([0 width])
ylim([0 height])
xticks(0:Xcoordinate:width)
yticks(0:Xcoordinate:height)
hold(ax, 'off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/671168/image.jpeg)
Walter Roberson
2021-7-1
if isunix() && strcmp(getenv('USER'), 'mluser')
%fake values for demonstration
platewidth = 200;
plateheight = 200;
ax = gca;
GridWidth = 180;
Xcoordinate = 20;
Lengthofcell = 10;
else
platewidth=app.WidthEditField.Value;
plateheight=app.HeightEditField.Value;
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value;
Lengthofcell = app.LengthofcellEditField.Value;
end
M = (GridWidth-Xcoordinate)/Lengthofcell;
Grid = Xcoordinate : Lengthofcell : GridWidth;
[X, Y] = meshgrid(1:M,1:M);
[x, y] = meshgrid(((1:M)-1/2)*Lengthofcell+Xcoordinate);
%create the list of text
string = mat2cell(num2str((1:M*M)'),ones(M*M,1));
%insert the labels
hold(ax, 'on')
scatter(ax,x, y,"O","MarkerFaceColor",'r')
text(ax, y(:), x(:), string,'HorizontalAlignment','left')
%calculte the grid lines
grid1 = [Grid;Grid];
grid2 = repmat([Xcoordinate;GridWidth],1,length(Grid));
%plot the grid lines
plot(ax, grid2, grid1, "Color", 'b');
plot(ax, grid1, grid2, "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:Xcoordinate:plateheight)
hold(ax, 'off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/671233/image.png)
Walter Roberson
2021-7-1
You tried to "fix" the calculation of M, and the positions to text() to, but you messed those up instead.
Jeet Shetty
2021-7-1
oh but the numbers are being sent on the center of the cell. i was trying to do that for each node( corner of each cell)
Walter Roberson
2021-7-1
Is it the case that each corner is a node? If so then in the case above where there are currently 16 numbers, would it have to become 17 per row, so there would be 17*17 = 289 total numbered nodes?
Where do you want the markers put? At the intersections, I gather?
Where do you want the numbers to go? Above and to the right of the marker? And to confirm, if the grid has 16 interior cells, then it has 17 exterior intersections and you would want 17 numbers, one of which would end up to the right of the enclosed area ?
Jeet Shetty
2021-7-1
yes each corner is a node and put the marker at each corner and intersections the numbers go Above and to the right of the marker
Walter Roberson
2021-7-1
if isunix() && strcmp(getenv('USER'), 'mluser')
%fake values for demonstration
platewidth = 200;
plateheight = 200;
ax = gca;
GridWidth = 180;
Xcoordinate = 20;
Lengthofcell = 10;
else
platewidth=app.WidthEditField.Value;
plateheight=app.HeightEditField.Value;
ax = app.UIAxes;
GridWidth = app.GridWidthEditField.Value;
Xcoordinate = app.XcoordinateEditField.Value;
Lengthofcell = app.LengthofcellEditField.Value;
end
M = (GridWidth-Xcoordinate)/Lengthofcell + 1;
Grid = Xcoordinate : Lengthofcell : GridWidth;
[X, Y] = meshgrid(1:M,1:M);
[x, y] = meshgrid(((1:M)-1)*Lengthofcell+Xcoordinate);
%create the list of text
string = mat2cell(num2str((1:M*M)'),ones(M*M,1));
%insert the labels
hold(ax, 'on')
scatter(ax,x, y,"O","MarkerFaceColor",'r')
text(ax, y(:), x(:), string, 'HorizontalAlignment', 'left', 'verticalalignment', 'bottom')
%calculte the grid lines
grid1 = [Grid;Grid];
grid2 = repmat([Xcoordinate;GridWidth],1,length(Grid));
%plot the grid lines
plot(ax, grid2, grid1, "Color", 'b');
plot(ax, grid1, grid2, "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:Xcoordinate:plateheight)
hold(ax, 'off')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/671758/image.png)
Walter Roberson
2021-7-2
Really though the tick interval should be disconnected from Xcoordinate, which is the offset to the beginning of the cells.
Jeet Shetty
2021-7-5
I am trying to find a command or input box where I can make dynamic inputs to plort on the graph. I want it to take in the value of the no of sensors in rthe GUI and the then open an input box asking for the coorinates of each sensor for example :- No of sensors= 3 an input box asking for 3 sets of X and Y coordinates to plot on the graph No of sensors= 4 an input box asking for 4 sets of X and Y coordinates to plot on the graph
Jeet Shetty
2021-7-7
i had a doubt , if and how we can incoparate the Y coordinate along with the X coordinate to use as the starting point opf the grid
Walter Roberson
2021-7-7
temp = ((1:M)-1)*Lengthofcell;
[x, y] = meshgrid(temp+Xcoordinate, temp+Ycoordinate);
Walter Roberson
2021-7-9
This required a rethinking of what the inputs mean.
Now, GridWidth is the width of the part that is labeled and dotted, and Xcoordinate is the offset of that onto the plate -- so you more or less construct a grid and then drop it somewhere on the plate. The Xcoordinate no longer affects how wide the grid is created. And similarly for the new Ycoordinate and the height.
If you want it to be possible for the grid to end exactly at the plate edges, then change the >= to > in the error test. (Or you could eliminate those tests if you wanted to make it possible to have the grid hang over the edge of the plate.)
It is deliberate that the spacing between the grid and the plate does not have to be the same on the two sides.
If you always wanted the grid to fill the plate except for a margin, then the calculations could be done differently, taking the plate width or height, subtracting twice the margin, and using that as the grid width or height. Just watch out for the case where the cell length does not permit you to get an even distribution.
It would also be reasonable to instead define the number of cells wide and high and the cell length, and calculate everything else from there.
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 = string(1:Mx*My).';
%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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678678/image.png)
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/679308/image.png)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)