wolffd@0: function [x, y, h] = draw_graph(adj, labels, node_t, x, y, varargin) wolffd@0: % DRAW_LAYOUT Draws a layout for a graph wolffd@0: % wolffd@0: % [X, Y, H] = DRAW_LAYOUT(ADJ, ) wolffd@0: % wolffd@0: % Inputs : wolffd@0: % ADJ : Adjacency matrix (source, sink) wolffd@0: % LABELS : Cell array containing labels wolffd@0: % ISBOX : 1 if node is a box, 0 if oval wolffd@0: % X, Y, : Coordinates of nodes on the unit square wolffd@0: % wolffd@0: % Outputs : wolffd@0: % X, Y : Coordinates of nodes on the unit square wolffd@0: % H : Object handles wolffd@0: % wolffd@0: % Usage Example : [x, y] = draw_layout([0 1;0 0], {'Hidden','Visible'}, [1 0]'); wolffd@0: % wolffd@0: % h(i,1) is the text handle - color wolffd@0: % h(i,2) is the circle handle - facecolor wolffd@0: % wolffd@0: % See also MAKE_LAYOUT wolffd@0: wolffd@0: % Change History : wolffd@0: % Date Time Prog Note wolffd@0: % 13-Apr-2000 9:06 PM ATC Created under MATLAB 5.3.1.29215a (R11.1) wolffd@0: % wolffd@0: % ATC = Ali Taylan Cemgil, wolffd@0: % SNN - University of Nijmegen, Department of Medical Physics and Biophysics wolffd@0: % e-mail : cemgil@mbfys.kun.nl wolffd@0: wolffd@0: adj = double(adj); wolffd@0: N = size(adj,1); wolffd@0: if nargin<2, wolffd@0: labels = cellstr(int2str((1:N)')); wolffd@0: end wolffd@0: wolffd@0: if nargin<3, wolffd@0: node_t = zeros(N,1); wolffd@0: else wolffd@0: node_t = node_t(:); wolffd@0: end; wolffd@0: wolffd@0: axis([0 1 0 1]); wolffd@0: set(gca,'XTick',[],'YTick',[],'box','on'); wolffd@0: % axis('square'); wolffd@0: %colormap(flipud(gray)); wolffd@0: wolffd@0: if nargin<4, wolffd@0: [x y] = make_layout(adj); wolffd@0: end; wolffd@0: wolffd@0: idx1 = find(node_t==0); h1 = []; wd1=[]; wolffd@0: if ~isempty(idx1) wolffd@0: [h1 wd1] = textoval(x(idx1), y(idx1), labels(idx1), varargin{:}); wolffd@0: end; wolffd@0: wolffd@0: idx2 = find(node_t~=0); h2 = []; wd2 = []; wolffd@0: if ~isempty(idx2) wolffd@0: [h2 wd2] = textbox(x(idx2), y(idx2), labels(idx2), varargin{:}); wolffd@0: end; wolffd@0: wolffd@0: wd = zeros(size(wd1,1)+size(wd2,1),2); wolffd@0: if ~isempty(idx1), wd(idx1, :) = wd1; end; wolffd@0: if ~isempty(idx2), wd(idx2, :) = wd2; end; wolffd@0: wolffd@0: % bug: this code assumes [x y] is the center of each box and oval, which wolffd@0: % isn't exactly true. wolffd@0: h_edge = []; wolffd@0: for i=1:N, wolffd@0: j = find(adj(i,:)==1); wolffd@0: for k=j, wolffd@0: if x(k)-x(i)==0, wolffd@0: sign = 1; wolffd@0: if y(i)>y(k), alpha = -pi/2; else alpha = pi/2; end; wolffd@0: else wolffd@0: alpha = atan((y(k)-y(i))/(x(k)-x(i))); wolffd@0: if x(i)2, wolffd@0: h = zeros(length(wd),2); wolffd@0: if ~isempty(idx1), wolffd@0: h(idx1,:) = h1; wolffd@0: end; wolffd@0: if ~isempty(idx2), wolffd@0: h(idx2,:) = h2; wolffd@0: end; wolffd@0: end; wolffd@0: wolffd@0: %%%%% wolffd@0: wolffd@0: function [t, wd] = textoval(x, y, str, varargin) wolffd@0: % TEXTOVAL Draws an oval around text objects wolffd@0: % wolffd@0: % [T, WIDTH] = TEXTOVAL(X, Y, STR) wolffd@0: % [..] = TEXTOVAL(STR) % Interactive wolffd@0: % wolffd@0: % Inputs : wolffd@0: % X, Y : Coordinates wolffd@0: % TXT : Strings wolffd@0: % wolffd@0: % Outputs : wolffd@0: % T : Object Handles wolffd@0: % WIDTH : x and y Width of ovals wolffd@0: % wolffd@0: % Usage Example : [t] = textoval('Visit to Asia?'); wolffd@0: % wolffd@0: % wolffd@0: % Note : wolffd@0: % See also TEXTBOX wolffd@0: wolffd@0: % Uses : wolffd@0: wolffd@0: % Change History : wolffd@0: % Date Time Prog Note wolffd@0: % 15-Jun-1998 10:36 AM ATC Created under MATLAB 5.1.0.421 wolffd@0: % 12-Mar-2004 10:00 AM minka Changed placement/sizing. wolffd@0: % wolffd@0: % ATC = Ali Taylan Cemgil, wolffd@0: % SNN - University of Nijmegen, Department of Medical Physics and Biophysics wolffd@0: % e-mail : cemgil@mbfys.kun.nl wolffd@0: wolffd@0: temp = []; wolffd@0: textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'}; wolffd@0: varargin = argfilter(varargin,textProperties); wolffd@0: wolffd@0: if nargin == 1 wolffd@0: str = x; wolffd@0: end wolffd@0: if ~isa(str,'cell') str=cellstr(str); end; wolffd@0: N = length(str); wolffd@0: wd = zeros(N,2); wolffd@0: for i=1:N, wolffd@0: if nargin == 1 wolffd@0: [x, y] = ginput(1); wolffd@0: end wolffd@0: tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:}); wolffd@0: % minka wolffd@0: [ptc wx wy] = draw_oval(tx); wolffd@0: wd(i,:) = [wx wy]; wolffd@0: % draw_oval will paint over the text, so need to redraw it wolffd@0: delete(tx); wolffd@0: tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:}); wolffd@0: temp = [temp; tx ptc]; wolffd@0: end wolffd@0: if nargout>0, t = temp; end; wolffd@0: wolffd@0: %%%%%%%%% wolffd@0: wolffd@0: wolffd@0: function [ptc, wx, wy] = draw_oval(tx, x, y) wolffd@0: % Draws an oval box around a tex object wolffd@0: sz = get(tx,'Extent'); wolffd@0: % minka wolffd@0: wy = 2/3*sz(4); wolffd@0: wx = 2/3*sz(3); wolffd@0: x = sz(1)+sz(3)/2; wolffd@0: y = sz(2)+sz(4)/2; wolffd@0: ptc = ellipse(x, y, wx, wy); wolffd@0: set(ptc, 'FaceColor','w'); wolffd@0: wolffd@0: wolffd@0: %%%%%%%%%%%%% wolffd@0: wolffd@0: function [p] = ellipse(x, y, rx, ry, c) wolffd@0: % ELLIPSE Draws Ellipse shaped patch objects wolffd@0: % wolffd@0: % [

] = ELLIPSE(X, Y, Rx, Ry, C) wolffd@0: % wolffd@0: % Inputs : wolffd@0: % X : N x 1 vector of x coordinates wolffd@0: % Y : N x 1 vector of y coordinates wolffd@0: % Rx, Ry : Radii wolffd@0: % C : Color index wolffd@0: % wolffd@0: % wolffd@0: % Outputs : wolffd@0: % P = Handles of Ellipse shaped path objects wolffd@0: % wolffd@0: % Usage Example : [] = ellipse(); wolffd@0: % wolffd@0: % wolffd@0: % Note : wolffd@0: % See also wolffd@0: wolffd@0: % Uses : wolffd@0: wolffd@0: % Change History : wolffd@0: % Date Time Prog Note wolffd@0: % 27-May-1998 9:55 AM ATC Created under MATLAB 5.1.0.421 wolffd@0: wolffd@0: % ATC = Ali Taylan Cemgil, wolffd@0: % SNN - University of Nijmegen, Department of Medical Physics and Biophysics wolffd@0: % e-mail : cemgil@mbfys.kun.nl wolffd@0: wolffd@0: if (nargin < 2) error('Usage Example : e = ellipse([0 1],[0 -1],[1 0.5],[2 0.5]); '); end; wolffd@0: if (nargin < 3) rx = 0.1; end; wolffd@0: if (nargin < 4) ry = rx; end; wolffd@0: if (nargin < 5) c = 1; end; wolffd@0: wolffd@0: if length(c)==1, c = ones(size(x)).*c; end; wolffd@0: if length(rx)==1, rx = ones(size(x)).*rx; end; wolffd@0: if length(ry)==1, ry = ones(size(x)).*ry; end; wolffd@0: wolffd@0: n = length(x); wolffd@0: p = zeros(size(x)); wolffd@0: t = 0:pi/30:2*pi; wolffd@0: for i=1:n, wolffd@0: px = rx(i)*cos(t)+x(i); wolffd@0: py = ry(i)*sin(t)+y(i); wolffd@0: p(i) = patch(px,py,c(i)); wolffd@0: end; wolffd@0: wolffd@0: if nargout>0, pp = p; end; wolffd@0: wolffd@0: %%%%% wolffd@0: wolffd@0: function [t, wd] = textbox(x,y,str,varargin) wolffd@0: % TEXTBOX Draws A Box around the text wolffd@0: % wolffd@0: % [T, WIDTH] = TEXTBOX(X, Y, STR) wolffd@0: % [..] = TEXTBOX(STR) wolffd@0: % wolffd@0: % Inputs : wolffd@0: % X, Y : Coordinates wolffd@0: % TXT : Strings wolffd@0: % wolffd@0: % Outputs : wolffd@0: % T : Object Handles wolffd@0: % WIDTH : x and y Width of boxes wolffd@0: %% wolffd@0: % Usage Example : t = textbox({'Ali','Veli','49','50'}); wolffd@0: % wolffd@0: % wolffd@0: % Note : wolffd@0: % See also TEXTOVAL wolffd@0: wolffd@0: % Uses : wolffd@0: wolffd@0: % Change History : wolffd@0: % Date Time Prog Note wolffd@0: % 09-Jun-1998 11:43 AM ATC Created under MATLAB 5.1.0.421 wolffd@0: % 12-Mar-2004 10:00 AM minka Changed placement/sizing. wolffd@0: % wolffd@0: % ATC = Ali Taylan Cemgil, wolffd@0: % SNN - University of Nijmegen, Department of Medical Physics and Biophysics wolffd@0: % e-mail : cemgil@mbfys.kun.nl wolffd@0: wolffd@0: temp = []; wolffd@0: textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'}; wolffd@0: varargin = argfilter(varargin,textProperties); wolffd@0: wolffd@0: if nargin == 1 wolffd@0: str = x; wolffd@0: end wolffd@0: if ~isa(str,'cell') str=cellstr(str); end; wolffd@0: N = length(str); wolffd@0: wd = zeros(N,2); wolffd@0: for i=1:N, wolffd@0: if nargin == 1 wolffd@0: [x, y] = ginput(1); wolffd@0: end wolffd@0: tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:}); wolffd@0: % minka wolffd@0: [ptc wx wy] = draw_box(tx); wolffd@0: wd(i,:) = [wx wy]; wolffd@0: % draw_box will paint over the text, so need to redraw it wolffd@0: delete(tx); wolffd@0: tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:}); wolffd@0: temp = [temp; tx ptc]; wolffd@0: end; wolffd@0: wolffd@0: if nargout>0, t = temp; end; wolffd@0: wolffd@0: wolffd@0: function [ptc, wx, wy] = draw_box(tx) wolffd@0: % Draws a box around a text object wolffd@0: sz = get(tx,'Extent'); wolffd@0: % minka wolffd@0: wy = 1/2*sz(4); wolffd@0: wx = 1/2*sz(3); wolffd@0: x = sz(1)+sz(3)/2; wolffd@0: y = sz(2)+sz(4)/2; wolffd@0: ptc = patch([x-wx x+wx x+wx x-wx], [y+wy y+wy y-wy y-wy],'w'); wolffd@0: set(ptc, 'FaceColor','w'); wolffd@0: wolffd@0: wolffd@0: wolffd@0: function args = argfilter(args,keep) wolffd@0: %ARGFILTER Remove unwanted arguments. wolffd@0: % ARGFILTER(ARGS,KEEP), where ARGS = {'arg1',value1,'arg2',value2,...}, wolffd@0: % returns a new argument list where only the arguments named in KEEP are wolffd@0: % retained. KEEP is a character array or cell array of strings. wolffd@0: wolffd@0: % Written by Tom Minka wolffd@0: wolffd@0: if ischar(keep) wolffd@0: keep = cellstr(keep); wolffd@0: end wolffd@0: i = 1; wolffd@0: while i < length(args) wolffd@0: if ~ismember(args{i},keep) wolffd@0: args = args(setdiff(1:length(args),[i i+1])); wolffd@0: else wolffd@0: i = i + 2; wolffd@0: end wolffd@0: end