現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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
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
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')
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)
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')
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')
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')
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')
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')
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')
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Geographic Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)