%
% plot voltage at a point over time
%

function view(v_dat,indexes,len)

fid = fopen(v_dat,'r');		% read voltage and time data
v = fread(fid,'double');
fclose(fid);
sv = size(v);

dt = v(1);
Nt = v(2);
n = v(3);

t = 0:dt:dt*Nt;

v = v(4:sv);

sv = sv-3;

xfid = fopen('xdat','r');	% read morphology
x = fread(xfid,'double');
yfid = fopen('ydat','r');
y = fread(yfid,'double');
afid = fopen('adat','r');
a = fread(afid,'double');
ifid = fopen('idat','r');
ind = fread(ifid,'double');
Dfid = fopen('Ddat','r');
dnum = fread(Dfid,'double');

pp = 1;				% new plot indicator

while pp > 0

   figure(1)
   [xp,yp] = ginput(1);		% grab point at cross-hairs

   misfit = abs(x-xp) + abs(y-yp);

   [m,mi] = min(misfit);	% mi is index of closest (x,y) point

   d = dnum(mi);		% dendrite number

   ii = ind(mi);		% branch number

   rb = find(indexes==0);
   srb = length(rb);
   if d==srb,
      rend = length(indexes);
   else
      rend = rb(d+1) - 1;
   end
   dend = indexes(rb(d):rend);
   iii = find(dend==ii);
%  disp(['dendrite = ' num2str(d) ' and branch = ' num2str(ii)]);

   p = 1 + sum(len(1:rb(d)-1+iii-1)) + min(round(a(mi)),len(rb(d)-1+iii));

   text(xp,yp,num2str(p))


   vp = v(p:n:sv);	% voltage at node p
   [mv,iv] = max(vp);

   figure(2)
   plot(t,vp)
   xlabel('time (ms)','fontsize',16)
   ylabel('voltage (mV)','fontsize',16);
   text(t(iv),mv,num2str(p));
   hold on

   pp = input('another point (1=y) (0=n) : ');

end

figure(2)
hold off
