Reading a Graph Object from a Text File

I'm reading through this blog post and the author appears to read this text document of contiguous US states and their neighbors as a graph object on which he then calls the function adjacency(S). I'm curious about how to do this myself, as the blog post defines
S = get_state_neighbors;
Without any reference to what get_state_neighbors is. I've tried downloading the text file and using
S=readcell("get_state_neighbors.txt");
however this produces a 53x1 cell array, which the adjacency function cannot be used on.

 채택된 답변

Steven Lord
Steven Lord 2022년 10월 17일
For each line of the text file, use addedge to add an edge between the first state whose abbreviation appears on the line and each of the others that appear on that line.
rawData = 'AL FL GA TN MS'
rawData = 'AL FL GA TN MS'
listOfStates = split(rawData)
listOfStates = 5×1 cell array
{'AL'} {'FL'} {'GA'} {'TN'} {'MS'}
g = graph;
g = addedge(g, listOfStates(1), listOfStates(2:end))
g =
graph with properties: Edges: [4×1 table] Nodes: [5×1 table]
plot(g)

댓글 수: 1

Arman
Arman 2022년 10월 17일
Thank you, I managed to generate my graph by reading the text file line-by-line and appending to the graph!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 10월 17일

댓글:

2022년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by