figure(1);clf

IMGFILEFORMAT=1 %1=tif 0=avi %% You can either have a series of tifs or an avi that contains a stack of images. Or you can add whatever format you desire.

MASS=1.7; % in grams

M=0;clear M;
vv=21000:36000;  %% range of force data to plot


%%%% Read A/D data into an array called dd; forceplateread is a script I
%%%% have to read in A/D data for this experiment
dd=forceplateread('C:\climbing\newclimbingdata\122104\cagec\r1_10.raw'); 

%% an array of times for force data
tt=(0:length(dd)-1)/9500;



%% Set up axes in the figure
SFX=1000;
SFY=700;
h=gcf;
set(gcf,'PaperPositionMode','auto');
set(gcf,'Position',[100 200 SFX SFY]);
ax1_1=axes('position',[.18 .05 .4 .9]);
ax1_1s=axes('position',[0 .2 .22 .55]);
ax1_2=axes('position',[.625 .69 .35 .2*SFX/SFY]);
ax1_3=axes('position',[.625 .37 .35 .2*SFX/SFY]);
ax1_4=axes('position',[.625 .05 .35 .2*SFX/SFY]);


Nx=512;
Ny=384;
x0=150;
y0=1;
nx=200;
ny=384;

t0=min(find(diff(dd(:,1))<-.4)); %% find locomotion in A/D data that matches the first frame in the image data--in my data I detect a falling edge in channel zero that occurs when camera recording begins.



tifimgnameintopbase='F:\\climbing_data_new\\122104\\cagec\\r1_10_c1_%6.6d.tif';
tifimgnameinsidebase='F:\\climbing_data_new\\122104\\cagec\\r1_10_c2_%6.6d.tif';
imgnumz=-500:1:0;

if IMGFILEFORMAT==0  % for an avi
ii=aviinfo(aviimgnamein);
numz=ii.NumFrames;
imgnumz=-(numz-1):1:0;
end
numz=length(imgnumz);
filez=imgnumz(1:4:end);  %% use only these files


count=1

if IMGFILEFORMAT==0
    rawin=(double(frame2im(aviread(aviimgnamein,1))));
    rawinbkg=rawin(:,:,1);
else
    nameintop=sprintf(tifimgnameintopbase,imgnumz(1));     
    rawinbkgtop=double(imread(nameintop));
    rawinbkgtop=rawinbkgtop(:,:,1);
end    

for jj=1:length(filez);
    fprintf('.');
           
    if IMGFILEFORMAT==0
    rawin=(double(frame2im(aviread(aviimgnamein,numz+filez(jj)))));
    rawin=rawin(:,:,1);
    else
     
     %%% Can read in images from two views, top view and side view
     nameintop=sprintf(tifimgnameintopbase,filez(jj));     
     rawintop=double(imread(nameintop));
     rawintop=rawintop(:,:,1);
     
     nameinside=sprintf(tifimgnameinsidebase,filez(jj));     
     rawinside=double(imread(nameinside));
     rawinside=rawinside(:,:,1);
     
    end
 
figure(1);  

%%% Display images (suitably transformed, rotated--these are for me)

axes(ax1_1);
   
 rawinfliptop=fliplr(rawintop')-0*fliplr(rawinbkgtop');
 rawtr=rawinfliptop;
 imagesc(rawtr(:,50:280));axis image 
 axis off
 
axes(ax1_1s);
 
 rawinside=rawinside(:,350:512);
 rawtr=rawinside;
 imagesc(rawtr);axis image 
 axis off

drawnow;

dnum=t0+filez(jj)*19; %%% since sampling rate of A/D board is 9500 Hz and camera frame rate is 500 Hz, must convert
 
axes(ax1_2);hold off
%% plot data for fore-aft force
plot(tt(vv),dp(vv,3)/.11*9.8,'k-');
hold on;
%% plot point on data that corresponds to current image frame
plot(tt(dnum),dp(dnum,3)/.11*9.8,'ro');
line([min(tt) max(tt)],[MASS*9.8 MASS*9.8])

%% Set the axes
set(gca,'xlim',[min(tt(vv)) max(tt(vv))],'xticklabel','','ylim',[-1 4]*9.8);
ylabel('Force (mN)');
text(2.4, 30,'Fore-aft','fontsize',18);

%%% Do the same for other data
axes(ax1_3);hold off
plot(tt(vv),-dp(vv,8)/.5*9.8,'k-');
hold on;
plot(tt(dnum),-dp(dnum,8)/.5*9.8,'ro');
line([min(tt) max(tt)],[MASS*9.8 MASS*9.8])
set(gca,'xlim',[min(tt(vv)) max(tt(vv))],'xticklabel','','ylim',[-3 3]*9.8);
text(2.4, 20,'Normal','fontsize',18);

axes(ax1_4);hold off
plot(tt(vv),-dp(vv,4)/.2*9.8,'k-');
hold on;
plot(tt(dnum),-dp(dnum,4)/.2*9.8,'ro');
line([min(tt) max(tt)],[MASS*9.8 MASS*9.8])
set(gca,'xlim',[min(tt(vv)) max(tt(vv))],'ylim',[-3 3]*9.8);
text(2.4, 22,'Lateral','fontsize',18);


figure(1)
M(count)=getframe(h); % get a frame
drawnow;
count=count+1;
    
end
