Tuesday, June 28, 2022

[FIXED] How can I remove the 'fishbowl' effect when creating a square grid on MATLAB?

Issue

I have been trying to create a graph representing a square grid with 8 nodes. I have been using code given by Mathworks here:

n = 8;
A = delsq(numgrid('S',n+2));
G = graph(A,'omitselfloops');
p = plot(G);` 

The plotted result is:

enter image description here

But I just wondered if I could make the image less curved. In order to make the graph look more 'uniform' and have all the edges the same length.


Solution

The graph G contains no coordinates for the nodes, so MATLAB basically has to "guess" where to put them (and does a remarkably good job). You can use the additional argument XData, YData (and ZData) to add coordinates to your nodes (see documentation), so in your case you might want to use e.g meshgrid:

n = 8; 
A = delsq(numgrid('S',n+2)); 
G = graph(A,'omitselfloops'); 
[x,y] = meshgrid(1:n, 1:n);
p = plot(G, 'XData',x(:), 'YData',y(:));


Answered By - flawr
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.