Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on21/01/2009 16:34:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,1647 @@
1
+function vis_trajgui(trajStruct,arg)
2
+
3
+% VIS_TRAJGUI subfuntion for SOM_TRAJECTORY
4
+%
5
+% This function is the actual GUI called by SOM_TRAJECTORY
6
+% function. 
7
+%
8
+% See also SOM_TRAJECTORY.
9
+
10
+% Contributed code to SOM Toolbox 2.0, February 11th, 2000 by Juha Parhankangas
11
+% Copyright (c) by Juha Parhankangas.
12
+% http://www.cis.hut.fi/projects/somtoolbox/        
13
+
14
+% Version 2.0beta juha 180699
15
+
16
+if nargin == 1
17
+
18
+  sM_h=trajStruct.figure;
19
+
20
+  if size(trajStruct.bmus,1) ~= 1 & size(trajStruct.bmus,2) ~= 1
21
+    fuzzy_traj(trajStruct,[]);
22
+    return;
23
+  end
24
+  
25
+  
26
+  if size(trajStruct.bmus,1) == 1 | size(trajStruct.bmus,2) == 1
27
+
28
+    udata.bmus = trajStruct.bmus;
29
+    udata.a_h=[findobj(get(sM_h,'Children'),'Tag','Uplane');...
30
+	       findobj(get(sM_h,'Children'),'Tag','Cplane')];
31
+    udata.sM_h=trajStruct.figure;
32
+    udata.traj=[];
33
+    data1 = trajStruct.primary_data;
34
+    if ~isempty(trajStruct.primary_names)
35
+      names=trajStruct.primary_names;
36
+    else
37
+      for i=1:size(data1,2)
38
+	names{i,1}=sprintf('Var%d',i);
39
+      end
40
+    end
41
+
42
+    udata.lattice=trajStruct.lattice;
43
+    form = 0.7*vis_patch(udata.lattice);
44
+    udata.msize = trajStruct.msize;
45
+
46
+
47
+    %%%%%%%%%%%%%%%%%%%%%%%%
48
+    %
49
+    % forming a patch object, which is placed above every component plane
50
+    %
51
+    
52
+    
53
+    l = size(form,1);
54
+    
55
+    nx = repmat(form(:,1),1,prod(udata.msize));
56
+    ny = repmat(form(:,2),1,prod(udata.msize));
57
+    
58
+    x=reshape(repmat(1:udata.msize(2),l*udata.msize(1),1),l,prod(udata.msize));
59
+    y=repmat(repmat(1:udata.msize(1),l,1),1,udata.msize(2));
60
+    
61
+    if strcmp(udata.lattice,'hexa')
62
+      t = find(~rem(y(1,:),2));
63
+      x(:,t)=x(:,t)+.5;
64
+    end
65
+    x=x+nx;
66
+    y=y+ny;
67
+    
68
+    colors=reshape(ones(prod(udata.msize),1)*[NaN NaN NaN],...
69
+		   [1 prod(udata.msize) 3]);
70
+    
71
+    set(0,'CurrentFigure',udata.sM_h);
72
+    
73
+    %%%%%%%%%%%%%%%%%%%%%%
74
+    %
75
+    % drawing patch
76
+    %
77
+    % caxis -commands keep the colormap of the original patch unchanged.
78
+    %
79
+    
80
+    for i=1:length(udata.a_h)
81
+      udata.real_patch(i)=get(udata.a_h(i),'Children');
82
+      set(udata.real_patch(i),'ButtonDownFcn',...
83
+			'vis_trajgui([],''click'')');
84
+      subplot(udata.a_h(i));
85
+      v=caxis;
86
+      udata.tmp_patch(i)=patch(x,y,colors,'EdgeColor','none',...
87
+			       'ButtonDownFcn',...
88
+			       'vis_trajgui([],''click'')',...
89
+			       'Tag','TmpPatch');
90
+      caxis(v);
91
+    end
92
+    
93
+    %%%%%%%%%%%%%%%%%%%%
94
+    
95
+    
96
+    
97
+    udata.length_of_traj=length(trajStruct.size);
98
+    udata.size=trajStruct.size;
99
+    udata.color=trajStruct.color;
100
+    udata.poly.x=[];
101
+    udata.poly.y=[];
102
+    udata.poly.h=[];
103
+    udata.new_marks=[];
104
+    udata.all_marks=[];
105
+    udata.d_mark2=[];
106
+    udata.fig1 = figure;
107
+    set(udata.fig1,'KeyPressFcn','vis_trajgui([],''key'')',...
108
+		   'Name','Primary Data');
109
+    
110
+    
111
+    %%%%%%%%%%%%%%%%%%%%
112
+    %
113
+    % making the 'Tools' -menu
114
+    %
115
+    
116
+    udata.m_i=uimenu(udata.fig1,'Label','Trajectoy T&ools');
117
+    udata.m_i(2)=uimenu(udata.m_i,'Label','&Remove Trajectory',...
118
+			'Callback',...
119
+			'vis_trajgui([],''remove_traj'')');
120
+    udata.m_i(3)=uimenu(udata.m_i(1),'Label','&Dye Nodes',...
121
+			'Callback',...
122
+			'vis_trajgui([],''dye_gui'')');
123
+    udata.m_i(4)=uimenu(udata.m_i(1),'Label','&Clear Markers',...
124
+			'Callback',...
125
+			'vis_trajgui([],''clear'')');
126
+    udata.m_i(5)=uimenu(udata.m_i(1),'Label','&Save',...
127
+			'Callback',...
128
+			'vis_trajgui([],''save'')');
129
+    udata.m_i(6)=uimenu(udata.m_i(1),'Label','&Load',...
130
+			'Callback',...
131
+			'vis_trajgui([],''load'')');
132
+
133
+    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134
+    %
135
+    % drawing data components to the figure ....
136
+    %
137
+    % 
138
+    
139
+    if nargin < 5 | isempty(comps) | (isstr(comps) & strcmp(comps,'all'))
140
+      comps = 1:size(data1,2);
141
+    end
142
+    
143
+    x=1:size(data1,1);
144
+    
145
+    for i=1:length(comps)
146
+      subplot(length(comps),1,i);
147
+      udata.h(i)=gca;
148
+      udata.d_mark(i).h=[];
149
+      
150
+      udata.d(i)=plot(x,data1(:,comps(i)),...
151
+		      'ButtonDownFcn',...
152
+		      'vis_trajgui([],''line_down'')');        
153
+      set(gca,'XLim',[1 size(data1,1)],...
154
+	      'XTick',[],...	      
155
+	      'ButtonDownFcn','vis_trajgui([],''line_down'')'); %,...
156
+	      %'YLim',[min(data1(:,comps(i))) max(data1(:,comps(i)))]);
157
+	      
158
+      ylabel(names{comps(i)});
159
+      hold on;
160
+      ymin=get(udata.h(i),'YLim');
161
+      pos=mean(get(udata.h(i),'XLim'));
162
+      udata.l(i) = line([pos pos],[ymin(1) ymin(2)],...
163
+			'Color','red',...
164
+			'ButtonDownFcn',...
165
+			'vis_trajgui([],''down'')');
166
+    end
167
+    udata.text1=[];
168
+    
169
+    udata.fig2=[];
170
+    udata.h2=[];
171
+    udata.l2=[];
172
+    udata.text2=[];
173
+    
174
+    %%%%%%%%%%%%%%%%%%%%%%%%%%%
175
+    %
176
+    % ... and to the figure 2.
177
+    %
178
+    
179
+    
180
+    if ~isempty(trajStruct.secondary_data)
181
+      data2=trajStruct.secondary_data;
182
+      if isempty(trajStruct.secondary_names)
183
+	for i=1:size(data1,2)
184
+	  names2{i,1}=sprintf('Var%d',i);
185
+	end
186
+      else
187
+	names2=trajStruct.secondary_names;
188
+      end
189
+      
190
+      udata.fig2 = figure;
191
+      set(udata.fig2,'Name','Secondary Data');
192
+      set(udata.fig2,'KeyPressFcn',...
193
+		     'vis_trajgui([],''key'')');
194
+      for i=1:size(data2,2)
195
+	subplot(size(data2,2),1,i);
196
+	udata.h2(i) = gca;
197
+	udata.d_mark2(i).h=[];
198
+	udata.d2(i) = plot(x,data2(:,i),...
199
+			   'ButtonDownFcn',...
200
+			   'vis_trajgui([],''line_down'')');
201
+	set(gca,'XLim',[1 size(data1,1)],'XTick',[],'ButtonDownFcn',...
202
+		'vis_trajgui([],[],[],[],[],[],''line_down'')');
203
+	ylabel(names2{i});
204
+	hold on;
205
+	ymin = get(udata.h2(i),'YLim');
206
+	pos = mean(get(udata.h2(i),'XLim'));
207
+	udata.l2(i) = line([pos pos],ymin,'Color','red',...
208
+			   'ButtonDownFcn','vis_trajgui([],''down'')');
209
+      end
210
+    end  
211
+    
212
+    %%%%%%%%%%%%%%%%%%%%%%%%%%
213
+    
214
+    set(udata.fig1,'UserData',udata);
215
+    if ~isempty(udata.fig2);
216
+      tmp.fig1=udata.fig1;
217
+      set(udata.fig2,'UserData',tmp);
218
+    end
219
+    tmp=get(udata.sM_h,'UserData');
220
+    tmp.fig1=udata.fig1;
221
+    set(udata.sM_h,'UserData',tmp);  
222
+    set_numbers(round(pos)); 
223
+    return;
224
+  end
225
+  
226
+end
227
+
228
+%%%%%%%%%%%%%%%%%
229
+%
230
+% if figures have been drawn, the only function calls that may exist
231
+% are the ones that change the state of the application.
232
+%
233
+udata=get(gcf,'UserData');
234
+udata=get(udata.fig1,'UserData');
235
+
236
+switch arg
237
+ case 'fuzzy'
238
+  fuzzy_traj(tS,[]);
239
+  return;
240
+ case 'move_fuzzy'
241
+  fuzzy_traj([],'move');
242
+  return;
243
+ case 'remove_traj'
244
+  remove_traj;
245
+  return;
246
+ case 'line_down'
247
+  line_bdf('down');
248
+  return;
249
+ case 'line_drag'
250
+  line_bdf('drag');
251
+  return;
252
+ case 'line_up'
253
+  line_bdf('up');
254
+  return;
255
+ case 'dye_gui';
256
+  color_gui(udata.fig1);
257
+  return;
258
+ case {'dye','cyan','magenta','yellow','red','green','blue','white','grey'}
259
+  dye_nodes(arg);
260
+  return;
261
+ case 'clear'
262
+  clear_markers;
263
+  return;
264
+ case 'key'
265
+  key_bdf;
266
+  return;   
267
+ case 'click'
268
+  click;
269
+  return;
270
+ case 'save'
271
+  save_data;
272
+  return;
273
+ case 'load'
274
+  load_data;
275
+  return;
276
+end
277
+
278
+
279
+%%%%%%%%%%
280
+%
281
+% lines in the data figure(s) are dragged ...
282
+%
283
+
284
+udata=get(gcf,'UserData');
285
+udata=get(udata.fig1,'UserData');
286
+
287
+
288
+lims=get(gca,'XLim');
289
+x = getfield(get(gca,'CurrentPoint'),{1});  % the location of the line
290
+
291
+if x < lims(1)
292
+  x=lims(1);
293
+elseif x > lims(2)
294
+  x=lims(2);
295
+end
296
+
297
+old = gcf;
298
+
299
+switch arg
300
+ case 'down',...
301
+      
302
+  % mouse button is pressed down above the line
303
+  
304
+  set(gcf,'WindowButtonMotionFcn','vis_trajgui([],''drag'')');
305
+  set(gcf,'WindowButtonUpFcn','vis_trajgui([],''up'')');
306
+  set(udata.l,'EraseMode','xor');
307
+  delete(udata.text1);
308
+  if ~isempty(udata.l2);
309
+    set(udata.l2,'EraseMode','xor');
310
+    delete(udata.text2);
311
+  end
312
+  set(gcf,'Pointer','crosshair');
313
+  
314
+ case 'drag'
315
+  % change the location of the lines
316
+  
317
+  set(0,'CurrentFigure',udata.fig1);
318
+  set(udata.l,'XData',[x x]);
319
+  if ~isempty(udata.fig2)
320
+    set(0,'CurrentFigure',udata.fig2);
321
+    set(udata.l2,'XData',[x x]);
322
+  end
323
+  draw_traj(round(x));
324
+  set(0,'CurrentFigure',old);
325
+ case 'up'
326
+  
327
+  % draw trajectory and set figure to the normal state.
328
+  
329
+  set(udata.l,'EraseMode','normal');
330
+  set(gcf,'Pointer','arrow','WindowButtonMotionFcn','',...
331
+	  'WindowButtonUpFcn','');
332
+  draw_traj(round(x));
333
+  set_numbers(round(x));
334
+end
335
+
336
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337
+
338
+function draw_traj(point)
339
+
340
+udata=get(gcf,'UserData');
341
+udata=get(udata.fig1,'UserData');
342
+color=udata.color;
343
+eMode='normal';
344
+if isstr(udata.color) & strcmp(udata.color,'xor')
345
+  eMode='xor';
346
+  color='black';
347
+end
348
+ind=udata.bmus(point);
349
+[i j] = ind2sub(udata.msize,ind);
350
+if ~mod(i,2)
351
+  j=j+0.5;
352
+end
353
+old = gcf;
354
+set(0,'CurrentFigure',udata.sM_h);
355
+hold on;
356
+if isempty(udata.traj) | length(udata.traj.h) ~= length(udata.a_h)
357
+  
358
+  % trajectory does not exist
359
+  
360
+  for i=1:length(udata.a_h)
361
+    subplot(udata.a_h(i));
362
+    hold on;
363
+    new.h = plot(j,i,'Color',color,'EraseMode',eMode,'LineStyle','none');
364
+    udata.traj.h(i)=new;
365
+    udata.traj.j=j;
366
+    udata.traj.i=i;
367
+  end
368
+else
369
+  if length(udata.traj.j) == udata.length_of_traj
370
+    % if the length of trajectory == ..., 
371
+    udata.traj.j(1) = [];         % the first (the oldest) coordinate pair
372
+    udata.traj.i(1) = [];         % is removed.
373
+  end
374
+  udata.traj.j=[udata.traj.j;j]; % the new point is added to the
375
+  udata.traj.i=[udata.traj.i;i]; % end of coordinate vectors (i and j)
376
+  for i=1:length(udata.a_h)
377
+    subplot(udata.a_h(i));            % remove the existing trajectory
378
+    delete(udata.traj.h(i).h);          % and plot the new one.
379
+    for j=1:length(udata.traj.j)
380
+      udata.traj.h(i).h(j)=plot(udata.traj.j(j),udata.traj.i(j),...
381
+				'Color',color,...
382
+				'EraseMode',eMode,'Marker','o','LineWidth',2,...
383
+				'MarkerSize',udata.size(udata.length_of_traj-j+1),...
384
+				'LineStyle','none');
385
+    end
386
+  end
387
+end 
388
+set(0,'CurrentFigure',udata.fig1);
389
+set(udata.fig1,'UserData',udata);
390
+
391
+
392
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393
+
394
+function set_numbers(x);
395
+
396
+% This function writes the numbers beside of the pointer lines
397
+
398
+
399
+udata=get(gcf,'UserData');
400
+udata=get(udata.fig1,'UserData');
401
+
402
+xlim = get(gca,'XLim');
403
+ylim = get(gca,'YLim');
404
+p = ylim(1) + 0.9*(ylim(2)-ylim(1));
405
+
406
+old = gcf;
407
+set(0,'CurrentFigure',udata.fig1);
408
+
409
+
410
+for i=1:length(udata.h)
411
+  subplot(udata.h(i));
412
+  
413
+  % check if the text is placed to the left side of the line...
414
+  
415
+  if abs(x-xlim(1)) > (abs(x-xlim(2)))
416
+    udata.text1(i)=text(x-1,p,sprintf('%d ->',x),...
417
+                        'VerticalAlignment','top',...
418
+                        'HorizontalAlignment','right',...
419
+                        'FontWeight','demi');
420
+  else
421
+    
422
+    %  or to the right side.
423
+    
424
+    udata.text1(i)=text(x+1,p,sprintf('<- %d',x),...
425
+			'VerticalAlignment','top',...
426
+			'FontWeight','demi');
427
+  end
428
+end
429
+
430
+if ~isempty(udata.fig2)
431
+  set(0,'CurrentFigure',udata.fig2);
432
+  
433
+  for i=1:length(udata.h2)
434
+    subplot(udata.h2(i));
435
+    
436
+    if abs(x-xlim(1)) > (abs(x-xlim(2)))
437
+      udata.text2(i)=text(x-1,p,sprintf('%d ->',x),...
438
+			  'VerticalAlignment','top',...
439
+			  'HorizontalAlignment','right',...
440
+			  'FontWeight','demi');
441
+    else
442
+      udata.text2(i)=text(x+1,p,sprintf('<- %d',x),...
443
+			  'VerticalAlignment','top',...
444
+			  'FontWeight','demi');
445
+    end
446
+  end
447
+end
448
+
449
+set(0,'CurrentFigure',old);
450
+set(udata.fig1,'UserData',udata);
451
+
452
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453
+
454
+function remove_traj()
455
+
456
+% delete trajectory -object from every component plane.
457
+
458
+udata=get(gcf,'UserData');
459
+udata=get(udata.fig1,'UserData');
460
+
461
+if isempty(udata.traj)
462
+  return;
463
+end
464
+
465
+
466
+for i=1:length(udata.traj.h)
467
+  delete(udata.traj.h(i).h);
468
+end
469
+
470
+udata.traj=[];
471
+set(udata.fig1,'UserData',udata);
472
+
473
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474
+
475
+function line_bdf(arg)
476
+
477
+% this function takes care of action when region is selected in the
478
+% data figure.
479
+
480
+
481
+udata=get(gcf,'UserData');
482
+udata=get(udata.fig1,'UserData');
483
+xlim=get(gca,'XLim');
484
+
485
+if ~(any(strcmp('THIS',fieldnames(udata))))
486
+  p = getfield(get(gca,'CurrentPoint'),{1});
487
+else
488
+  p = getfield(get(udata.THIS,'CurrentPoint'),{1});
489
+end
490
+
491
+if p < xlim(1)
492
+  p = xlim(1);
493
+elseif p > xlim(2)
494
+  p = xlim(2);
495
+end
496
+
497
+
498
+
499
+switch arg
500
+ case 'down'
501
+  
502
+  % the mouse button is pressed down, the pointer lines are drawn.
503
+  % and the state of the figure is changed.
504
+  
505
+  udata.THIS=gca;
506
+  set(gcf,'WindowButtonMotionFcn',...
507
+	  'vis_trajgui([],''line_drag'')',...
508
+	  'WindowButtonUpFcn','vis_trajgui([],''line_up'')');
509
+  udata.start_p=p;
510
+  
511
+  old = gcf;
512
+  set(0,'CurrentFigure',udata.fig1);
513
+  
514
+  for i=1:length(udata.h)
515
+    subplot(udata.h(i));
516
+    udata.t_line.h(i)=line([p p],get(gca,'YLim'),'Color','red');
517
+    udata.t_line.h2(i)=line([p p],get(gca,'YLim'),'Color','red',...
518
+			    'EraseMode','xor');
519
+  end
520
+  if ~isempty(udata.h2)
521
+    set(0,'CurrentFigure',udata.fig2);
522
+    for i=1:length(udata.h2)
523
+      subplot(udata.h2(i));
524
+      udata.t_line2.h(i)=line([p p],get(gca,'YLim'),'Color','red');
525
+      udata.t_line2.h2(i)=line([p p],get(gca,'YLim'),'Color','red',...
526
+			       'EraseMode','xor');
527
+    end
528
+  end
529
+  
530
+ case 'drag'
531
+  
532
+  % change the position of the pointer lines
533
+  
534
+  old = gcf;
535
+  set(0,'CurrentFigure',udata.fig1);
536
+  set(udata.t_line.h2,'XData',[p p]);
537
+  if ~isempty(udata.fig2)
538
+    set(0,'CurrentFigure',udata.fig2);
539
+    set(udata.t_line2.h2,'XData',[p p]);
540
+  end
541
+  set(0,'CurrentFigure',old);
542
+ case 'up'
543
+  
544
+  
545
+  % sort the 'points' -vector and draw the markers to the data and nodes
546
+  
547
+  points=sort([round(udata.start_p) round(p)]);
548
+  draw_markers(points(1):points(2));
549
+  udata=get(udata.fig1,'UserData');
550
+  udata.new_marks=unique([udata.new_marks ;(points(1):points(2))']);
551
+  delete([udata.t_line.h2 udata.t_line.h]);
552
+  if ~isempty(udata.fig2)
553
+    delete([udata.t_line2.h2 udata.t_line2.h]);
554
+  end
555
+  set(get(udata.THIS,'Parent'),'WindowButtonMotionFcn','',...
556
+		    'WindowButtonUpFcn','');
557
+  udata=rmfield(udata,'THIS');
558
+end
559
+
560
+set(udata.fig1,'UserData',udata);
561
+
562
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563
+
564
+function draw_markers(x);
565
+
566
+plot2data(x);
567
+plot2plane(x);
568
+
569
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570
+
571
+function plot2data(x);
572
+
573
+% plot black markers to the data figure(s)
574
+
575
+udata=get(gcf,'UserData');
576
+udata=get(udata.fig1,'UserData');
577
+
578
+old = gcf;
579
+
580
+set(0,'CurrentFigure',udata.fig1);
581
+
582
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583
+
584
+% if there already exist points in the positions that are members
585
+% of the set x, then the old points are removed from data figures...
586
+
587
+for i=1:length(udata.d_mark(1).h)
588
+  tmp1 = get(udata.d_mark(1).h(i),'XData');
589
+  tmp2 = setdiff(tmp1,x);
590
+  if length(tmp1) ~= length(tmp2)
591
+    inds=[];
592
+    for j=1:length(tmp2);
593
+      inds=[inds find(tmp2(j)==tmp1)];
594
+    end
595
+    for j=1:length(udata.d_mark)
596
+      ydata=getfield(get(udata.d_mark(j).h(i),'YData'),{inds});
597
+      set(udata.d_mark(j).h(i),'XData',tmp2,'YData',ydata);
598
+    end
599
+    if ~isempty(udata.fig2)
600
+      for j=1:length(udata.d_mark2)
601
+        ydata=getfield(get(udata.d_mark2(j).h(i),'YData'),{inds});
602
+        set(udata.d_mark2(j).h(i),'XData',tmp2,'YData',ydata);
603
+      end
604
+    end
605
+  end
606
+end
607
+
608
+
609
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610
+
611
+% ... and the new ones are plotted.
612
+
613
+for i=1:length(udata.h)
614
+  subplot(udata.h(i));
615
+  h=plot(x,getfield(get(udata.d(i),'YData'),{x}),'oblack',...
616
+	 'ButtonDownFcn',...
617
+	 'vis_trajgui([],''line_down'')');
618
+  udata.d_mark(i).h=[udata.d_mark(i).h;h];
619
+end
620
+
621
+if ~isempty(udata.h2)
622
+  set(0,'CurrentFigure',udata.fig2);
623
+  
624
+  for i=1:length(udata.h2)
625
+    subplot(udata.h2(i));
626
+    h=plot(x,getfield(get(udata.d2(i),'YData'),{x}),'oblack',...
627
+	   'ButtonDownFcn',...
628
+	   'vis_trajgui([],''line_down'')');
629
+    udata.d_mark2(i).h=[udata.d_mark2(i).h;h];
630
+  end
631
+end
632
+
633
+set(0,'CurrentFigure',old);
634
+set(udata.fig1,'UserData',udata);
635
+
636
+
637
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638
+
639
+function plot2plane(x);
640
+
641
+% sets markers to the component planes.
642
+
643
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644
+%
645
+% actually new markers are never plotted, but the color of the patch
646
+% lying above the original component plane patch is changed black in
647
+% the right positions.
648
+
649
+udata=get(gcf,'UserData');
650
+udata=get(udata.fig1,'UserData');
651
+
652
+udata.new_marks=unique([udata.new_marks ;x']);
653
+
654
+for i=1:length(udata.a_h)
655
+  col=get(udata.tmp_patch(i),'FaceVertexCData');
656
+  if length(size(col)) == 3
657
+    col = reshape(col,[size(col,1) 3]);
658
+  end
659
+  for j=1:length(udata.new_marks)
660
+    col(udata.bmus(udata.new_marks(j)),:)=[0 0 0];
661
+  end
662
+  set(udata.tmp_patch(i),'FaceVertexCData',col);
663
+end
664
+
665
+set(udata.fig1,'UserData',udata);
666
+
667
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668
+
669
+function color_gui(fig1)
670
+
671
+% construct the graphical user interface for changing the color of the
672
+% black (marked) nodes.
673
+
674
+
675
+udata=get(fig1,'UserData');
676
+
677
+a = figure('Color',[0.8 0.8 0.8], ...
678
+	   'Name','Colors', ...
679
+	   'PaperType','a4letter', ...
680
+	   'Position',[518 456 120 311], ...
681
+	   'Tag','Fig1');
682
+
683
+udata.c_struct.fig=a;
684
+
685
+b = uicontrol('Parent',a, ...
686
+	      'Units','normalized', ...
687
+	      'BackgroundColor',[0.701961 0.701961 0.701961], ...
688
+	      'Position',[0.0700415 0.28956 0.830492 0.594566], ...
689
+	      'Style','frame', ...
690
+	      'Tag','Frame1');
691
+b = uicontrol('Parent',a, ...
692
+	      'Units','normalized', ...
693
+	      'BackgroundColor',[0.8 0.8 0.8], ...
694
+	      'Position',[0.100059 0.301143 0.770456 0.571399], ...
695
+	      'Style','frame', ...
696
+	      'Tag','Frame2');
697
+
698
+b = uicontrol('Parent',a, ...
699
+	      'Units','normalized', ...
700
+	      'Callback','vis_trajgui([],''cyan'')', ...
701
+	      'Position',[0.130077 0.795326 0.170101 0.0617729], ...
702
+	      'Style','radiobutton', ...
703
+	      'Tag','cyan', ...
704
+	      'Value',1);
705
+
706
+
707
+b = uicontrol('Parent',a, ...
708
+	      'Units','normalized', ...
709
+	      'Callback','vis_trajgui([],''magenta'')', ...
710
+	      'Position',[0.130077 0.733553 0.170101 0.057912], ...
711
+	      'Style','radiobutton', ...
712
+	      'Tag','magenta');
713
+
714
+
715
+b = uicontrol('Parent',a, ...
716
+	      'Units','normalized', ...
717
+	      'Callback','vis_trajgui([],''yellow'')', ...
718
+	      'Position',[0.130077 0.664059 0.170101 0.0617729], ...
719
+	      'Style','radiobutton', ...
720
+	      'Tag','yellow');
721
+
722
+
723
+b = uicontrol('Parent',a, ...
724
+	      'Units','normalized', ...
725
+	      'Callback','vis_trajgui([],''red'')', ...
726
+	      'Position',[0.130077 0.590703 0.170101 0.0617729], ...
727
+	      'Style','radiobutton', ...
728
+	      'Tag','red');
729
+
730
+
731
+b = uicontrol('Parent',a, ...
732
+	      'Units','normalized', ...
733
+	      'Callback','vis_trajgui([],''green'')', ...
734
+	      'Position',[0.130077 0.525068 0.170101 0.057912], ...
735
+	      'Style','radiobutton', ...
736
+	      'Tag','green');
737
+
738
+
739
+b = uicontrol('Parent',a, ...
740
+	      'Units','normalized', ...
741
+	      'Callback','vis_trajgui([],''blue'')', ...
742
+	      'Position',[0.130077 0.455575 0.170101 0.0617729], ...
743
+	      'Style','radiobutton', ...
744
+	      'Tag','blue');
745
+
746
+
747
+b = uicontrol('Parent',a, ...
748
+	      'Units','normalized', ...
749
+	      'Callback','vis_trajgui([],''white'')', ...
750
+	      'Position',[0.130077 0.38608 0.170101 0.0617729], ...
751
+	      'Style','radiobutton', ...
752
+	      'Tag','white');
753
+
754
+
755
+b = uicontrol('Parent',a, ...
756
+	      'Units','normalized', ...
757
+	      'Callback','vis_trajgui([],''grey'')', ...
758
+	      'Position',[0.130077 0.320447 0.170101 0.057912], ...
759
+	      'Style','radiobutton', ...
760
+	      'Tag','grey');
761
+
762
+
763
+b = uicontrol('Parent',a, ...
764
+	      'Units','normalized', ...
765
+	      'BackgroundColor',[0.8 0.8 0.8], ...
766
+	      'FontWeight','demi', ...
767
+	      'HorizontalAlignment','left', ...
768
+	      'Position',[0.32019 0.795326 0.470278 0.0501905], ...
769
+	      'String','Cyan', ...
770
+	      'Style','text', ...
771
+	      'Tag','StaticText1');
772
+b = uicontrol('Parent',a, ...
773
+	      'Units','normalized', ...
774
+	      'BackgroundColor',[0.8 0.8 0.8], ...
775
+	      'FontWeight','demi', ...
776
+	      'HorizontalAlignment','left', ...
777
+	      'Position',[0.32019 0.733553 0.520308 0.0463296], ...
778
+	      'String','Magenta', ...
779
+	      'Style','text', ...
780
+	      'Tag','StaticText2');
781
+b = uicontrol('Parent',a, ...
782
+	      'Units','normalized', ...
783
+	      'BackgroundColor',[0.8 0.8 0.8], ...
784
+	      'FontWeight','demi', ...
785
+	      'HorizontalAlignment','left', ...
786
+	      'Position',[0.32019 0.664059 0.470278 0.0501905], ...
787
+	      'String','Yellow', ...
788
+	      'Style','text', ...
789
+	      'Tag','StaticText3');
790
+b = uicontrol('Parent',a, ...
791
+	      'Units','normalized', ...
792
+	      'BackgroundColor',[0.8 0.8 0.8], ...
793
+	      'FontWeight','demi', ...
794
+	      'HorizontalAlignment','left', ...
795
+	      'Position',[0.32019 0.590703 0.470278 0.0501905], ...
796
+	      'String','Red', ...
797
+	      'Style','text', ...
798
+	      'Tag','StaticText4');
799
+b = uicontrol('Parent',a, ...
800
+	      'Units','normalized', ...
801
+	      'BackgroundColor',[0.8 0.8 0.8], ...
802
+	      'FontWeight','demi', ...
803
+	      'HorizontalAlignment','left', ...
804
+	      'Position',[0.32019 0.525068 0.470278 0.0463296], ...
805
+	      'String','Green', ...
806
+	      'Style','text', ...
807
+	      'Tag','StaticText5');
808
+b = uicontrol('Parent',a, ...
809
+	      'Units','normalized', ...
810
+	      'BackgroundColor',[0.8 0.8 0.8], ...
811
+	      'FontWeight','demi', ...
812
+	      'HorizontalAlignment','left', ...
813
+	      'Position',[0.32019 0.455575 0.470278 0.0463296], ...
814
+	      'String','Blue', ...
815
+	      'Style','text', ...
816
+	      'Tag','StaticText6');
817
+b = uicontrol('Parent',a, ...
818
+	      'Units','normalized', ...
819
+	      'BackgroundColor',[0.8 0.8 0.8], ...
820
+	      'FontWeight','demi', ...
821
+	      'HorizontalAlignment','left', ...
822
+	      'Position',[0.32019 0.38608 0.470278 0.0501905], ...
823
+	      'String','White', ...
824
+	      'Style','text', ...
825
+	      'Tag','StaticText7');
826
+b = uicontrol('Parent',a, ...
827
+	      'Units','normalized', ...
828
+	      'BackgroundColor',[0.8 0.8 0.8], ...
829
+	      'FontWeight','demi', ...
830
+	      'HorizontalAlignment','left', ...
831
+	      'Position',[0.32019 0.320447 0.470278 0.0463296], ...
832
+	      'String','Grey', ...
833
+	      'Style','text', ...
834
+	      'Tag','StaticText8');
835
+b = uicontrol('Parent',a, ...
836
+	      'Units','normalized', ...
837
+	      'Position',[0.0700415 0.146711 0.830492 0.135128], ...
838
+	      'Style','frame', ...
839
+	      'Tag','Frame3');
840
+b = uicontrol('Parent',a, ...
841
+	      'Units','normalized', ...
842
+	      'BackgroundColor',[0.8 0.8 0.8], ...
843
+	      'Position',[0.100059 0.158293 0.770456 0.111963], ...
844
+	      'Style','frame', ...
845
+	      'Tag','Frame4');
846
+b = uicontrol('Parent',a, ...
847
+	      'Units','normalized', ...
848
+	      'BackgroundColor',[0.8 0.8 0.8], ...
849
+	      'FontWeight','demi', ...
850
+	      'HorizontalAlignment','left', ...
851
+	      'Position',[0.130077 0.177597 0.270833 0.0617729], ...
852
+	      'String','RGB', ...
853
+	      'Style','text', ...
854
+	      'Tag','StaticText9');
855
+b = uicontrol('Parent',a, ...
856
+	      'Units','normalized', ...
857
+	      'BackgroundColor',[1 1 1], ...
858
+	      'Position',[0.410243 0.173736 0.420249 0.0810768], ...
859
+	      'Style','edit', ...
860
+	      'Tag','EditText1');
861
+
862
+udata.c_struct.RGB=b;
863
+
864
+b = uicontrol('Parent',a, ...
865
+	      'Units','normalized', ...
866
+	      'Callback','vis_trajgui([],''dye'')', ...
867
+	      'FontWeight','demi', ...
868
+	      'Position',[0.0700415 0.0270256 0.360214 0.0772162], ...
869
+	      'String','OK', ...
870
+	      'Tag','Pushbutton1');
871
+b = uicontrol('Parent',a, ...
872
+	      'Units','normalized', ...
873
+	      'Callback','close gcf', ...
874
+	      'FontWeight','demi', ...
875
+	      'Position',[0.54032 0.0270256 0.360214 0.0772162], ...
876
+	      'String','Close', ...
877
+	      'Tag','Pushbutton2');
878
+
879
+udata.c_struct.color=[0 1 1];
880
+
881
+tmp.fig1=fig1;
882
+set(a,'UserData',tmp);
883
+set(udata.fig1,'UserData',udata);
884
+
885
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886
+
887
+function dye_nodes(arg)
888
+
889
+% takes care of the action, when radiobuttons are pressed 
890
+% (or the RGB value is set) in the color_gui -figure.
891
+% It also handles the starting of dying nodes and plots.
892
+
893
+udata=get(gcf,'UserData');
894
+udata=get(udata.fig1,'UserData');
895
+
896
+
897
+switch arg
898
+ case {'cyan','magenta','yellow','red','green','blue','white','grey'}
899
+  h=findobj(get(gcf,'Children'),'Style','radiobutton');
900
+  set(h,'Value',0);
901
+  set(gcbo,'Value',1);
902
+end
903
+
904
+
905
+switch arg
906
+ case 'cyan'
907
+  RGB = [0 1 1];
908
+ case 'magenta'
909
+  RGB = [1 0 1];
910
+ case 'yellow'
911
+  RGB = [1 1 0];
912
+ case 'red'
913
+  RGB = [1 0 0];
914
+ case 'green'
915
+  RGB = [0 1 0];
916
+ case 'blue'
917
+  RGB = [0 0 1];
918
+ case 'white'
919
+  RGB = [1 1 1];
920
+ case 'grey'
921
+  RGB = [0.4 0.4 0.4];
922
+ case 'dye'
923
+  
924
+  RGB = get(udata.c_struct.RGB,'String');
925
+  if isempty(RGB)
926
+    dye;
927
+    return;
928
+  else
929
+    str1='The value of RGB must be vector containing three scalars';
930
+    str2='between 0 and 1.';
931
+    color = str2num(RGB);
932
+    set(udata.c_struct.RGB,'String','');
933
+    if isempty(color)
934
+      close gcf;
935
+      udata=rmfield(udata,'c_struct');
936
+      set(udata.fig1,'UserData',udata);
937
+      errordlg([{str1};{str2}]);
938
+      return;
939
+    end
940
+    if ~all([1 3] == size(color)) & ~all([3 1] == size(color))
941
+      close gcf;
942
+      errordlg([{str1};{str2}]);
943
+      udata=rmfield(udata,'c_struct',udata);
944
+      set(udata.fig1,'UserData',udata);
945
+      return;
946
+    end
947
+    if ~isempty(cat(2,find(color>1),find(color<0)))
948
+      close gcf
949
+      errordlg([{str1};{str2}]);
950
+      udata=rmfield(udata,'c_struct',udata);
951
+      set(udata.fig1,'UserData',udata);
952
+      return;
953
+    end
954
+    udata.c_struct.color=color;
955
+    set(udata.fig1,'UserData',udata);
956
+    dye;
957
+    return;
958
+  end
959
+end
960
+
961
+udata.c_struct.color=RGB;
962
+set(udata.fig1,'UserData',udata);
963
+
964
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965
+
966
+function dye()
967
+
968
+% dyes black markers in the component planes and in the data figures
969
+
970
+udata=get(gcf,'UserData');
971
+udata=get(udata.fig1,'UserData');
972
+
973
+inds=unique([udata.all_marks ; udata.new_marks]);
974
+
975
+
976
+for i=1:length(udata.d_mark);
977
+  for j=1:length(udata.d_mark(i).h)
978
+    if all(get(udata.d_mark(i).h(j),'Color') == [0 0 0])
979
+      set(udata.d_mark(i).h(j),'Color',udata.c_struct.color);
980
+    end
981
+  end
982
+end
983
+
984
+if ~isempty(udata.fig2);
985
+  for i=1:length(udata.d_mark2)
986
+    for j=1:length(udata.d_mark2(i).h)
987
+      if all(get(udata.d_mark2(i).h(j),'Color') == [0 0 0])
988
+        set(udata.d_mark2(i).h(j),'Color',udata.c_struct.color);
989
+      end
990
+    end
991
+  end
992
+end
993
+
994
+
995
+for i=1:length(udata.a_h)
996
+  col=get(udata.tmp_patch(i),'FaceVertexCData');
997
+  for j=1:length(udata.new_marks)
998
+    col(udata.bmus(udata.new_marks(j)),:)=udata.c_struct.color;
999
+  end
1000
+  set(udata.tmp_patch(i),'FaceVertexCData',col);
1001
+end
1002
+
1003
+
1004
+udata.all_marks=unique([udata.all_marks;udata.new_marks]);
1005
+udata.new_marks=[];
1006
+close gcf;
1007
+udata=rmfield(udata,'c_struct');
1008
+set(udata.fig1,'UserData',udata);
1009
+
1010
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011
+
1012
+function clear_markers()
1013
+
1014
+% removes markers from the componentplanes and the data figure(s).
1015
+
1016
+udata=get(gcf,'UserData');
1017
+udata=get(udata.fig1,'UserData');
1018
+
1019
+for i=1:length(udata.d_mark)
1020
+  delete(udata.d_mark(i).h);
1021
+  udata.d_mark(i).h=[];
1022
+end
1023
+
1024
+for i=1:length(udata.d_mark2)
1025
+  delete(udata.d_mark2(i).h);
1026
+  udata.d_mark2(i).h=[];
1027
+end
1028
+
1029
+col=NaN*get(udata.tmp_patch(1),'FaceVertexCData');
1030
+col=reshape(col,[size(col,1) 3]);
1031
+
1032
+for i=1:length(udata.tmp_patch)
1033
+  set(udata.tmp_patch(i),'FaceVertexCData',col);
1034
+end
1035
+
1036
+udata.new_marks=[];
1037
+udata.all_marks=[];
1038
+
1039
+
1040
+if any(strcmp('c_struct',fieldnames(udata)))
1041
+  udata=rmfield(udata,'c_struct');
1042
+end
1043
+
1044
+set(udata.fig1,'UserData',udata);
1045
+
1046
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047
+
1048
+function key_bdf
1049
+
1050
+% moves trajectory and pointer lines, when either of 
1051
+% the keys '>' or '<' is pressed.
1052
+
1053
+udata=get(gcf,'UserData');
1054
+udata=get(udata.fig1,'UserData');
1055
+
1056
+key=get(gcbo,'CurrentCharacter');
1057
+
1058
+% The easiest way to get a new coordinates is to get them from the texts...
1059
+% The texts are either '<- x' or 'x ->' 
1060
+
1061
+x=get(udata.text1(1),'String');
1062
+x=str2num(x(4:length(x)));
1063
+
1064
+if isempty(x)
1065
+  x=get(udata.text1(1),'String');
1066
+  x=str2num(x(1:length(x)-3));
1067
+end
1068
+
1069
+switch(key)
1070
+ case '<'
1071
+  if x ~= 1
1072
+    x= x-1;
1073
+  end
1074
+ case '>'
1075
+  if x ~= getfield(get(get(udata.text1(1),'Parent'),'XLim'),{2}) 
1076
+    x = x+1;
1077
+  end
1078
+ otherwise
1079
+  return;
1080
+end
1081
+
1082
+set(udata.l,'XData',[x x]);
1083
+if ~isempty(udata.fig2)
1084
+  set(udata.l2,'XData',[x x]);
1085
+end
1086
+
1087
+delete(udata.text1);
1088
+delete(udata.text2);
1089
+
1090
+set_numbers(x);
1091
+draw_traj(x);
1092
+
1093
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094
+
1095
+function click()
1096
+
1097
+
1098
+switch get(gcf,'SelectionType')
1099
+ case 'open'
1100
+  return;
1101
+ case {'normal','alt'}
1102
+  draw_poly;
1103
+ case 'extend'
1104
+  click_node;
1105
+end
1106
+
1107
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108
+
1109
+function click_node()
1110
+
1111
+% takes care of the action, when the middle mouse button is
1112
+% pressed (mouse pointer is above some component plane).
1113
+
1114
+udata=get(gcf,'UserData');
1115
+udata=get(udata.fig1,'UserData');
1116
+new_marks=[];
1117
+
1118
+old=gcf;
1119
+
1120
+NEW=0;
1121
+AGAIN = 0;
1122
+
1123
+
1124
+coords=get(gca,'CurrentPoint');
1125
+row=round(coords(1,2));
1126
+
1127
+if strcmp(udata.lattice,'hexa') & ~mod(row,2)
1128
+  col = round(coords(1,1) - 0.5);
1129
+else
1130
+  col = round(coords(1,1));
1131
+end
1132
+
1133
+ind = sub2ind(udata.msize,row,col);
1134
+new_marks=find(udata.bmus==ind);
1135
+
1136
+if strcmp(get(gcbo,'Tag'),'TmpPatch');
1137
+  
1138
+  % if the callback is made via temporary patch object, node is marked
1139
+  % (node is black) => the mark is to be removed 
1140
+  
1141
+  node_color = getfield(get(gcbo,'FaceVertexCData'),{ind,[1:3]});
1142
+  AGAIN = 1;
1143
+end
1144
+
1145
+
1146
+
1147
+for i=1:length(udata.tmp_patch)
1148
+  color = get(udata.tmp_patch(i),'FaceVertexCData');
1149
+  if length(size(color)) ~= 2
1150
+    color = reshape(color,[size(color,1) 3]);
1151
+  end
1152
+  if all(isnan(color(ind,:)))
1153
+    NEW=1;
1154
+    color(ind,:)=[0 0 0];
1155
+  else
1156
+    color(ind,:)=[NaN NaN NaN];
1157
+  end
1158
+  set(udata.tmp_patch(i),'FaceVertexCData',color);
1159
+end
1160
+
1161
+set(0,'CurrentFigure',udata.fig1);
1162
+
1163
+for j=1:length(udata.h)
1164
+  subplot(udata.h(j));
1165
+  if NEW
1166
+    y=getfield(get(udata.d(j),'YData'),{new_marks});
1167
+    udata.d_mark(j).h=[udata.d_mark(j).h;plot(new_marks,y,'Color',[0 0 0],...
1168
+                                              'LineStyle','none',...
1169
+                                              'Marker','o')];
1170
+  end
1171
+end
1172
+
1173
+
1174
+if ~isempty(udata.fig2)
1175
+  set(0,'CurrentFigure',udata.fig2);
1176
+  for j=1:length(udata.h2);
1177
+    subplot(udata.h2(j));
1178
+    if NEW
1179
+      y=getfield(get(udata.d2(j),'YData'),{new_marks});
1180
+      udata.d_mark2(j).h=[udata.d_mark2(j).h;plot(new_marks,y,...
1181
+						  'LineStyle','none',...
1182
+						  'Color','black',...
1183
+						  'Marker','o')];
1184
+    end
1185
+  end
1186
+end
1187
+
1188
+if NEW
1189
+  udata.new_marks=[udata.new_marks; new_marks];
1190
+end
1191
+
1192
+if AGAIN
1193
+  
1194
+  % find marks from the data that map to the clicked node. if the color
1195
+  % of the mark(s) is the same as the node's color, remove mark(s), else
1196
+  % let mark be unchanged.
1197
+  
1198
+  for i=1:length(udata.d_mark(1).h)
1199
+    if all(node_color==get(udata.d_mark(1).h(i),'Color'))
1200
+      tmp1 = get(udata.d_mark(1).h(i),'XData');
1201
+      tmp2 = setdiff(tmp1,new_marks);
1202
+      if length(tmp1) ~= length(tmp2)
1203
+        inds=[];
1204
+        for j=1:length(tmp2);
1205
+          inds=[inds find(tmp2(j)==tmp1)];
1206
+        end
1207
+        for j=1:length(udata.d_mark)
1208
+          ydata=getfield(get(udata.d_mark(j).h(i),'YData'),{inds});
1209
+          set(udata.d_mark(j).h(i),'XData',tmp2,'YData',ydata);
1210
+        end
1211
+        if ~isempty(udata.fig2)
1212
+          for j=1:length(udata.d_mark2)
1213
+            ydata=getfield(get(udata.d_mark2(j).h(i),'YData'),{inds});
1214
+            set(udata.d_mark2(j).h(i),'XData',tmp2,'YData',ydata);
1215
+          end
1216
+        end
1217
+      end  
1218
+    end
1219
+  end
1220
+  udata.new_marks=setdiff(udata.new_marks, new_marks);
1221
+  udata.all_marks=setdiff(udata.all_marks,new_marks);
1222
+end
1223
+
1224
+set(udata.fig1,'UserData',udata);
1225
+set(0,'CurrentFigure',old);
1226
+
1227
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228
+
1229
+function draw_poly()
1230
+
1231
+udata=get(gcf,'UserData');
1232
+udata=get(udata.fig1,'UserData');
1233
+
1234
+if isempty(udata.poly.x)
1235
+  if strcmp(get(gcf,'SelectionType'),'alt')
1236
+    return;
1237
+  end
1238
+  udata.poly.THIS = gca;
1239
+end
1240
+
1241
+% 'THIS' indicates what was the axes where the polygon was meant to
1242
+% drawn. It is not possible to add points, that lie in another axes, to the
1243
+% polygon.
1244
+
1245
+
1246
+if gca ~= udata.poly.THIS
1247
+  return;
1248
+end
1249
+
1250
+coords(1,1) = getfield(get(gca,'CurrentPoint'),{3});
1251
+coords(1,2) = getfield(get(gca,'CurrentPoint'),{1});
1252
+
1253
+udata.poly.x=cat(1,udata.poly.x,coords(2));
1254
+udata.poly.y=cat(1,udata.poly.y,coords(1));
1255
+
1256
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1257
+% remove old 'polygon' from axis
1258
+
1259
+delete(udata.poly.h);
1260
+
1261
+
1262
+switch get(gcf,'SelectionType')
1263
+ case 'normal'
1264
+  
1265
+  % add point to the 'polygon' and draw it
1266
+  
1267
+  for i=1:length(udata.a_h)
1268
+    subplot(udata.a_h(i));
1269
+    hold on;
1270
+    udata.poly.h(i) = plot(udata.poly.x,udata.poly.y,'black',...
1271
+			   'EraseMode','xor',...
1272
+			   'ButtonDownFcn',...
1273
+			   'vis_trajgui([],''click'')',...
1274
+			   'LineWidth',2);
1275
+  end
1276
+ case 'alt'
1277
+  
1278
+  % The polygon is ready.
1279
+  
1280
+  
1281
+  udata.poly.x=cat(1,udata.poly.x,udata.poly.x(1));
1282
+  udata.poly.y=cat(1,udata.poly.y,udata.poly.y(1));
1283
+  
1284
+  for i=1:length(udata.a_h)
1285
+    subplot(udata.a_h(i));
1286
+    udata.poly.h(i) = plot(udata.poly.x,udata.poly.y,'black',...
1287
+			   'EraseMode','xor',...
1288
+			   'ButtonDownFcn',...
1289
+			   'vis_trajgui([],''click'')',...
1290
+			   'LineWidth',2);
1291
+  end
1292
+  
1293
+  tmp=sort(repmat((1:udata.msize(1))',udata.msize(2),1));
1294
+  tmp(:,2)=repmat((1:udata.msize(2))',udata.msize(1),1);
1295
+  tmp2=tmp;
1296
+  if strcmp(udata.lattice,'hexa');
1297
+    t=find(~rem(tmp(:,1),2));
1298
+    tmp(t,2)=tmp(t,2)+0.5;
1299
+  end
1300
+  
1301
+  
1302
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1303
+  % find the nodes that lie inside polygon and change coordinates to
1304
+  % linear indices.
1305
+  
1306
+  in = find(inpolygon(tmp(:,2),tmp(:,1),udata.poly.x,udata.poly.y));
1307
+  in = sub2ind(udata.msize,tmp2(in,1),tmp2(in,2));
1308
+  
1309
+  colors=get(udata.tmp_patch(1),'FaceVertexCData');
1310
+  colors=reshape(colors,[size(colors,1) 3]);
1311
+  tmp=ones(length(in),1);
1312
+  
1313
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
1314
+  % set the color of the nodes just selected, black.
1315
+  
1316
+  colors(in,:)=tmp*[0 0 0];
1317
+  set(udata.tmp_patch,'FaceVertexCData',colors);
1318
+  
1319
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320
+  % find the points mapping to the nodes from data 
1321
+  
1322
+  inds = [];
1323
+  for i=1:length(in)
1324
+    inds=[inds;find(in(i) == udata.bmus)];
1325
+  end
1326
+  
1327
+  %%%%%%%%%%%%%%%%%%%  
1328
+  % plot marks to data
1329
+  
1330
+  set(udata.fig1,'UserData',udata);
1331
+  plot2data(inds);
1332
+  udata=get(udata.fig1,'UserData');
1333
+  udata.new_marks=union(udata.new_marks,inds);
1334
+  delete(udata.poly.h);
1335
+  udata.poly.h=[];
1336
+  udata.poly.x=[];
1337
+  udata.poly.y=[];
1338
+  udata.poly.THIS=[];
1339
+end
1340
+
1341
+set(udata.fig1,'UserData',udata);
1342
+
1343
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1344
+
1345
+function save_data()
1346
+
1347
+udata=get(gcf,'UserData');
1348
+udata=get(udata.fig1,'UserData');
1349
+
1350
+data.points=[];
1351
+data.nodes=[];
1352
+k=1;
1353
+
1354
+for i=1:length(udata.d_mark(1).h)
1355
+  data.points(i).inds=get(udata.d_mark(1).h(i),'XData');
1356
+  data.points(i).color=get(udata.d_mark(1).h(i),'Color');
1357
+end
1358
+
1359
+color=get(udata.tmp_patch(1),'FaceVertexCData');
1360
+color=reshape(color,[size(color,1) 3]);
1361
+
1362
+for i=1:size(color,1)
1363
+  if all(~isnan(color(i,:)))
1364
+    tmp.ind=i;
1365
+    tmp.color=color(i,:);
1366
+    data.nodes(k)=tmp;
1367
+    k=k+1;
1368
+  end
1369
+end
1370
+
1371
+answer=inputdlg('Enter the name of the output variable:','',1);
1372
+
1373
+if isempty(answer) | isempty(answer{1})
1374
+  msgbox('Output is not set to workspace.');
1375
+  return;
1376
+else
1377
+  assignin('base',answer{1},data);
1378
+  disp(sprintf('Struct is set to the workspace as ''%s''.',answer{1}));
1379
+end
1380
+
1381
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382
+
1383
+function load_data()
1384
+
1385
+answer = inputdlg('Enter the name of the struct to be loaded:','',1);
1386
+
1387
+if isempty(answer) | isempty(answer{1})
1388
+  msgbox('Data is not loaded.');
1389
+  return;
1390
+end
1391
+
1392
+data=evalin('base',answer{1});
1393
+
1394
+if ~isstruct(data)  
1395
+  errordlg('Input variable must be a struct.');
1396
+  return;
1397
+end
1398
+
1399
+tmp1 = fieldnames(data);
1400
+tmp2 = {'nodes','points'};
1401
+
1402
+for i=1:length(tmp1)
1403
+  for j=1:length(tmp2);
1404
+    if ~any(strcmp(tmp2{j},tmp1))
1405
+      errordlg('Wrong type of struct.');
1406
+      return;
1407
+    end
1408
+  end
1409
+end