clear; figure(1); clf; daqreset;

%%% parameters for the experiment
sample_rate = 1000;  
plot_duration = 1;   %% how frequent to plot
exp_duration = 10;   %% how long to collect data for

%%% Set up the parameters to acquire the data
AI = analoginput('nidaq','Dev1');    
chan = addchannel(AI,1);
set(AI,'SampleRate',sample_rate);
set(AI,'SamplesPerTrigger',inf);
set(AI.Channel(1),'InputRange',[-1 1]);

start(AI);

all_data = []; all_time = [];   %% for storing all the data
for i =1:ceil(exp_duration/plot_duration)
    
    while AI.SamplesAvailable < sample_rate*plot_duration   %% wait until enough data
    end 
    [data time] = getdata(AI,(sample_rate*plot_duration));  %% get the dataa

    %%% save all the data, 
    all_data = [all_data;data];   
    all_time = [all_time;time];
    
    %%% plot the current and all data to show two different time scales. 
     subplot(2,1,2);
     plot(time,data);        
     xlabel('t (sec)'); ylabel('V');
     
     subplot(2,1,1)
     plot(all_time,all_data);             
     xlabel('t (sec)'); ylabel('V');

     drawnow;
end

delete(AI);
clear AI;
