Azzera filtri
Azzera filtri

wanted: function for drawing simple network diagrams

1 visualizzazione (ultimi 30 giorni)
We are looking for a simple way to draw diagrams with circles and lines.
Requirements: * creates a figure based on the results of a simulation (i.e. we need a function for matlab, not a separate tool like powerpoint) * a free function, not part of a paid toolbox * draws shapes (circle) and puts a label on each * places the shapes in a specific location * draws lines with arrows between the shapes * allows for the line thickness to be changed * (nice to have) allows for the colors of objects to be changed
Here is a diagram we have created and with the built in methods. We are looking for a function that does this more easily and perhaps more beautifully. http://i55.tinypic.com/1zz3z9e.jpg
We have already investigated these methods: - biograph toolbox (not free) - graphViz (might be ok, but looks complicated) - graphViz-like tools from file exchange (looks limited, i.e. arrow size cannot be changed) : http://www.mathworks.com/matlabcentral/fileexchange/27608-graphviz-like-tools-for-matlab
Thanks for any ideas!

Risposte (3)

Kelly Kearney
Kelly Kearney il 10 Giu 2011
If you're okay with straight edges in the graphs (i.e. no need to route edges around nodes or anything like that), you can probably use gplot along with patch and text to get what you want. The gplot function is annoyingly limited in line customization and doesn't return individual handles for the lines it plots, but a series of calls to it can approximate what you want. I prefer to stay away from rectangle objects since they don't play well with axis limits, and use patch instead to generate my node objects.
Here's an example that more or less replicates your image:
% The data
x = [1 2 2 2 ]; % Node x coordinates
y = [3 3 2 1]; % Node y coordinates
lbl = cellstr(num2str((1:nx)')); % Node labels
r = 0.4; % radius of nodes
nx = length(x);
adj = zeros(nx); % Adjacency matrix for edges,
adj(1,2) = 1; % values specify line width
adj(1,3) = 2;
adj(1,4) = 2;
% The plot
figure;
axes;
hold on;
linewidth = unique(adj(adj>0));
for il = 1:length(linewidth)
h = findall(gca, 'type', 'line');
gplot(adj == linewidth(il), [x' y']);
hnew = setdiff(findall(gca, 'type', 'line'), h);
set(hnew, 'linewidth', linewidth(il));
end
theta = linspace(0, 2*pi, 20)';
xc = bsxfun(@plus, r .* cos(theta), x);
yc = bsxfun(@plus, r .* sin(theta), y);
patch(xc, yc, 'w');
text(x,y,lbl);
axis equal;
  1 Commento
Danielle Ripsman
Danielle Ripsman il 5 Giu 2014
This was exactly what I was looking for, thanks! I did however notice an error (or well it crashed) nx = length(x); is used before it's assigned - it needs to come before line 3, rather than in line 5.

Accedi per commentare.


Sean de Wolski
Sean de Wolski il 10 Giu 2011
Why not just use powerpoint/keynote/open office?
EDIT per clarification
doc rectangle %rectangles/circles etc.
doc annotation %arrows/text
doc patch %triangles
playing with these two functions should get you everything you need.
  1 Commento
Robert
Robert il 10 Giu 2011
Thanks for your answer. We want to automatically generate this diagram based on the results of some other analysis in MATLAB. I have edited my requirements now.

Accedi per commentare.


Daniel Shub
Daniel Shub il 10 Giu 2011
If the figure does not have to be manipulated in MATLAB after it has been created, I would suggest using MATLAB to write code for PGF/TikZ and then compile with LaTeX. This would give you the automation you need and image quality that is hard to obtain with straight MATLAB.

Categorie

Scopri di più su Graphics Object Properties in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by