이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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')
![](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
Jeet Shetty
2021년 6월 27일
thanks, are there othr ways to label those nodes with a dot to mark the nodes
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);
Jeet Shetty
2021년 7월 7일
how would i apply it to the actual mesh as well the blue grid, the same thing
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 (한국어)