Effacer les filtres
Effacer les filtres

How do I change the topology of my nodes?

3 vues (au cours des 30 derniers jours)
Divya
Divya le 26 Mai 2023
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);

Réponses (1)

Amit Dhakite
Amit Dhakite le 7 Juin 2023
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');

Catégories

En savoir plus sur View and Analyze Simulation Results dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by