Daniel@0: classdef graphViz4Matlab < handle Daniel@0: % Visualize a graph in a Matlab figure window by specifying an Daniel@0: % adjacency matrix and optionally node labels, descriptions, colors and the Daniel@0: % the layout algorithm. The best layout algorithms require that graphViz be Daniel@0: % installed, available free at . Daniel@0: % Daniel@0: % Matthew Dunham Daniel@0: % University of British Columbia Daniel@0: % Last Updated: April 24, 2010 Daniel@0: % Requires Matlab version 2008a or newer. Daniel@0: % Daniel@0: % Syntax (see more examples below): Daniel@0: % graphViz4Matlab('-adjMat',adj,'-nodeLabels',labels,'-nodeColors',colors); Daniel@0: % Daniel@0: % Self loops are removed and not represented on the graph. Daniel@0: % Daniel@0: % Once the graph is displayed, there are several operations you can perform Daniel@0: % with the mouse. Daniel@0: % (1) Move a single node around. Daniel@0: % (2) Draw a mouse box around several nodes and move them all together. Daniel@0: % (3) Enter a description for a node by double clicking it. Daniel@0: % (4) Shade the node by right clicking it Daniel@0: % (5) Display a node's properties on the console by shift clicking it. Daniel@0: % (6) Increase or decrease the font size Daniel@0: % (7) Increase or decrease the node size Daniel@0: % (8) Tighten the axes and relax the square aspect ratio. Daniel@0: % (9) Ask the current layout algorithm to layout the nodes as though the Daniel@0: % arrows were pointing the other way. This only affects some of the Daniel@0: % layouts. Daniel@0: % (10) Change the layout algorithm and refresh the graph Daniel@0: % Daniel@0: % Additionally, any operation you could do with a regular Matlab figure can Daniel@0: % be done here, e.g. annotating or saving as a pdf. Daniel@0: % Daniel@0: % Options are specified via name value pairs in any order. Daniel@0: % [] denote defaults. Daniel@0: % Daniel@0: % '-adjMat' [example matrix] The adjacency matrix Daniel@0: % Daniel@0: % '-layout' [Gvizlayout if graphViz installed, else Gridlayout] Daniel@0: % A layout object, i.e. Gvizlayout | Gridlayout | Circlelayout Daniel@0: % (See knownLayouts Property) Daniel@0: % Daniel@0: % '-nodeLabels' ['1':'n'] A cell array of labels for the nodes Daniel@0: % Daniel@0: % '-nodeDescriptions' [{}] Longer descriptions for the nodes, displayed when Daniel@0: % double clicking on a node. Daniel@0: % Daniel@0: % '-nodeColors' ['c'] A cell array or n-by-3 matrix specifying colors Daniel@0: % for the nodes. If fewer colors than nodes are specified, Daniel@0: % the specified colors are reused and cycled through. Daniel@0: % Daniel@0: % '-undirected' [false] If true, no arrows are displayed. Daniel@0: % Daniel@0: % '-edgeColors' [] An n-by-3 cell array listing Daniel@0: % {fromName,toName,color} for each row. You can Daniel@0: % list only the n < numel(edges) edges you want to Daniel@0: % color. If you do not label the nodes, graphViz4Matlab Daniel@0: % uses '1','2','3', etc, in which case use these. Daniel@0: % You can specify the text 'all' in place of toName, Daniel@0: % to mean all nodes, i.e. {fromName,'all',color} Daniel@0: % Daniel@0: % Daniel@0: % '-splitLabels' [true] If true, long node labels are split into Daniel@0: % several rows Daniel@0: % Daniel@0: % '-doubleClickFn' (by default, double clicking a node brings up Daniel@0: % an edit box for the node's description, but you Daniel@0: % can pass in a custom function handle. The function Daniel@0: % gets passed the node's label. Daniel@0: % Examples: Daniel@0: % Daniel@0: % adj = rand(5,5) > 0.8; Daniel@0: % labels = {'First','Second','Third','Fourth','Fifth'}; Daniel@0: % colors = {'g','b'}; % will cycle through Daniel@0: % s = graphViz4Matlab('-adjMat',adj,'-nodeLabels',labels,'-nodeColors',colors); Daniel@0: % freeze(s); % convert to an image Daniel@0: % Daniel@0: % If you are only specifying an adjacency matrix, you can omit the Daniel@0: % '-adjMat' name as in graphViz4Matlab(adj). Daniel@0: % Daniel@0: % Calling graphViz4Matlab without any parameters displays an example graph. Daniel@0: % Daniel@0: Daniel@0: Daniel@0: properties(GetAccess = 'public', SetAccess = 'private') Daniel@0: % read only Daniel@0: path = addpath(genpath(fileparts(which(mfilename)))); % automatically adds subdirectories to path Daniel@0: graphVizPath = setupPath(); Daniel@0: nnodes = 0; % The number of nodes Daniel@0: nedges = 0; % The number of edges Daniel@0: currentLayout= []; % The current layout object Daniel@0: layouts = []; % List currently added layout objects Daniel@0: adjMatrix = []; % The adjacency matrix Daniel@0: isvisible = false; % True iff the graph is being displayed Daniel@0: nodeArray = []; % The nodes Daniel@0: edgeArray = []; % The edges Daniel@0: fig = []; % The main window Daniel@0: ax = []; % The main axes Daniel@0: doubleClickFn = []; % function to execute when a user double clicks on a node, (must be a function handle that takes in the node name Daniel@0: selectedNode = []; % The selected node, if any Daniel@0: minNodeSize = []; % A minimum size for the nodes Daniel@0: maxNodeSize = []; % A maximum size for the nodes Daniel@0: undirected = false; % If undirected, arrows are not displayed Daniel@0: flipped = false; % If true, layout is done as though edge directions were reversed. Daniel@0: % (does not affect the logical layout). Daniel@0: knownLayouts = {Gvizlayout ,... % add your own layout here or use Daniel@0: Treelayout ,... % the addLayout() method Daniel@0: Radiallayout,... Daniel@0: Circularlayout,... Daniel@0: Springlayout,... Daniel@0: Circlelayout,... Daniel@0: Gridlayout ,... Daniel@0: Randlayout }; Daniel@0: defaultEdgeColor = [0,0,0];%[20,43,140]/255; Daniel@0: edgeColors; Daniel@0: square = true; % amounts to a the call "axis square" Daniel@0: splitLabels = true; Daniel@0: end Daniel@0: Daniel@0: properties(GetAccess = 'private', SetAccess = 'private') Daniel@0: % These values store the initial values not the current ones. Daniel@0: nodeLabels = {}; Daniel@0: nodeDescriptions = {}; Daniel@0: nodeColors = {}; Daniel@0: end Daniel@0: Daniel@0: properties(GetAccess = 'protected',SetAccess = 'protected') Daniel@0: toolbar; % The button toolbar Daniel@0: layoutButtons; % The layout buttons Daniel@0: fontSize; % last calculated optimal font size Daniel@0: selectedFontSize; % Daniel@0: Daniel@0: previousMouseLocation; % last mouse location relative to the axes Daniel@0: groupSelectionMode = 0; % current stage in a group selection task Daniel@0: groupSelectedNodes; % selected nodes in a group selection Daniel@0: groupSelectedDims; % size of enclosing rectangle of selected nodes Daniel@0: groupSelectedRect; % a bounding rectangle for the selected nodes Daniel@0: end Daniel@0: Daniel@0: methods Daniel@0: Daniel@0: function obj = graphViz4Matlab(varargin) Daniel@0: % graphViz4Matlab constructor Daniel@0: if(~exist('processArgs','file')), error('Requires processArgs() function'); end Daniel@0: obj.addKnownLayouts(); Daniel@0: obj.processInputs(varargin{:}) Daniel@0: obj.addNodes(); Daniel@0: obj.addEdges(); Daniel@0: obj.draw(); Daniel@0: end Daniel@0: Daniel@0: function draw(obj) Daniel@0: % Draw the graph Daniel@0: if(obj.isvisible) Daniel@0: obj.erase() Daniel@0: end Daniel@0: obj.createWindow(); Daniel@0: obj.calculateMinMaxNodeSize(); Daniel@0: obj.layoutNodes(); Daniel@0: obj.displayGraph(); Daniel@0: obj.isvisible = true; Daniel@0: obj.paperCrop(); Daniel@0: end Daniel@0: Daniel@0: function fig = freeze(obj) Daniel@0: % Freeze the current image into a regular Matlab figure Daniel@0: figure(obj.fig); Daniel@0: print tmp.png -dpng -r300 Daniel@0: fig = figure; Daniel@0: image(imread('tmp.png')); Daniel@0: axis off; Daniel@0: delete tmp.png; Daniel@0: close(obj.fig); Daniel@0: end Daniel@0: Daniel@0: function redraw(obj) Daniel@0: % Redraw the graph. (You could also call draw() again but then the Daniel@0: % window is recreated as well and it doesn't look as nice). Daniel@0: if(~obj.isvisible) Daniel@0: obj.draw(); Daniel@0: return; Daniel@0: end Daniel@0: cla(obj.ax); Daniel@0: obj.clearGroupSelection(); Daniel@0: obj.calculateMinMaxNodeSize(); Daniel@0: obj.layoutNodes(); Daniel@0: obj.displayGraph(); Daniel@0: end Daniel@0: Daniel@0: function flip(obj,varargin) Daniel@0: % Have the layout algorithms layout the graph as though the arrows Daniel@0: % were pointing in the opposite direction. The node connectivity Daniel@0: % remains the same and if node one pointed to node 2 before, it Daniel@0: % still does after. This is useful for tree layout, for example to Daniel@0: % turn the tree on its head. Calling it twice flips it back. Daniel@0: obj.flipped = ~obj.flipped; Daniel@0: if(obj.isvisible) Daniel@0: obj.redraw(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function erase(obj) Daniel@0: % Erase the graph but maintain the state so that it can be redrawn. Daniel@0: if(obj.isvisible) Daniel@0: obj.clearGroupSelection(); Daniel@0: delete(obj.fig); Daniel@0: obj.isvisible = false; Daniel@0: Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function nodeSelected(obj,node) Daniel@0: % This function is called by nodes when they are selected by the Daniel@0: % mouse. It should not be called manually. Daniel@0: if(obj.groupSelectionMode == 1) Daniel@0: obj.groupSelectionStage1(); Daniel@0: return; Daniel@0: end Daniel@0: if(~isempty(obj.selectedNode)) Daniel@0: node.deselect(); Daniel@0: obj.selectedNode = []; Daniel@0: return; Daniel@0: end Daniel@0: switch get(obj.fig,'SelectionType') Daniel@0: case 'normal' Daniel@0: obj.singleClick(node); Daniel@0: case 'open' Daniel@0: obj.doubleClick(node); Daniel@0: case 'alt' Daniel@0: obj.rightClick(node); Daniel@0: otherwise Daniel@0: obj.shiftClick(node); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function addLayout(obj,layout) Daniel@0: % Let the graph know about a new layout you have created so that it Daniel@0: % will be available via a toolbar button. The layout object must be Daniel@0: % a descendant of the Abstractlayout class. This method does not have Daniel@0: % to be called for existing layouts, nor does it need to be called Daniel@0: % if you passed the new layout to the constructor or to the Daniel@0: % setLayout() method. It will not add two layouts with the same Daniel@0: % name property. Daniel@0: if(~ismember(layout.name,fieldnames(obj.layouts))) Daniel@0: if(layout.isavailable()) Daniel@0: obj.layouts.(layout.name) = layout; Daniel@0: if(obj.isvisible) Daniel@0: obj.addButtons(); Daniel@0: end Daniel@0: else Daniel@0: warning('graphViz4Matlab:layout','This layout is not available'); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function setLayout(obj,layout) Daniel@0: % Set a new layout algorithm and refresh the graph. Daniel@0: if(layout.isavailable()) Daniel@0: obj.addLayout(layout); Daniel@0: obj.currentLayout = obj.layouts.(layout.name); Daniel@0: obj.redraw(); Daniel@0: else Daniel@0: warning('graphViz4Matlab:layout','Sorry, this layout is not available'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function squareAxes(obj,varargin) Daniel@0: % Toggle the axes from square to normal and vice versa. Daniel@0: obj.clearGroupSelection(); Daniel@0: if(obj.square) Daniel@0: axis(obj.ax,'normal'); Daniel@0: obj.square = false; Daniel@0: else Daniel@0: axis(obj.ax,'square'); Daniel@0: obj.square = true; Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: function tightenAxes(obj,varargin) Daniel@0: % Tighten the axes as much as possible. Daniel@0: obj.clearGroupSelection(); Daniel@0: xpos = vertcat(obj.nodeArray.xpos); Daniel@0: ypos = vertcat(obj.nodeArray.ypos); Daniel@0: r = obj.nodeArray(1).width/2; Daniel@0: axis(obj.ax,[min(xpos)-r,max(xpos)+r,min(ypos)-r,max(ypos)+r]); Daniel@0: axis normal; Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: end % end of public methods Daniel@0: Daniel@0: Daniel@0: methods(Access = 'protected') Daniel@0: Daniel@0: function addKnownLayouts(obj) Daniel@0: % Add all of the known layouts Daniel@0: obj.layouts = struct; Daniel@0: for i=1:numel(obj.knownLayouts) Daniel@0: layout = obj.knownLayouts{i}; Daniel@0: if(layout.isavailable()) Daniel@0: obj.layouts.(layout.name) = layout; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function processInputs(obj,varargin) Daniel@0: % Process the inputs and perform error checking Daniel@0: labels = {'adj', 'adjMatrix', 'adjMat', 'layout', 'nodeLabels', 'nodeDescriptions', 'nodeColors', 'undirected', 'edgeColors', 'splitLabels', 'doubleClickFn'}; Daniel@0: for i=1:numel(varargin) Daniel@0: arg = varargin{i}; Daniel@0: if ~ischar(arg), continue; end Daniel@0: for j = 1:numel(labels) Daniel@0: if strcmpi(arg, labels{i}); Daniel@0: varargin{i} = ['-', arg]; Daniel@0: end Daniel@0: if strcmpi(arg, '-adj') || strcmpi(arg, '-adjMatrix') Daniel@0: varargin{i} = '-adjMat'; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: [adjMatrix, currentLayout, nodeLabels, nodeDescriptions, nodeColors,obj.undirected,obj.edgeColors,obj.splitLabels,obj.doubleClickFn] = processArgs(varargin,... Daniel@0: '-adjMat' , [] ,... Daniel@0: '-layout' , [] ,... Daniel@0: '-nodeLabels' , {} ,... Daniel@0: '-nodeDescriptions' , {} ,... Daniel@0: '-nodeColors' , {} ,... Daniel@0: '-undirected' , false ,... Daniel@0: '-edgeColors' , [] ,... Daniel@0: '-splitLabels' , true ,... Daniel@0: '-doubleClickFn' , [] ); Daniel@0: Daniel@0: Daniel@0: if(~isempty(currentLayout) && ~isavailable(currentLayout)) Daniel@0: currentLayout = []; Daniel@0: end Daniel@0: if(isempty(adjMatrix)) Daniel@0: adjMatrix = [0 0 0 0; 1 0 0 0; 1 1 0 0; 1 1 1 0]; % example graph Daniel@0: end Daniel@0: Daniel@0: if(isempty(currentLayout)) Daniel@0: fields = fieldnames(obj.layouts); Daniel@0: currentLayout = obj.layouts.(fields{1}); Daniel@0: else Daniel@0: obj.addLayout(currentLayout); Daniel@0: end Daniel@0: obj.nnodes = size(adjMatrix,1); Daniel@0: obj.adjMatrix = adjMatrix; Daniel@0: if(isempty(nodeDescriptions)) Daniel@0: nodeDescriptions = repmat({'Enter a description here...'},size(adjMatrix,1),1); Daniel@0: end Daniel@0: obj.nodeDescriptions = nodeDescriptions; Daniel@0: obj.nodeColors = nodeColors; Daniel@0: Daniel@0: if(isempty(nodeLabels)) Daniel@0: nodeLabels = cellfun(@(x)num2str(x),mat2cell(1:obj.nnodes,1,ones(1,obj.nnodes)),'UniformOutput',false); Daniel@0: end Daniel@0: Daniel@0: obj.nodeLabels = nodeLabels; Daniel@0: if(~isequal(numel(obj.nodeLabels),size(adjMatrix,1),size(adjMatrix,2))) Daniel@0: error('graphViz4Matlab:dimMismatch','The number of labels must match the dimensions of adjmatrix.'); Daniel@0: end Daniel@0: obj.currentLayout = currentLayout; Daniel@0: end Daniel@0: Daniel@0: function createWindow(obj) Daniel@0: % Create the main window Daniel@0: obj.fig = figure(floor(1000*rand) + 1000); Daniel@0: set(obj.fig,'Name','graphViz4Matlab',... Daniel@0: 'NumberTitle' ,'off',... Daniel@0: 'Color','w' ,'Toolbar','none'); Daniel@0: obj.createAxes(); Daniel@0: ssize = get(0,'ScreenSize'); Daniel@0: pos = [ssize(3)/2,50,-20+ssize(3)/2,ssize(4)-200]; Daniel@0: set(obj.fig,'Position',pos); Daniel@0: obj.setCallbacks(); Daniel@0: obj.addButtons(); Daniel@0: Daniel@0: end Daniel@0: Daniel@0: function createAxes(obj) Daniel@0: % Create the axes upon which the graph will be displayed. Daniel@0: obj.ax = axes('Parent',obj.fig,'box','on','UserData','main'); Daniel@0: outerpos = get(obj.ax,'OuterPosition'); Daniel@0: axpos = outerpos; Daniel@0: axpos(4) = 0.90; Daniel@0: axpos(2) = 0.03; Daniel@0: axis manual Daniel@0: if(obj.square) Daniel@0: axis square Daniel@0: end Daniel@0: set(obj.ax,'Position',axpos,'XTick',[],'YTick',[],'LineWidth',0.5); Daniel@0: set(obj.ax,'ButtonDownFcn',@obj.axPressed); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: function setCallbacks(obj) Daniel@0: % Set the callback functions for the figure, i.e. functions that Daniel@0: % will be called when the user performs various actions. Daniel@0: set(obj.fig,'ResizeFcn' ,@obj.windowResized); Daniel@0: set(obj.fig,'WindowButtonMotionFcn' ,@obj.mouseMoved); Daniel@0: set(obj.fig,'WindowButtonUpFcn' ,@obj.buttonUp); Daniel@0: set(obj.fig,'DeleteFcn' ,@obj.deleted); Daniel@0: end Daniel@0: Daniel@0: function addNodes(obj) Daniel@0: % Add all of the nodes to the graph structure, but don't display Daniel@0: % them yet. Daniel@0: obj.nodeArray = []; Daniel@0: for i=1:obj.nnodes Daniel@0: newnode = graphViz4MatlabNode(obj.nodeLabels{i}); Daniel@0: newnode.containingGraph = obj; Daniel@0: newnode.showFullLabel = ~obj.splitLabels; Daniel@0: obj.nodeArray = [obj.nodeArray newnode]; Daniel@0: end Daniel@0: obj.addNodeDescriptions(obj.nodeDescriptions); Daniel@0: obj.addNodeColors(obj.nodeColors); Daniel@0: end Daniel@0: Daniel@0: function addNodeDescriptions(obj,nodeDescriptions) Daniel@0: % Add any descriptions to the newly created nodes. Daniel@0: if(~isempty(nodeDescriptions)) Daniel@0: if(numel(nodeDescriptions) == 1) Daniel@0: nodeDescriptions = repmat(nodeDescriptions,obj.nnodes,1); Daniel@0: end Daniel@0: for i=1:obj.nnodes Daniel@0: obj.nodeArray(i).description = nodeDescriptions{i}; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function addNodeColors(obj,nodeColors) Daniel@0: % Shade the nodes according to the specified colors. If too few Daniel@0: % colors are specified, they are cycled through. Daniel@0: if(~isempty(nodeColors)) Daniel@0: if(~iscell(nodeColors)) Daniel@0: nodeColors = mat2cell(nodeColors,ones(1,size(nodeColors,1)),size(nodeColors,2)); Daniel@0: end Daniel@0: if(size(nodeColors,2) > size(nodeColors,1)) Daniel@0: nodeColors = nodeColors'; Daniel@0: end Daniel@0: if(numel(nodeColors) < obj.nnodes) Daniel@0: nodeColors = repmat(nodeColors,ceil(obj.nnodes/numel(nodeColors)),1); Daniel@0: nodeColors = nodeColors(1:obj.nnodes); Daniel@0: end Daniel@0: for i=1:obj.nnodes Daniel@0: obj.nodeArray(i).shade(nodeColors{i}); Daniel@0: end Daniel@0: obj.nodeColors = nodeColors; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function addEdges(obj) Daniel@0: % Add all of the edges to the graph structure, but don't display Daniel@0: % them yet. Daniel@0: if(any(diag(obj.adjMatrix))) Daniel@0: fprintf('\nRemoving Self Loops\n'); Daniel@0: obj.adjMatrix = obj.adjMatrix - diag(diag(obj.adjMatrix)); Daniel@0: end Daniel@0: obj.edgeArray = struct('from',[],'to',[],'arrow',[]); Daniel@0: counter = 1; Daniel@0: for i=1:obj.nnodes Daniel@0: for j=1:obj.nnodes Daniel@0: if(obj.adjMatrix(i,j)) Daniel@0: obj.edgeArray(counter) = struct('from',obj.nodeArray(i),'to',obj.nodeArray(j),'arrow',-1); Daniel@0: obj.nodeArray(i).outedges = [obj.nodeArray(i).outedges,counter]; Daniel@0: obj.nodeArray(j).inedges = [obj.nodeArray(j).inedges,counter]; Daniel@0: counter = counter + 1; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: obj.nedges = counter -1; Daniel@0: end Daniel@0: Daniel@0: function calculateMinMaxNodeSize(obj) Daniel@0: % calculates the maximum and minimum node sizes in data units Daniel@0: SCREEN_PROPORTION_MAX = 1/10; Daniel@0: SCREEN_PROPORTION_MIN = 1/35; Daniel@0: units = get(0,'Units'); Daniel@0: set(0,'Units','pixels'); Daniel@0: screensize = get(0,'ScreenSize'); Daniel@0: set(0,'Units',units); Daniel@0: axunits = get(obj.ax,'Units'); Daniel@0: set(obj.ax,'Units','pixels'); Daniel@0: axsize = get(obj.ax,'Position'); Daniel@0: set(obj.ax,'Units',axunits); Daniel@0: if(screensize(3) < screensize(4)) Daniel@0: dataUnitsPerPixel = abs(diff(xlim))/axsize(3); Daniel@0: obj.minNodeSize = (SCREEN_PROPORTION_MIN*screensize(3))*dataUnitsPerPixel; Daniel@0: obj.maxNodeSize = (SCREEN_PROPORTION_MAX*screensize(3))*dataUnitsPerPixel; Daniel@0: else Daniel@0: dataUnitsPerPixel = abs(diff(ylim))/axsize(4); Daniel@0: obj.minNodeSize = (SCREEN_PROPORTION_MIN*screensize(4))*dataUnitsPerPixel; Daniel@0: obj.maxNodeSize = (SCREEN_PROPORTION_MAX*screensize(4))*dataUnitsPerPixel; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function layoutNodes(obj) Daniel@0: % Layout the nodes and edges according to the current layout Daniel@0: % algorithm. Daniel@0: if(obj.flipped) Daniel@0: adj = obj.adjMatrix'; Daniel@0: else Daniel@0: adj = obj.adjMatrix; Daniel@0: end Daniel@0: obj.currentLayout.dolayout(adj,obj.ax,obj.maxNodeSize); Daniel@0: nodesize = obj.currentLayout.nodeSize(); Daniel@0: locs = obj.currentLayout.centers(); Daniel@0: for i=1:obj.nnodes Daniel@0: node = obj.nodeArray(i); Daniel@0: node.resize(nodesize); Daniel@0: node.move(locs(i,1),locs(i,2)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function displayGraph(obj) Daniel@0: % Display all of the nodes and edges. Daniel@0: cla(obj.ax); Daniel@0: obj.setFontSize(); Daniel@0: for i=1:obj.nnodes Daniel@0: node = obj.nodeArray(i); Daniel@0: node.fontSize = obj.fontSize; Daniel@0: node.draw(obj.ax); Daniel@0: end Daniel@0: displayEdges(obj); Daniel@0: end Daniel@0: Daniel@0: function displayEdges(obj,indices) Daniel@0: % Display or refresh the specified edges. If none specified, all Daniel@0: % are refreshed. Currently only works with round nodes. Daniel@0: figure(obj.fig); Daniel@0: if(nargin < 2) Daniel@0: indices = 1:obj.nedges; Daniel@0: else Daniel@0: indices = unique(indices); Daniel@0: end Daniel@0: for i=1:numel(indices) Daniel@0: edge = obj.edgeArray(indices(i)); Daniel@0: [X,Y,Xarrow,Yarrow] = obj.calcPositions(edge); Daniel@0: if(ishandle(edge.arrow)) Daniel@0: delete(edge.arrow) Daniel@0: end Daniel@0: hold on; Daniel@0: edgeColor = obj.defaultEdgeColor; Daniel@0: if ~isempty(obj.edgeColors) Daniel@0: candidates = obj.edgeColors(findString(edge.from.label,obj.edgeColors(:,1)),:); Daniel@0: if size(candidates,1)==1 && strcmpi(candidates(1,2),'all') Daniel@0: edgeColor = candidates{1,3}; Daniel@0: else Daniel@0: edgeCol = candidates(findString(edge.to.label,candidates(:,2)),3); Daniel@0: if ~isempty(edgeCol); edgeColor = edgeCol{1}; end Daniel@0: end Daniel@0: end Daniel@0: edge.arrow = plot(X,Y,'LineWidth',2,'HitTest','off','Color',edgeColor); Daniel@0: if(~obj.undirected) Daniel@0: arrowHead = obj.displayArrowHead(X,Y,Xarrow,Yarrow,edgeColor); Daniel@0: edge.arrow = [edge.arrow arrowHead]; Daniel@0: end Daniel@0: hold off; Daniel@0: obj.edgeArray(indices(i)) = edge; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function arrowHead = displayArrowHead(obj,X,Y,Xarrow,Yarrow,arrowColor) %#ok Daniel@0: % Displays the arrow head given the appropriate coordinates Daniel@0: % calculated via the calcPositions() function. Daniel@0: Daniel@0: arrowHead = patch('Faces' ,[1,2,3] ,... Daniel@0: 'Vertices' ,[Xarrow(1) Yarrow(1); Xarrow(2) Yarrow(2) ;X(2) Y(2)],... Daniel@0: 'FaceColor' ,arrowColor); Daniel@0: end Daniel@0: Daniel@0: function [X,Y,Xarrow,Yarrow] = calcPositions(obj,edge) Daniel@0: % Helper function for displayEdges() - calculates edge and arrow Daniel@0: % start and end positions in data units. Daniel@0: X = [edge.from.xpos edge.to.xpos]; Daniel@0: Y = [edge.from.ypos edge.to.ypos]; Daniel@0: ratio = (Y(2) - Y(1))/(X(2)-X(1)); Daniel@0: if(isinf(ratio)) Daniel@0: ratio = realmax; Daniel@0: end Daniel@0: % dx: x-distance from node1 center to perimeter in direction of node2 Daniel@0: % dy: y-distance from node1 center to perimeter in direction of node2 Daniel@0: % ddx: x-distance from node1 perimeter to base of arrow head Daniel@0: % ddy: y-distance from node1 perimeter to base of arrow head Daniel@0: % dpx: x-offset away from edge in perpendicular direction, for arrow head Daniel@0: % dpy: y-offset away from edge in perpendicular direction, for arrow head Daniel@0: Daniel@0: arrowSize = obj.maxNodeSize/10; Daniel@0: [dx,dy] = pol2cart(atan(ratio),edge.from.width/2); Daniel@0: [ddx,ddy] = pol2cart(atan(ratio),arrowSize); Daniel@0: ratio = 1/ratio; % now work out perpendicular directions. Daniel@0: if(isinf(ratio)) Daniel@0: ratio = realmax; Daniel@0: end Daniel@0: [dpx dpy] = pol2cart(atan(ratio),arrowSize/2); Daniel@0: ddx = abs(ddx); ddy = abs(ddy); dpx = abs(dpx); dpy = abs(dpy); Daniel@0: dx = abs(dx); dy = abs(dy); Daniel@0: if(X(1) < X(2)) Daniel@0: X(1) = X(1) + dx; X(2) = X(2) - dx; Daniel@0: else Daniel@0: X(1) = X(1) - dx; X(2) = X(2) + dx; Daniel@0: end Daniel@0: if(Y(1) < Y(2)) Daniel@0: Y(1) = Y(1) + dy; Y(2) = Y(2) - dy; Daniel@0: else Daniel@0: Y(1) = Y(1) - dy; Y(2) = Y(2) + dy; Daniel@0: end Daniel@0: if(X(1) <= X(2) && Y(1) <= Y(2)) Daniel@0: Xarrow(1) = X(2) - ddx - dpx; Xarrow(2) = X(2) - ddx + dpx; Daniel@0: Yarrow(1) = Y(2) - ddy + dpy; Yarrow(2) = Y(2) - ddy - dpy; Daniel@0: elseif(X(1) <= X(2) && Y(1) >= Y(2)) Daniel@0: Xarrow(1) = X(2) - ddx - dpx; Xarrow(2) = X(2) - ddx + dpx; Daniel@0: Yarrow(1) = Y(2) + ddy - dpy; Yarrow(2) = Y(2) + ddy + dpy; Daniel@0: elseif(X(1) >= X(2) && Y(1) <= Y(2)) Daniel@0: Xarrow(1) = X(2) + ddx - dpx; Xarrow(2) = X(2) + ddx + dpx; Daniel@0: Yarrow(1) = Y(2) - ddy - dpy; Yarrow(2) = Y(2) - ddy + dpy; Daniel@0: else % (X(1) >= (X(2) && Y(1) >= Y(2)) Daniel@0: Xarrow(1) = X(2) + ddx - dpx; Xarrow(2) = X(2) + ddx + dpx; Daniel@0: Yarrow(1) = Y(2) + ddy + dpy; Yarrow(2) = Y(2) + ddy - dpy; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function addButtons(obj) Daniel@0: % Add user interface buttons. Daniel@0: if(~isempty(obj.toolbar)) Daniel@0: if(ishandle(obj.toolbar)) Daniel@0: delete(obj.toolbar); Daniel@0: obj.toolbar = []; Daniel@0: end Daniel@0: end Daniel@0: obj.toolbar = uitoolbar(obj.fig); Daniel@0: Daniel@0: % button icons Daniel@0: load glicons; Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.decreaseFontSize,... Daniel@0: 'TooltipString' ,'Decrease Font Size',... Daniel@0: 'CData' ,icons.downblue); Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.increaseFontSize,... Daniel@0: 'TooltipString' ,'Increase Font Size',... Daniel@0: 'CData' ,icons.upblue); Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.tightenAxes,... Daniel@0: 'TooltipString' ,'Tighten Axes',... Daniel@0: 'CData' ,icons.expand); Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.flip,... Daniel@0: 'TooltipString' ,'Flip/Reset Layout',... Daniel@0: 'CData' , icons.flip); Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.shrinkNodes,... Daniel@0: 'TooltipString' ,'Decrease Node Size',... Daniel@0: 'CData' , icons.downdarkblue); Daniel@0: Daniel@0: uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback' ,@obj.growNodes,... Daniel@0: 'TooltipString' ,'Increase Node Size',... Daniel@0: 'CData' , icons.updarkblue); Daniel@0: Daniel@0: if(~isempty(obj.layoutButtons)) Daniel@0: for i=1:numel(obj.layoutButtons) Daniel@0: if(ishandle(obj.layoutButtons(i))) Daniel@0: delete(obj.layoutButtons(i)); Daniel@0: end Daniel@0: end Daniel@0: obj.layoutButtons = []; Daniel@0: end Daniel@0: Daniel@0: layoutNames = fieldnames(obj.layouts); Daniel@0: for i=1:numel(layoutNames) Daniel@0: layout = obj.layouts.(layoutNames{i}); Daniel@0: layoutButton = uipushtool(obj.toolbar,... Daniel@0: 'ClickedCallback', @obj.layoutButtonPushed,... Daniel@0: 'TooltipString', layout.shortDescription,... Daniel@0: 'UserData' , layoutNames{i},... Daniel@0: 'Separator' , 'on',... Daniel@0: 'CData' , layout.image); Daniel@0: obj.layoutButtons = [obj.layoutButtons,layoutButton]; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function setFontSize(obj) Daniel@0: % fontsize = obj.maxFontSize; Daniel@0: fontSize = 20; Daniel@0: maxchars = size(char(obj.nodeLabels),2); Daniel@0: width = obj.nodeArray(1).width; Daniel@0: height = obj.nodeArray(1).height; Daniel@0: xpos = -10; ypos = -10; Daniel@0: t = text(xpos,ypos,repmat('g',1,maxchars),... Daniel@0: 'FontUnits' , 'points' ,... Daniel@0: 'Units' , 'data' ,... Daniel@0: 'HorizontalAlignment' , 'center' ,... Daniel@0: 'VerticalAlignment' , 'middle' ,... Daniel@0: 'FontWeight' , 'demi' ,... Daniel@0: 'LineStyle' , 'none' ,... Daniel@0: 'Margin' , 0.01 ,... Daniel@0: 'FontSize' , fontSize ,... Daniel@0: 'Color' , 'w' ); Daniel@0: Daniel@0: extent = get(t,'Extent'); Daniel@0: Daniel@0: while(extent(3) > width || extent(4) > height) Daniel@0: fontSize = fontSize - 1; Daniel@0: if(fontSize < 2), break,end Daniel@0: set(t,'FontSize',fontSize); Daniel@0: extent = get(t,'Extent'); Daniel@0: end Daniel@0: obj.fontSize = fontSize; Daniel@0: Daniel@0: end Daniel@0: Daniel@0: function asp = aspectRatio(obj) Daniel@0: % Return the current aspect ratio of the figure, width/height Daniel@0: Daniel@0: units = get(obj.ax,'Units'); Daniel@0: set(obj.ax,'Units','pixels'); Daniel@0: pos = get(obj.ax,'Position'); Daniel@0: set(obj.ax,'Units',units); Daniel@0: asp = (pos(3)/pos(4)); Daniel@0: Daniel@0: end Daniel@0: Daniel@0: function paperCrop(obj) Daniel@0: % Make the papersize the same as the the figure size. This is Daniel@0: % useful when saving as pdf. Daniel@0: units = get(obj.fig,'Units'); Daniel@0: set(obj.fig,'Units','inches'); Daniel@0: pos = get(obj.fig,'Position'); Daniel@0: set(obj.fig,'Units',units); Daniel@0: set(obj.fig,'PaperPositionMode','auto','PaperSize',pos(3:4)); Daniel@0: end Daniel@0: %% Daniel@0: % Callbacks Daniel@0: Daniel@0: function layoutButtonPushed(obj,buttonPushed,varargin) Daniel@0: % Called when a layout button is pushed. Daniel@0: name = get(buttonPushed,'UserData'); Daniel@0: obj.currentLayout = obj.layouts.(name); Daniel@0: axis square; Daniel@0: obj.redraw; Daniel@0: end Daniel@0: Daniel@0: function windowResized(obj,varargin) Daniel@0: % This function is called whenever the window is resized. It Daniel@0: % redraws the whole graph. Daniel@0: if(obj.isvisible) Daniel@0: obj.redraw; Daniel@0: obj.paperCrop(); Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: function mouseMoved(obj,varargin) Daniel@0: % This function is called whenever the mouse moves within the Daniel@0: % figure. Daniel@0: if(obj.groupSelectionMode == 2) Daniel@0: % Move all of the nodes & rectangle Daniel@0: currentPoint = get(obj.ax,'CurrentPoint'); Daniel@0: xlimits = get(obj.ax,'XLim'); Daniel@0: ylimits = get(obj.ax,'YLim'); Daniel@0: sdims = obj.groupSelectedDims; Daniel@0: xdiff = currentPoint(1,1) - obj.previousMouseLocation(1,1); Daniel@0: ydiff = currentPoint(1,2) - obj.previousMouseLocation(1,2); Daniel@0: Daniel@0: if(xdiff <=0) Daniel@0: xdiff = max(xdiff,(xlimits(1)-sdims(1))); Daniel@0: else Daniel@0: xdiff = min(xdiff,xlimits(2)-sdims(2)); Daniel@0: end Daniel@0: if(ydiff <=0) Daniel@0: ydiff = max(ydiff,(ylimits(1)-sdims(3))); Daniel@0: else Daniel@0: ydiff = min(ydiff,ylimits(2)-sdims(4)); Daniel@0: end Daniel@0: xnodepos = vertcat(obj.groupSelectedNodes.xpos) + xdiff; Daniel@0: ynodepos = vertcat(obj.groupSelectedNodes.ypos) + ydiff; Daniel@0: for i=1:numel(obj.groupSelectedNodes) Daniel@0: obj.groupSelectedNodes(i).move(xnodepos(i),ynodepos(i)); Daniel@0: end Daniel@0: recpos = get(obj.groupSelectedRect,'Position'); Daniel@0: recpos(1) = recpos(1) + xdiff; Daniel@0: recpos(2) = recpos(2) + ydiff; Daniel@0: obj.groupSelectedDims = [recpos(1),recpos(1)+recpos(3),recpos(2),recpos(2)+recpos(4)]; Daniel@0: set(obj.groupSelectedRect,'Position',recpos); Daniel@0: edges = [obj.groupSelectedNodes.inedges,obj.groupSelectedNodes.outedges]; Daniel@0: obj.displayEdges(edges); Daniel@0: obj.previousMouseLocation = currentPoint; Daniel@0: else Daniel@0: if(isempty(obj.selectedNode)), return,end Daniel@0: currentPoint = get(obj.ax,'CurrentPoint'); Daniel@0: x = currentPoint(1,1); y = currentPoint(1,2); Daniel@0: xl = xlim + [obj.selectedNode.width,-obj.selectedNode.width]/2; Daniel@0: yl = ylim + [obj.selectedNode.height,-obj.selectedNode.height]/2; Daniel@0: x = min(max(xl(1),x),xl(2)); Daniel@0: y = min(max(yl(1),y),yl(2)); Daniel@0: obj.selectedNode.move(x,y); Daniel@0: obj.displayEdges([obj.selectedNode.inedges,obj.selectedNode.outedges]); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function buttonUp(obj,varargin) Daniel@0: % This function executes when the mouse button is released. Daniel@0: if(obj.groupSelectionMode == 2) Daniel@0: obj.clearGroupSelection(); Daniel@0: return; Daniel@0: end Daniel@0: if(isempty(obj.selectedNode)),return,end Daniel@0: obj.selectedNode.deselect(); Daniel@0: Daniel@0: obj.selectedNode.useFullLabel = false; Daniel@0: obj.selectedNode.fontSize = obj.selectedFontSize; Daniel@0: obj.selectedNode.redraw(); Daniel@0: obj.selectedNode = []; Daniel@0: set(gcf,'Pointer','arrow'); Daniel@0: end Daniel@0: Daniel@0: function axPressed(obj,varargin) Daniel@0: % Called when the user selects the axes but not a node Daniel@0: switch obj.groupSelectionMode Daniel@0: case 0 % hasn't been selected yet Daniel@0: xpos = vertcat(obj.nodeArray.xpos); Daniel@0: ypos = vertcat(obj.nodeArray.ypos); Daniel@0: p1 = get(obj.ax,'CurrentPoint'); Daniel@0: rbbox; % returns after box drawn Daniel@0: p2 = get(obj.ax,'CurrentPoint'); Daniel@0: xleft = min(p1(1,1),p2(1,1)); Daniel@0: xright = max(p1(1,1),p2(1,1)); Daniel@0: ylower = min(p1(1,2),p2(1,2)); Daniel@0: yupper = max(p1(1,2),p2(1,2)); Daniel@0: selectedX = (xpos <= xright) & (xpos >= xleft); Daniel@0: selectedY = (ypos <= yupper) & (ypos >= ylower); Daniel@0: selected = selectedX & selectedY; Daniel@0: if(~any(selected)),return,end Daniel@0: obj.groupSelectionMode = 1; Daniel@0: obj.groupSelectedNodes = obj.nodeArray(selected); Daniel@0: for i=1:numel(obj.groupSelectedNodes) Daniel@0: node = obj.groupSelectedNodes(i); Daniel@0: node.select(); Daniel@0: node.redraw(); Daniel@0: end Daniel@0: Daniel@0: w = obj.groupSelectedNodes(1).width/2; Daniel@0: h = obj.groupSelectedNodes(1).height/2; Daniel@0: x = vertcat(obj.groupSelectedNodes.xpos); Daniel@0: y = vertcat(obj.groupSelectedNodes.ypos); Daniel@0: minx = min(x)-w; maxx = max(x)+w; miny = min(y)-h; maxy = max(y)+h; Daniel@0: obj.groupSelectedDims = [minx,maxx,miny,maxy]; Daniel@0: obj.groupSelectedRect = rectangle('Position',[minx,miny,maxx-minx,maxy-miny],'LineStyle','--','EdgeColor','r'); Daniel@0: case 1 % nodes selected Daniel@0: obj.groupSelectionStage1(); Daniel@0: case 2 %not ever reached in this function Daniel@0: obj.clearGroupSelection(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function groupSelectionStage1(obj) Daniel@0: % Called after a group of nodes has been selected and the mouse Daniel@0: % button has been pressed somewhere on the axes, (or on a node). Daniel@0: p = get(obj.ax,'CurrentPoint'); Daniel@0: obj.previousMouseLocation = p; Daniel@0: dims = obj.groupSelectedDims; Daniel@0: if(p(1,1) >= dims(1) && p(1,1) <= dims(2) && p(1,2) >= dims(3) && p(1,2) <=dims(4)) Daniel@0: set(gcf,'Pointer','hand'); Daniel@0: obj.groupSelectionMode = 2; Daniel@0: else Daniel@0: obj.clearGroupSelection(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function clearGroupSelection(obj) Daniel@0: % Clear a group selection Daniel@0: if(ishandle(obj.groupSelectedRect)) Daniel@0: delete(obj.groupSelectedRect); Daniel@0: end Daniel@0: obj.groupSelectedRect = []; Daniel@0: for i=1:numel(obj.groupSelectedNodes) Daniel@0: obj.groupSelectedNodes(i).deselect(); Daniel@0: end Daniel@0: obj.groupSelectedNodes = []; Daniel@0: obj.groupSelectedDims = []; Daniel@0: obj.groupSelectionMode = 0; Daniel@0: set(gcf,'Pointer','arrow'); Daniel@0: end Daniel@0: Daniel@0: function deleted(obj,varargin) Daniel@0: % Called when the figure is deleted by the user. Daniel@0: obj.isvisible = false; Daniel@0: obj.clearGroupSelection(); Daniel@0: end Daniel@0: Daniel@0: function singleClick(obj,node) Daniel@0: % Called when a user single clicks on a node. Daniel@0: obj.selectedNode = node; Daniel@0: node.select(); Daniel@0: set(gcf,'Pointer','hand'); Daniel@0: obj.selectedFontSize = node.fontSize; Daniel@0: node.useFullLabel = true; Daniel@0: node.fontSize = max(15,node.fontSize*1.5); Daniel@0: node.redraw(); Daniel@0: end Daniel@0: Daniel@0: function doubleClick(obj,node) Daniel@0: % Called when a user double clicks on a node Daniel@0: if isempty(obj.doubleClickFn) Daniel@0: description = node.description; Daniel@0: if(~iscell(description)) Daniel@0: description = {description}; Daniel@0: end Daniel@0: answer = inputdlg('',node.label,4,description); Daniel@0: if(~isempty(answer)) Daniel@0: node.description = answer; Daniel@0: end Daniel@0: else Daniel@0: obj.doubleClickFn(node.label); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function rightClick(obj,node) %#ok Daniel@0: % Called when a user right clicks on a node Daniel@0: if(node.isshaded) Daniel@0: node.unshade(); Daniel@0: else Daniel@0: node.shade(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function shiftClick(obj,node) %#ok Daniel@0: % Called when a user shift clicks on a node Daniel@0: display(node); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: end Daniel@0: Daniel@0: methods Daniel@0: % Callbacks that can be called by the user programmatically. Daniel@0: function shrinkNodes(obj,varargin) Daniel@0: % Shrink the nodes to 95% of their original size, (but not smaller Daniel@0: % than a calculated minimum. Daniel@0: obj.clearGroupSelection(); Daniel@0: s = max(0.8*obj.nodeArray(1).width,obj.minNodeSize); Daniel@0: obj.nodeArray(1).resize(s); Daniel@0: obj.setFontSize(); Daniel@0: for i=1:obj.nnodes Daniel@0: node = obj.nodeArray(i); Daniel@0: node.fontSize = obj.fontSize; Daniel@0: node.resize(s); Daniel@0: end Daniel@0: obj.displayEdges(); Daniel@0: end Daniel@0: Daniel@0: function growNodes(obj,varargin) Daniel@0: % Grow the nodes to 1/0.95 times their original size, (but not Daniel@0: % larger than a calculated maximum. Daniel@0: obj.clearGroupSelection(); Daniel@0: s = min(obj.nodeArray(1).width/0.8,1.5*obj.maxNodeSize); Daniel@0: obj.nodeArray(1).resize(s); Daniel@0: obj.setFontSize(); Daniel@0: for i=1:obj.nnodes Daniel@0: node = obj.nodeArray(i); Daniel@0: node.fontSize = obj.fontSize; Daniel@0: node.resize(s); Daniel@0: end Daniel@0: obj.displayEdges(); Daniel@0: end Daniel@0: Daniel@0: function increaseFontSize(obj,varargin) Daniel@0: % Increase the fontsize of all the nodes by 0.5 points. Daniel@0: current = get(obj.nodeArray(1).labelhandle,'FontSize'); Daniel@0: newsize = current + 1; Daniel@0: for i=1:numel(obj.nodeArray) Daniel@0: node = obj.nodeArray(i); Daniel@0: node.fontSize = newsize; Daniel@0: node.redraw(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function decreaseFontSize(obj,varargin) Daniel@0: % Decrease the fontsize of all the nodes by 0.5 points. Daniel@0: current = get(obj.nodeArray(1).labelhandle,'FontSize'); Daniel@0: newsize = max(current - 1,1); Daniel@0: for i=1:numel(obj.nodeArray) Daniel@0: node = obj.nodeArray(i); Daniel@0: node.fontSize = newsize; Daniel@0: node.redraw(); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function XY = getNodePositions(obj) Daniel@0: % Return the current positions of the nodes. The bottom left Daniel@0: % corner is [0 0] and the top right is [1 1]. Node positions Daniel@0: % refer to the centre of a node. Daniel@0: XY = zeros(obj.nnodes, 2); Daniel@0: for i=1:obj.nnodes Daniel@0: XY(i, 1) = obj.nodeArray(i).xpos; Daniel@0: XY(i, 2) = obj.nodeArray(i).ypos; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function setNodePositions(obj, XY) Daniel@0: % Programmatically set the node positions Daniel@0: % XY(i, 1) is the xposition of node i, XY(i, 2) is the yposition. Daniel@0: for i=1:obj.nnodes Daniel@0: obj.nodeArray(i).move(XY(i, 1), XY(i, 2)); Daniel@0: end Daniel@0: obj.displayGraph(); Daniel@0: end Daniel@0: Daniel@0: function moveNode(obj, nodeIndex, xpos, ypos) Daniel@0: % Programmatically set a node position. Daniel@0: obj.nodeArray(nodeIndex).move(xpos, ypos); Daniel@0: obj.displayGraph(); Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: end