You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can i align these numbers to each cell
2 views (last 30 days)
Show older comments
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')
Accepted Answer
Walter Roberson
on 25 Jun 2021
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 Comments
Jeet Shetty
on 25 Jun 2021
Can you show me how ???
Walter Roberson
on 25 Jun 2021
[x, y] = meshgrid((1:M)*app.LengthofcellEditField.Value);
Jeet Shetty
on 27 Jun 2021
thanks, are there othr ways to label those nodes with a dot to mark the nodes
Walter Roberson
on 30 Jun 2021
[x, y] = meshgrid(((1:M)+1/2)*app.LengthofcellEditField.Value);
scatter(x, y, '.')
Jeet Shetty
on 30 Jun 2021
it worked kinda, but the program is not excuting the outer rectangle
rectangle(app.UIAxes,'Position',[0 0 width height],'LineWidth', 5);
Walter Roberson
on 30 Jun 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
You tried to "fix" the calculation of M, and the positions to text() to, but you messed those up instead.
Jeet Shetty
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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
on 1 Jul 2021
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')
Jeet Shetty
on 2 Jul 2021
yes it works, thank you
Walter Roberson
on 2 Jul 2021
Really though the tick interval should be disconnected from Xcoordinate, which is the offset to the beginning of the cells.
Jeet Shetty
on 5 Jul 2021
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
Walter Roberson
on 5 Jul 2021
Edited: Walter Roberson
on 5 Jul 2021
Jeet Shetty
on 5 Jul 2021
Yea just wondering if i could get another input , thank you
Jeet Shetty
on 7 Jul 2021
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
on 7 Jul 2021
temp = ((1:M)-1)*Lengthofcell;
[x, y] = meshgrid(temp+Xcoordinate, temp+Ycoordinate);
Jeet Shetty
on 7 Jul 2021
how would i apply it to the actual mesh as well the blue grid, the same thing
Walter Roberson
on 9 Jul 2021
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')
Jeet Shetty
on 9 Jul 2021
yes, understood. thank you, i will run it
Walter Roberson
on 9 Jul 2021
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
on 9 Jul 2021
Oh cool cool , thanks. i trying to edit that ui table at the moment 😁
More Answers (0)
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)