Raw File
Tip revision: 70d31d43686fdd0577bf1903dccfa544327b375a authored by huguenardlab on 22 March 2023, 17:15:05 UTC
Add files via upload
Tip revision: 70d31d4
%By: Brielle Ferguson
%Date: 1/10/18
%Script will take serial monitor data generated from the arduino
%"OperantTaskMasterScript" sketch during a behavioral session. The OperantTaskMasterScript
% sketch must be uploaded first through the Arduino IDE before running this 
% script in MATLAB.
% - Arduino sketch is already loaded
% - cd is set to the folder for saving data
% - path includes the helper functions cleanUpOperantData and sepCueLengthandAccuracy
% - animalID: string collected collected in input box
% - behData: a cell array with trial information about the behavioral
%   session. This can be analyzed with the function readAnalyzePlotBehData, which
%   is called at the end of the session to plot some summary data about the
%   behavioral session.

%% set up serial port
delete(instrfindall);%make sure no serial ports are already open
serialPort = 'COM5'; %the port the arduino is connected to
baudeRate = 115200; %make sure this matches the rate in the arduino script that you're streaming your serial monitor data
s = serial(serialPort,'Timeout', 5000, 'BaudRate', baudeRate, 'Terminator', 'LF');
s.ReadAsyncMode = 'continuous';

%% pre-allocate variables to be populated in the loop
timeStamp = {};
formatOut = 'hh:mm:ss.fff';
behData = {};
index = 1;
running = true;

%% create figure asking user for filename and then indicating that the session is running

fig1 = figure('pos',[500 500 550 200]);

% filename edit
h1 = uicontrol(fig1,...
    'Position', [60 100 400 20]);

% filename prompt
h2 = uicontrol(fig1,...
    'String', 'Filename:',...
    'Position', [10 100 50 20]);

% continue button
h3 = uicontrol(fig1, 'Style', 'pushbutton', 'String', 'continue', 'Callback', 'uiresume(gcbf);');

baseFileName = h1.String;
textFileName = [h1.String '.txt'];

if exist(fullfile(cd,baseFileName),'file')
    warning([baseFileName ' already exists and will be overwritten in this session.'])


%figure indicating that MATLAB is collecting Arduino data. User can click end to
%end the session 
fig2 = figure('pos',[847 500 296 200]);

h4 = uicontrol(fig2,...
    'String', 'Collecting Serial Monitor Data...',...
    'Position', [58 35 184 124], 'FontSize', 15);
h5 = uicontrol(fig2, 'Style', 'pushbutton', 'String', 'End Session', 'Position', [89 60 117 20],'Callback', 'delete(gcbf);');

%% display and save serial monitor data until the user ends the session         
while true
    if  (s.BytesAvailable > 0)
        %read event and time information from the arduino
        event = fgetl(s); 
        timeString = char(datetime('now','Format', 'HH:mm:ss.SSS'));
        behData{index} = [timeString, ' -> ', event];  
        %stream data to the command window
        index = index + 1;
    %check to see if user has clicked "End Session" button
    if ~ishandle(h5)
        disp('End Session');

%% save and  plot data
behData = transpose(behData);
save behData behData baseFileName;
delete(instrfindall);%close the serial port

%plot behavioral data 
figure;[summary, trials] = readAnalyzePlotBehData(behData, baseFileName);

back to top