Josh wrote[quote='Josh' pid='454' dateline='1518555538']
Hi Randy
In Bpod, you write the top-level loop that runs each trial of your protocol. Protocols are m-files, that live in folders with the same name. When you choose a protocol from the launch manager and press the "Launch" button, MATLAB runs your protocol m-file as a script.
Our example protocols (e.g. Light2AFC or Operant) use the variable currentTrial as the index of the main loop - so you choose how to increment it, when you write the main loop's "for" or "while" statement.
I hope this helps!
-Josh
Thank you for your response. Based on the Operant protocol, the main loop starts and assigns currentTrial a value of 1 and runs until it reaches the value of MaxTrials. Forgive me for appearing dense, but I do not find any statement incrementing the value of currentTrial in the loop. Where, exactly, is this value incremented?
Thank you,
Randy
%% Main trial loop
for currentTrial = 1:MaxTrials
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin
R = GetValveTimes(S.GUI.RewardAmount, [1 3]); LeftValveTime = R(1); RightValveTime = R(2); % Update reward amounts
switch TrialTypes(currentTrial) % Determine trial-specific state matrix fields
case 1
StateOnLeftPoke1 = 'LeftReward'; StateOnRightPoke1 = 'RightReward';
StateOnLeftPoke2 = 'LeftReward'; StateOnRightPoke2 = 'RightReward';
case 2
StateOnLeftPoke1 = 'WaitForPokeOut1'; StateOnRightPoke1 = 'WaitForPokeOut1';
StateOnLeftPoke2 = 'LeftReward'; StateOnRightPoke2 = 'RightReward';
case 3
StateOnLeftPoke1 = 'WaitForPokeOut1'; StateOnRightPoke1 = 'WaitForPokeOut1';
StateOnLeftPoke2 = 'WaitForPokeOut2'; StateOnRightPoke2 = 'WaitForPokeOut2';
end
sma = NewStateMatrix(); % Assemble state matrix
sma = AddState(sma, 'Name', 'WaitForPoke1', ...
'Timer', 0,...
'StateChangeConditions', {'Port1In', StateOnLeftPoke1, 'Port3In', StateOnRightPoke1},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'WaitForPokeOut1', ...
'Timer', S.GUI.PortOutRegDelay,...
'StateChangeConditions', {'Port1Out', 'EnforcePokeOut1', 'Port3Out', 'EnforcePokeOut1'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'EnforcePokeOut1', ...
'Timer', S.GUI.PortOutRegDelay,...
'StateChangeConditions', {'Tup', 'WaitForPoke2'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'WaitForPoke2', ...
'Timer', 0,...
'StateChangeConditions', {'Port1In', StateOnLeftPoke2, 'Port3In', StateOnRightPoke2},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'WaitForPokeOut2', ...
'Timer', S.GUI.PortOutRegDelay,...
'StateChangeConditions', {'Port1Out', 'EnforcePokeOut2', 'Port3Out', 'EnforcePokeOut2'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'EnforcePokeOut2', ...
'Timer', S.GUI.PortOutRegDelay,...
'StateChangeConditions', {'Tup', 'WaitForPoke3'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'WaitForPoke3', ...
'Timer', 0,...
'StateChangeConditions', {'Port1In', 'LeftReward', 'Port3In', 'RightReward'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'LeftReward', ...
'Timer', LeftValveTime,...
'StateChangeConditions', {'Tup', 'Drinking'},...
'OutputActions', {'ValveState', 1});
sma = AddState(sma, 'Name', 'RightReward', ...
'Timer', RightValveTime,...
'StateChangeConditions', {'Tup', 'Drinking'},...
'OutputActions', {'ValveState', 4});
sma = AddState(sma, 'Name', 'Drinking', ...
'Timer', 10,...
'StateChangeConditions', {'Tup', 'exit', 'Port1Out', 'ConfirmPortOut', 'Port3Out', 'ConfirmPortOut'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'ConfirmPortOut', ...
'Timer', S.GUI.PortOutRegDelay,...
'StateChangeConditions', {'Tup', 'exit', 'Port1In', 'Drinking', 'Port3In', 'Drinking'},...
'OutputActions', {});
SendStateMatrix(sma);
RawEvents = RunStateMatrix;
if ~isempty(fieldnames(RawEvents)) % If trial data was returned
BpodSystem.Data = AddTrialEvents(BpodSystem.Data,RawEvents); % Computes trial events from raw data
BpodSystem.Data = BpodNotebook('sync', BpodSystem.Data); % Sync with Bpod notebook plugin
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
BpodSystem.Data.TrialTypes(currentTrial) = TrialTypes(currentTrial); % Adds the trial type of the current trial to data
UpdateOutcomePlot(TrialTypes, BpodSystem.Data);
SaveBpodSessionData; % Saves the field BpodSystem.Data to the current data file
end
HandlePauseCondition; % Checks to see if the protocol is paused. If so, waits until user resumes.
if BpodSystem.Status.BeingUsed == 0
return
end
end