Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
currentTrial increment
#1
Apologies if this has been explained elsewhere, but how/where is the value for "currentTrial" incremented?

Thank you,

Randy
Reply
#2
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
Reply
#3
(02-13-2018, 08:58 PM)Josh Wrote: 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

Code:
%% 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
Reply
#4
Hi Randy,

In MATLAB, the syntax 1:n generates an array of integers between 1 and n. (Try this at the command prompt!)

"for" loop runs the code between "for" and "end", once for each element in array 1:n. The value of the loop counter variable (in our case, currentTrial) is automatically set to the next value in the array, on each iteration of the loop.

Thus, 
Code:
for currentTrial = 1:5
   disp(currentTrial);
end
outputs currentTrial on each loop:
1
2
3
4
5

I hope this helps!
Reply
#5
(02-14-2018, 02:21 AM)Josh Wrote: Hi Randy,

In MATLAB, the syntax 1:n generates an array of integers between 1 and n. (Try this at the command prompt!)

"for" loop runs the code between "for" and "end", once for each element in array 1:n. The value of the loop counter variable (in our case, currentTrial) is automatically set to the next value in the array, on each iteration of the loop.

Thus, 
Code:
for currentTrial = 1:5
   disp(currentTrial);
end
outputs currentTrial on each loop:
1
2
3
4
5

I hope this helps!

Again, thank you for your response.  The concept of automatically incrementing variables in a a for loop is a new one for me.  I was looking for currentTrial = currentTrial+1.

Thank you,

Randy
Reply


Forum Jump: