How do I change the topology of my nodes?

3 次查看(过去 30 天)
Hi,
I want to connect the PSE with multiple PDs in a bus topology like this.
PSE----------------------------------------------------------------------
|| || ||
PD1 PD2 PD3
Any help would be great. Thanks!
This is my code :
% Create connection matrix
connections = zeros(num_nodes + 1);
% Connect each PD to the PSE from the left side
connections(1, 2) = 1;
connections(1, 3) = 1;
connections(1, 4) = 1;
% Create a digraph object
g = digraph(connections);
% Set the node properties
g.Nodes.Name = {'PSE'; 'PD1'; 'PD2'; 'PD3'};
g.Nodes.Type = {'PSE'; 'PD'; 'PD'; 'PD'};
% Define the node positions
nodePositions = [0, 0; 0, -1; 0, -2; 0, -3];
% Define custom markers for PSE and PDs
pseMarker = 's';
pdMarker = 's';
% Plot the graph with customized node markers and positions
figure;
h = plot(g, 'NodeColor', 'r', 'NodeLabel', '', 'MarkerSize', 10, ...
'XData', nodePositions(:, 1), 'YData', nodePositions(:, 2), 'ShowArrows', 'off');
% Customize the node markers
hold on;
for i = 1:num_nodes+1
if i == 1
marker = pseMarker;
else
marker = pdMarker;
end
plot(nodePositions(i, 1), nodePositions(i, 2), marker, 'MarkerSize', 20, 'MarkerFaceColor', 'r');
end
hold off;
% Adjust the axes limits
xLimits = [min(nodePositions(:, 1)) - 1, max(nodePositions(:, 1)) + 1];
yLimits = [min(nodePositions(:, 2)) - 1, max(nodePositions(:, 2)) + 1];
axis([xLimits, yLimits]);
% Add edge labels
edgeLabels = {'PD1'; 'PD2'; 'PD3'};
edgeIdx = find(connections(2:end, 1));
edgeX = nodePositions(edgeIdx, 1);
edgeY = nodePositions(edgeIdx, 2);

回答(1 个)

Amit Dhakite
Amit Dhakite 2023-6-7
Hi Divya,
After analysing your code, I got to know that the "nodePositions" are forming a straight line.
If you want to arrange the nodes in a bus topology, you can change the "nodePositions" as described below:
% Define the number of nodes
num_nodes = 3;
% Create connection matrix
connections = zeros(num_nodes + 1);
% Connect each PD to the PSE from the left side
connections(1, 2) = 1;
connections(1, 3) = 1;
connections(1, 4) = 1;
% Create a digraph
g = digraph(connections);
% Define the node positions
nodePositions = [0, 0; -1, -1; 0, -1; 1, -1];
% Plot the graph with customized node markers and positions
figure;
h = plot(g, 'NodeColor', 'r', 'NodeLabel', '', 'MarkerSize', 10, ...
'XData', nodePositions(:, 1), 'YData', nodePositions(:, 2), 'ShowArrows', 'off');
If you want to represent multiple PSE nodes, then you can consider the following example:
% Create connection matrix
connections = zeros(6);
% Connect each PD to one of the PSE
connections(1, 4) = 1;
connections(2, 5) = 1;
connections(3, 6) = 1;
connections(1, 2) = 1;
connections(2, 3) = 1;
% Create a digraph
g = digraph(connections);
% Define the node positions
nodePositions = [-1, 0; 0, 0; 1, 0; -1, -1; 0, -1; 1, -1];
% Plot the graph with customized node markers and positions
figure;
h = plot(g, 'NodeColor', 'r', 'NodeLabel', '', 'MarkerSize', 10, ...
'XData', nodePositions(:, 1), 'YData', nodePositions(:, 2), 'ShowArrows', 'off');

类别

Help CenterFile Exchange 中查找有关 View and Analyze Simulation Results 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by