How can i align these numbers to each cell

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

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

Can you show me how ???
[x, y] = meshgrid((1:M)*app.LengthofcellEditField.Value);
thanks, are there othr ways to label those nodes with a dot to mark the nodes
[x, y] = meshgrid(((1:M)+1/2)*app.LengthofcellEditField.Value);
scatter(x, y, '.')
it worked kinda, but the program is not excuting the outer rectangle
rectangle(app.UIAxes,'Position',[0 0 width height],'LineWidth', 5);
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')
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.
yea thank you , it is much cleaner now and this bit "if isunix() && strcmp(getenv('USER'), 'mluser')" is not nessessary if i give values
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)
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')
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.
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')
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')
You tried to "fix" the calculation of M, and the positions to text() to, but you messed those up instead.
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)
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 ?
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
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')
yes it works, thank you
Really though the tick interval should be disconnected from Xcoordinate, which is the offset to the beginning of the cells.
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
Yea just wondering if i could get another input , thank you
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
temp = ((1:M)-1)*Lengthofcell;
[x, y] = meshgrid(temp+Xcoordinate, temp+Ycoordinate);
how would i apply it to the actual mesh as well the blue grid, the same thing
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')
yes, understood. thank you, i will run it
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')
Oh cool cool , thanks. i trying to edit that ui table at the moment 😁

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!