Create and plot an oriented graph of a circuit from a netlist
Hello,
it should be a ridiculously trivial task, but I have to admit I’ve been stuck on it for a few months. Sadly, I’m not very good at Python either, so I’m coming here.
Assume that I have some circuit like the one below:
I want to read and parse a netlist such that I create a digraph object, which can later be used for testing subgraphs being a spanning tree and alike graph theoretic features. Prsing a netlist posses no difficulty, but it looks like the digraph function does not care about the order in my input cells and when I plot the graph, it is labeled wrongly.
I have spent weeks on it with no result. Can you see a easy solution how to turn it into a graph object and plot it accordingly?
Code below produces obvisouly wrong plot, for instance resistors, while the topoogy seems to be idnetified correctly. Edges/Nodes are mislabeled.
clear
close all
clc
netlist = {
‘R1 N001 0 R’;
‘R2 N002 N001 R’;
‘R3 0 N002 R’;
‘C1 N002 N001 C’;
‘C2 N001 0 C’;
‘C3 N002 0 C’;
‘L1 N002 N001 L’;
‘L2 0 N001 L’;
‘L3 0 N002 L’
};
elements = {};
sourceNodes = {};
targetNodes = {};
labels = {};
for i = 1:length(netlist)
parts = strsplit(netlist{i});
elements{end+1} = parts{1};
sourceNodes{end+1} = parts{2};
targetNodes{end+1} = parts{3};
labels{end+1} = [parts{4} ‘ – ‘ parts{1}];
end
edgeTable = table(sourceNodes’, targetNodes’, labels’, ‘VariableNames’, {‘EndNodes’, ‘EndNodes2’, ‘Label’});
G = digraph(edgeTable.EndNodes, edgeTable.EndNodes2);
G.Edges.Label = edgeTable.Label;
h = plot(G, ‘EdgeLabel’, G.Edges.Label, ‘NodeLabel’, G.Nodes.Name, ‘Layout’, ‘force’);Hello,
it should be a ridiculously trivial task, but I have to admit I’ve been stuck on it for a few months. Sadly, I’m not very good at Python either, so I’m coming here.
Assume that I have some circuit like the one below:
I want to read and parse a netlist such that I create a digraph object, which can later be used for testing subgraphs being a spanning tree and alike graph theoretic features. Prsing a netlist posses no difficulty, but it looks like the digraph function does not care about the order in my input cells and when I plot the graph, it is labeled wrongly.
I have spent weeks on it with no result. Can you see a easy solution how to turn it into a graph object and plot it accordingly?
Code below produces obvisouly wrong plot, for instance resistors, while the topoogy seems to be idnetified correctly. Edges/Nodes are mislabeled.
clear
close all
clc
netlist = {
‘R1 N001 0 R’;
‘R2 N002 N001 R’;
‘R3 0 N002 R’;
‘C1 N002 N001 C’;
‘C2 N001 0 C’;
‘C3 N002 0 C’;
‘L1 N002 N001 L’;
‘L2 0 N001 L’;
‘L3 0 N002 L’
};
elements = {};
sourceNodes = {};
targetNodes = {};
labels = {};
for i = 1:length(netlist)
parts = strsplit(netlist{i});
elements{end+1} = parts{1};
sourceNodes{end+1} = parts{2};
targetNodes{end+1} = parts{3};
labels{end+1} = [parts{4} ‘ – ‘ parts{1}];
end
edgeTable = table(sourceNodes’, targetNodes’, labels’, ‘VariableNames’, {‘EndNodes’, ‘EndNodes2’, ‘Label’});
G = digraph(edgeTable.EndNodes, edgeTable.EndNodes2);
G.Edges.Label = edgeTable.Label;
h = plot(G, ‘EdgeLabel’, G.Edges.Label, ‘NodeLabel’, G.Nodes.Name, ‘Layout’, ‘force’); Hello,
it should be a ridiculously trivial task, but I have to admit I’ve been stuck on it for a few months. Sadly, I’m not very good at Python either, so I’m coming here.
Assume that I have some circuit like the one below:
I want to read and parse a netlist such that I create a digraph object, which can later be used for testing subgraphs being a spanning tree and alike graph theoretic features. Prsing a netlist posses no difficulty, but it looks like the digraph function does not care about the order in my input cells and when I plot the graph, it is labeled wrongly.
I have spent weeks on it with no result. Can you see a easy solution how to turn it into a graph object and plot it accordingly?
Code below produces obvisouly wrong plot, for instance resistors, while the topoogy seems to be idnetified correctly. Edges/Nodes are mislabeled.
clear
close all
clc
netlist = {
‘R1 N001 0 R’;
‘R2 N002 N001 R’;
‘R3 0 N002 R’;
‘C1 N002 N001 C’;
‘C2 N001 0 C’;
‘C3 N002 0 C’;
‘L1 N002 N001 L’;
‘L2 0 N001 L’;
‘L3 0 N002 L’
};
elements = {};
sourceNodes = {};
targetNodes = {};
labels = {};
for i = 1:length(netlist)
parts = strsplit(netlist{i});
elements{end+1} = parts{1};
sourceNodes{end+1} = parts{2};
targetNodes{end+1} = parts{3};
labels{end+1} = [parts{4} ‘ – ‘ parts{1}];
end
edgeTable = table(sourceNodes’, targetNodes’, labels’, ‘VariableNames’, {‘EndNodes’, ‘EndNodes2’, ‘Label’});
G = digraph(edgeTable.EndNodes, edgeTable.EndNodes2);
G.Edges.Label = edgeTable.Label;
h = plot(G, ‘EdgeLabel’, G.Edges.Label, ‘NodeLabel’, G.Nodes.Name, ‘Layout’, ‘force’); digraph, circuit, netlist, spanning tree, graph plotting, spice MATLAB Answers — New Questions