I'm facing problem with graph and digraphs in matlab

Hello everyone, My question is how can we plot graphs using nodes and edges(streets in my case), while I am doing the same it keeps on showing the followingerror....can anyone show me how can I rectify the same....

6 Comments

You could help us to help you by providing your Code instead of Screenshots.
Hello Sir, Thanks for the help, This is what it is showing while I'm trying to plot the graph between nodes and edges of the road network....please help me to sort this outScreenshot (10).png
I repeat:
you could help us to help you by attaching code instead of Screenshots
%This is the script that I used for plotting the graph between the nodes and edges and while doing that
%it keeps on saying the same error that I mentioned in the above screenshots.
r = shaperead('Export_Output.shp');
kk = {{r.coor_x_sam},{r.coor_y_sam}}
kk = kk.';
ss = shaperead('Export_Output_3.shp');
sd = {{ss.START_X},{ss.END_X}}
sd = sd.';
dm = graph(kk,sd);
plot(dm);
Ok,
can you provide the results of this code which are in your workspace? Please save them as .mat-file and attach them. I do not have access to Mapping Toolbox and if i had it would be needed to see the .shp-files. But more important is what you have as variables in workspace.
Sir, below attached is the varaibles in workspace that are generated while running that code please check once.

Sign in to comment.

 Accepted Answer

kk = [r.coor_x_sam; r.coor_y_sam].';
sd = [ss.START_X; ss.END_X] .';
However, these are two different sizes: 2021 entries compared to 1044 entries. Neither one of them contains integers, so neither of them contains vertices indexes. Neither one of them is an adjacency matrix or mass matrix. The units appear to be incompatible: it looks to me as if coor_x_sam is degrees whereas START_X appears to be UMC, and coor_x appears to be UMC. You also are not taking into account any Y coordinates.
I think what you have is map data, not graph data.
Now, it happens to be possible to match coor_x and coor_y against START_X and START_Y, indicating that it would be possible to convert into node numbers to construct a graph; it would then be possible to associate X Y coordinates with each node when you plotted, but that can be a bit of a nuisance.

20 Comments

Yes sir, this is map data and it's not graph data. I'm trying to find the shortest route between any two nodes. For that I'm trying to import that into matlab and plot that as a graph in the matlab.But while doing that the above mentioned errors are araising.
Also you said that both are of different sizes yes both are of different size because one is node and the other is edges(for every edge there will be two nodes that is why the size will be different). I'll be very thankful if you suggest me the same.
I have some other things to do now, so I will write it up later.
Ok sir,will be waiting for your write and please do that as soon as possible because that is very urgent.
When you have "urgent" questions, you should hire a consulting company that can commit to offering you responses within your contracted timeline. I am a one-person organization, and I am assisting a number of other people at the same time, and I need to sleep and eat and relax. I do not offer urgent assistance on a volunteer basis, only on a commercial basis, and I charge a lot for it.
load workspace
SX = [ss.START_X];
SY = [ss.START_Y];
SXY = [SX(:), SY(:)];
EX = [ss.END_X];
EY = [ss.END_Y];
EXY = [EX(:), EY(:)];
CX = [r.coor_x];
CY = [r.coor_y];
CXY = [CX(:), CY(:)];
[tf, startidx] = ismembertol(SXY, CXY, 'byrows',true);
assert(all(tf), 'Some start points do not match r data');
[tf, endidx] = ismembertol(EXY, CXY, 'byrows', true);
assert(all(tf), 'Some end points do not match r data');
numc = length(CX);
dm = graph(startidx, endidx, 1, numc);
plot(dm, 'XData', CX, 'YData', CY);
A lot of the roads do not go anywhere, just connecting two points that have no other connections.
Sir, I'm very much thankful for your write but I'm just a student presently pursuing my bachelor's degree so I'm unable to hire any consulting company.
Then don't get yourself into situations where you need urgent programming assistance from volunteers . Volunteers work on their own time when they feel up to it and when it amuses them to do so.
Ok sir and thank you so much for the help.
%Sir, i have tried using the code for plotting the graph but it doesnot gets
%plotted.Once please check this.I have also attached the workspace for any
%reference.
d = shaperead('Export_Output_nodedata.shp');
km = {{d.coor_x},{d.coor_y}}
km = km.';
sm = shaperead('combined_shapefile.shp');
sl = {{sm.START_X},{sm.END_X}}
sl = sl.';
SX = [sm.START_X];
SY = [sm.START_Y];
SXY = [SX(:), SY(:)];
EX = [sm.END_X];
EY = [sm.END_Y];
EXY = [EX(:), EY(:)];
CX = [d.coor_x];
CY = [d.coor_y];
CXY = [CX(:), CY(:)];
[tf, startidx] = ismembertol(SXY, CXY, 'byrows',true);
assert(all(tf), 'Some start points do not match d data');
[tf, endidx] = ismembertol(EXY, CXY, 'byrows', true);
assert(all(tf), 'Some end points do not match d data');
numc = length(CX);
dm = graph(startidx, endidx, 1, numc);
plot(dm, 'XData', CX, 'YData', CY);
I do not appear to have a copy of those .shp files.
Sir, below attached is the zip file containing the shapefiles.Have a look at them.
The first of those files does not have coor_x or coor_y properties, but you can probably use X and Y instead.
The second of those files does not exist. If you substitute the combined_multi.shp file that you attached, then START_x and START_Y and END_X and END_Y properties are not present. X and Y properties are present, but they cannot be substituted for this purpose because the file does not define lines (lines need start and end locations.)
yes sir, I got it and now can we use any algorithms(like dijkstra's algorithm) for finding the distance between any two nodes?
With these files, you do not have any connection information between nodes.
With the data you had posted first, hardly any of the nodes were connected.
Once you have the x and y coordinates of endpoints of roads, then you can use Euclidean distances:
DXY = sqrt(sum((SXY - EXY).^2,2))
Then instead of
dm = graph(startidx, endidx, 1, numc);
use
dm = graph(startidx, endidx, DXY, numc);
After that you can use https://www.mathworks.com/help/bioinfo/ref/graphallshortestpaths.html to find the graph distance between any two nodes, keeping in mind that most of it will be empty because you have very connected nodes in your graph.
Finding the shortest path between the nodes is unfortunately more work.
Sir, what I'm asking is will there be any possibility of running the code for dijkstra's algorithm on this graph for finding the shortest path between any two nodes. It's not just dijkstra (can I be able to use kruskal or any other algorithm for finding the same)
If that can be done can you please tell me how will i be able to implement those algorithms on the data that I have and where can I find the codes for those algorithms to find the shortestpaths between any two nodes.
Sir, also you are telling that these nodes are not connected to edges(I couldnot understand this point)but by seeing the graph many of the nodes are connected to the edges(I have attached the graph below) also in the data that i provided(2nd time) has x and y coordinates as (START_X,START_Y and END_X,END_Y) once please go through them.
Screenshot (12).png
Sir, is it only applicable for the adjacency matrix which is square matrix or we can apply this for any(in my case) the adjacency matrix is not a square marix.
Adjacency matrices can always be extended to be square by padding with zeros along the shorter side.
Sir, while applying the all-pairs-shortest-path-graph-solver it is not running and it is out with various errors can you give a try on that ?
What error message are you observing?

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!