-
Notifications
You must be signed in to change notification settings - Fork 8
/
adj_matrix_gui.m
executable file
·176 lines (164 loc) · 5.89 KB
/
adj_matrix_gui.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function adj_matrix_gui(action)
% ADJ_MATRIX_GUI
% Opens a figure. Double click to create a vertex. Single click to
% connect vertices. Right click to delete vertices or edges.
%
% Systems Biology and Evolution Toolbox (SBEToolbox).
% Authors: Kranti Konganti, James Cai.
% (C) Texas A&M University.
%
% $LastChangedDate: 2012-12-28 19:01:43 -0600 (Fri, 28 Dec 2012) $
% $LastChangedRevision: 278 $
% $LastChangedBy: konganti $
%
if nargin == 0
action = 'init';
end
switch action
case 'motion'
line_h = getappdata(gcf,'motionline');
pt = get(gca,'CurrentPoint');
pt = pt(1,:);
xdata = get(line_h,'XData');
ydata = get(line_h,'YData');
xdata(2) = pt(1);
ydata(2) = pt(2);
set(line_h,'XData',xdata,'YData',ydata)
case 'down'
button = get(gcf,'SelectionType');
switch button
case 'normal'
h = gco;
fig = gcf;
% First click
if ~isappdata(fig, 'motionline')
if isequal(get(h,'Type'),'text')
pt = get(h,'Position');
hold on
line_h = plot(pt(1), pt(2),'b-.' ...
,'EraseMode','normal');
setappdata(line_h,'startobj',h) % Save start object
hold off
stack_text_on_top
setappdata(fig,'motionline',line_h)
set(fig,'WindowButtonMotionFcn', 'adj_matrix_gui(''motion'')');
end
else
% Second click
line_h = getappdata(fig,'motionline');
if isequal(get(gco,'Type'),'text')
startobj = getappdata(line_h,'startobj');
endobj = gco;
startpt = get(startobj,'Position');
endpt = get(endobj,'Position');
set(line_h,'XData',[startpt(1) endpt(1)] ...
,'YData',[startpt(2) endpt(2)]);
I = round(str2double(get(startobj,'String')));
J = round(str2double(get(endobj,'String')));
Matrix = getappdata(gcf,'Matrix');
Matrix(I,J) = Matrix(I,J)+1;
Matrix(J,I) = Matrix(J,I)+1;
setappdata(gcf,'Matrix',Matrix)
Matrix
else
delete(line_h)
end
rmappdata(gcf,'motionline')
set(fig,'WindowButtonMotionFcn', '');
end
case 'open'
pt = get(gca,'CurrentPoint');
pt = pt(1,:);
hold on
n = 1+length(findobj(get(gca,'Children'),'Type','text'));
h = text(pt(1),pt(2),num2str(n) ...
,'Color','r','FontWeight','bold');
hold off
if ~isappdata(gcf,'Matrix')
setappdata(gcf,'Matrix',[])
end
Matrix = getappdata(gcf,'Matrix');
Matrix(n,n) = 0;
setappdata(gcf,'Matrix',Matrix)
Matrix
case 'alt'
switch get(gco,'Type')
case 'text'
n = round(str2double(get(gco,'String')));
pt = get(gco,'Position');
handles = get(gca,'Children');
for I=1:length(handles)
h = handles(I);
if isequal(get(h,'Type'),'text')
n2 = round(str2double(get(h,'String')));
if n2 > n
set(h,'String',n2-1)
end
else
xdata = get(h,'XData');
ydata = get(h,'YData');
if (xdata(1) == pt(1) & ydata(1) == pt(2)) ...
| (xdata(2) == pt(1) & ydata(2) == pt(2))
delete(h)
end
end
end
if isappdata(gcf,'Matrix')
Matrix = getappdata(gcf,'Matrix');
Matrix(n,:) = [];
Matrix(:,n) = [];
setappdata(gcf,'Matrix',Matrix)
Matrix
end
delete(gco)
case 'line'
xdata = get(gco,'XData');
ydata = get(gco,'YData');
txt_h = findobj(get(gca,'Children'),'Type','text');
for K=1:length(txt_h)
h = txt_h(K);
pt = get(h,'Position');
if (xdata(1) == pt(1) & ydata(1) == pt(2))
I = round(str2double(get(h,'String')));
elseif (xdata(2) == pt(1) & ydata(2) == pt(2))
J = round(str2double(get(h,'String')));
end
end
if isappdata(gcf,'Matrix')
Matrix = getappdata(gcf,'Matrix');
Matrix(I,J) = Matrix(I,J)-1;
Matrix(J,I) = Matrix(J,I)-1;
setappdata(gcf,'Matrix',Matrix)
Matrix
end
delete(gco)
end % End object switch
end % End button switch
case 'keypress'
ESC = 27;
switch get(gcf,'CurrentCharacter')
case ESC
if isappdata(gcf,'motionline')
line_h = getappdata(gcf,'motionline');
delete(line_h)
rmappdata(gcf,'motionline')
end
set(gcf,'WindowButtonMotionFcn', '');
end
case 'init'
fig = figure('BackingStore', 'on', 'IntegerHandle', 'off', 'Name', 'Adjacency Matrix' ...
,'NumberTitle', 'off', 'MenuBar', 'none', 'DoubleBuffer','on');
ax = axes;
title('Double click to create vertex. Single click to connect. Right click to delete')
xlim([0 10]);
ylim([0 10]);
set(fig,'WindowButtonDownFcn', 'adj_matrix_gui(''down'')');
set(fig,'KeyPressFcn','adj_matrix_gui(''keypress'')')
otherwise
error(['Unknown - ' action])
end % End action switch
function stack_text_on_top
ax = gca;
handles = get(gca,'Children');
txt_h = findobj(handles,'Type','text');
set(gca,'Children',[txt_h; setdiff(handles,txt_h)])