Cerebral Haemodynamic Autoregulatory Information System GUI 1.0.0

File: <base>/CHARIS_GUI_CODE/CHARIS_GUI.m (41,490 bytes)
% Copyright (C) 2016 William Craelius (craelius@rci.rutgers.edu)

% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.

% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

function varargout = CHARIS_GUI(varargin)

% CHARIS_GUI Version 1.0 

% CHARIS_GUI MATLAB code for CHARIS_GUI.fig
%      CHARIS_GUI, by itself, creates a new CHARIS_GUI or raises the existing
%      singleton*.
%
%      H = CHARIS_GUI returns the handle to a new CHARIS_GUI or the handle to
%      the existing singleton*.
%
%      CHARIS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CHARIS_GUI.M with the given input arguments.
%
%      CHARIS_GUI('Property','Value',...) creates a new CHARIS_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before CHARIS_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to CHARIS_GUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help CHARIS_GUI

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @CHARIS_GUI_OpeningFcn, ...
    'gui_OutputFcn',  @CHARIS_GUI_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before CHARIS_GUI is made visible.
function CHARIS_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to CHARIS_GUI (see VARARGIN)

% Choose default command line output for CHARIS_GUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = CHARIS_GUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ICP
global ABP
global ECG
global Time
global OrigTable
global count
global PatientData
global eventTimes

PatientData=[];
count=0;
ICP=[];
ABP=[];
ECG=[];
Time=[];
eventTimes=[];


set(handles.text42,'String',' ')
% Get PRx Data
edit8=str2num(char(get(handles.edit8,'String')));edit9=str2num(char(get(handles.edit9,'String')));edit10=str2num(char(get(handles.edit10,'String')));
dirOriginal=pwd;

% Load Source Files
set(handles.text42,'String','Data can be loaded from LVMs, source files, or from CHARIS Data.mat');
choice=questdlg('Has data for this patient already been extracted from the LVMs into source files (.mat) or a CHARIS Data.mat?','Find Source Files','Yes','No','Yes');
switch choice
    case 'Yes'
        SaveFolder=1;
    case 'No'
        SaveFolder=0;
    case 'Cancel'
      cd(dirOriginal)
end

if SaveFolder==1
    set(handles.text42,'String','Select folder containing source files (.mat) or the CHARIS Data.mat');
    fprintf('Select folder with the desired source files (.mat)\n ');
    dirRawLocation = uigetdir('Select folder with source files (.mat)\n C:\...\PatientFolder\Source Channels \n');
    cd(dirRawLocation)
    found=0;
    found2=0;
    count1=0;
    count2=0;
    
    % Count files for CHARIS Data
    files = dir('*.mat');
    for i=1:length(files)
        if strncmpi(files(i).name,'CHARIS',5)==1
            count1=count1+1;
        end
    end
    
    % Multiple CHARIS found
    if count1>1
        str=sprintf('CHARIS parts 1-%d detected. Type a number to load desired part.\nCHARIS files are ordered as they are ordered in the folder.',count1);
        whichPart1=inputdlg(str);
        
        partLoad1=str2num(whichPart1{1,1});
        for i=1:length(files)
            if strncmpi(files(i).name,'CHARIS',5)==1
                count2=count2+1;
                if count2==partLoad1
                    load(files(i).name);
                    cd(dirOriginal)
                    found=1;
                end
            end
        end
        
        
    else
        %Only 1 CHARIS found
        for i=1:length(files)
            if strncmpi(files(i).name,'CHARIS',5)==1
                load(files(i).name);
                cd(dirOriginal)
                found=1;
                
            end
        end
        
    end

    if found==0
        for i=1:length(files)
            if strncmpi(files(i).name,'Time Part',9)==1                
               found2=1;
               count2=count2+1;
            end
        end
    end

    % No CHARIS Data found. Source Parts found (.mat)
    if found2==1
        str1=sprintf('Source Parts 1-%d detected. Type a number to load desired part.\nSource Parts are ordered as they are ordered in the folder.',count2);
        whichPart=inputdlg(str1);
        partLoad=str2num(whichPart{1,1});
        for i=1:length(files)
            strT=sprintf('Time Part %d',partLoad);
            strA=sprintf('ABP Part %d',partLoad);
            strI=sprintf('ICP Part %d',partLoad);
            strE=sprintf('ECG Part %d',partLoad);
            
            if strncmpi(files(i).name,strA,10)==1
                ABP=importdata(files(i).name);
            elseif strncmpi(files(i).name,strI,10)==1
                ICP=importdata(files(i).name);
            elseif strncmpi(files(i).name,strE,10)==1
                ECG=importdata(files(i).name);
            elseif strncmpi(files(i).name,strT,11)==1
                Time=importdata(files(i).name);
            end
        end
         cd(dirOriginal)
    end
    
    
    % No CHARIS data found and no source files found
    if found==0&&found2==0
        for i=1:length(files)
            if strncmpi(files(i).name,'ABP',3)==1
                ABP=importdata(files(i).name);
            elseif strncmpi(files(i).name,'ICP',3)==1
                ICP=importdata(files(i).name);
            elseif strncmpi(files(i).name,'ECG',3)==1
                ECG=importdata(files(i).name);
            elseif strncmpi(files(i).name,'Time',4)==1
                Time=importdata(files(i).name);
            end
        end
    end
    set(handles.text42,'String','Finished Loading Source Files')
else
    set(handles.text42,'String','Select folder containing LVM files.');
    dirName = uigetdir('Select folder containing LVM files.');
    set(handles.text42,'String','Select or create a folder for the source files.');
    dirRawSave = uigetdir('Select or create a folder for the source files.');
    set(handles.text42,'String','Compiling Data');
    
    cd(dirName)
    lvmFiles = dir('*.lvm');
    numfiles = length(lvmFiles);
    roundUp=ceil(numfiles/100);
    
    %Make sure that this is the correct column
    tempMatrix = importLVMfile(lvmFiles(1).name, 25, 180024);
    SaveABP=0;
    SaveICP=0;
    SaveECG=0;
    
    while SaveABP==0||SaveICP==0||SaveECG==0
        if SaveABP==0
        
        figure
        plot(tempMatrix(:,edit8));
        set(handles.text42,'String','Examine the figure for ABP data and then close figure to continue.');
        uiwait(gcf)
        
        figure
        plot(tempMatrix(:,edit8));
        choice=questdlg('Is this ABP data?','Find ABP Column','Yes','No','Yes');        
        switch choice
            case 'Yes'
                SaveABP=1;
            case 'No'
                SaveABP=0;
                edit8=inputdlg('Set new ABP Channel (Channels are 2-9)');
                edit9=str2num(edit9{1,1});
                set(handles.edit8,'String',edit8)
            case 'Cancel'
                break
        end
        
        close
        end
        if SaveICP==0
        figure
        plot(tempMatrix(:,edit9));
        set(handles.text42,'String','Examine figure for ICP data and then close figure to continue.');
        uiwait(gcf)
        
        figure     
        plot(tempMatrix(:,edit9));
        choice=questdlg('Is this ICP data?','Find ICP Column','Yes','No','Yes');
        switch choice
            case 'Yes'
                SaveICP=1;
            case 'No'
                SaveICP=0;
                edit9=inputdlg('Set new ICP Channel (Channels are 2-9)');
                edit9=str2num(edit9{1,1});
                set(handles.edit9,'String',edit9)
            case 'Cancel'
                break
        end
        close
        end
        
        if SaveECG==0
        figure
        plot(tempMatrix(:,edit10));
        set(handles.text42,'String','Examine figure for ECG and then close figure to continue.');
        uiwait(gcf)
        
        figure 
        plot(tempMatrix(:,edit10));
        choice=questdlg('Is this ECG data?','Find ECG Column','Yes','No','Yes');
        switch choice
            case 'Yes'
                SaveECG=1;
            case 'No'
                SaveECG=0;
                edit10=inputdlg('Set new ECG Channel (Channels are 2-9)');
                edit10=str2num(edit10{1,1});
                set(handles.edit10,'String',edit10)
            case 'Cancel'
                break
        end
        close
        end
    end
    cd(dirOriginal)
    
    if numfiles>=100
        set(handles.text42,'String','Over 100 files detected. Source files will be split into 100-file parts to improve performance');
        h=waitbar(0,'Please Wait (Files exceed 100)');
        
        for i=1:roundUp
            if i==roundUp;
                indTop=numfiles;
                indBot=roundUp*100-100+1;
            else
                indTop=i*100;
                indBot=indTop-100+1;
            end
            
            [Time1] = CHARISGUIcollectData(indBot,indTop,1,dirName);
            waitbar(((i-1)*4+1)/(roundUp*4))
            [ABP1] = CHARISGUIcollectData(indBot,indTop,edit8,dirName);
            waitbar(((i-1)*4+2)/(roundUp*4))
            [ICP1] = CHARISGUIcollectData(indBot,indTop,edit9,dirName);
            waitbar(((i-1)*4+3)/(roundUp*4))
            [ECG1] = CHARISGUIcollectData(indBot,indTop,edit10,dirName);
            waitbar(((i-1)*4+4)/(roundUp*4))
            
            
            cd(dirRawSave)
            ECG1=cell2mat(ECG1);
            ICP1=cell2mat(ICP1).*100;
            ABP1=cell2mat(ABP1);
            Time1=cell2mat(Time1);
            str1=sprintf('ECG Part %d.mat',i);
            str2=sprintf('ICP Part %d.mat',i);
            str3=sprintf('ABP Part %d.mat',i);
            str4=sprintf('Time Part %d.mat',i);
            save(str1,'ECG1');
            save(str2,'ICP1');
            save(str3,'ABP1');
            save(str4,'Time1');
            
            if i==1
                Time=Time1;
                ABP=ABP1;
                ICP=ICP1;
                ECG=ECG1;
            end
            cd(dirOriginal)
        end
        close(h)
        
        
    else
        h=waitbar(0,'Please wait...');
        [Time] = CHARISGUIcollectData(1,numfiles,1,dirName);
        waitbar(1/4)
        [ABP] = CHARISGUIcollectData(1,numfiles,edit8,dirName);
        waitbar(2/4)
        [ICP] = CHARISGUIcollectData(1,numfiles,edit9,dirName);
        waitbar(3/4)
        [ECG] = CHARISGUIcollectData(1,numfiles,edit10,dirName);
        waitbar(4/4)
        close(h)
        
        cd(dirRawSave)
        ECG=cell2mat(ECG);
        ICP=cell2mat(ICP).*100;
        ABP=cell2mat(ABP);
        Time=cell2mat(Time);
        
        save('ECG.mat','ECG');
        save('ICP.mat','ICP');
        save('ABP.mat','ABP');
        save('Time.mat','Time');
        set(handles.text42,'String','Finished Compiling');
    end
    
end
if isequal(length(Time),length(ABP))==0
    if length(Time)>length(ABP)
        fix=zeros(length(Time)-length(ABP),1);
        ABP=[ABP;fix];
    elseif length(ABP)>length(Time)
        ABP=ABP(1:length(Time));
    end
end
if isequal(length(Time),length(ICP))==0
    if length(Time)>length(ICP)
        fix=zeros(length(Time)-length(ICP),1);
        ICP=[ICP;fix];
    elseif length(ICP)>length(Time)
        ICP=ICP(1:length(Time));
    end
end
if isequal(length(Time),length(ECG))==0
    if length(Time)>length(ECG)
        fix=zeros(length(Time)-length(ECG),1);
        ECG=[ECG;fix];
    elseif length(ECG)>length(Time)
        ECG=ECG(1:length(Time));
    end
end
cd(dirOriginal);
cla(handles.axes1)
cla(handles.axes2)
cla(handles.axes3)
cla(handles.axes4)
set(handles.axes1, 'Xdir', 'normal')
set(handles.axes2, 'Xdir', 'normal')
set(handles.axes3, 'Xdir', 'normal')
set(handles.axes4, 'Xdir', 'normal')
if length(Time)==length(ICP)
    plot(handles.axes1,Time,ICP);
end
set(handles.text1, 'String', 'Raw ICP');
set(handles.text2, 'String', 'Time in seconds');
set(handles.text3, 'String', 'mmHg');
if length(Time)==length(ABP)
    plot(handles.axes2,Time,ABP);
end
set(handles.text4, 'String', 'Raw ABP');
set(handles.text10, 'String', 'Time in seconds');
set(handles.text11, 'String', 'mmHg');
if length(Time)==length(ECG)
    plot(handles.axes3,Time,ECG);
end
set(handles.text12, 'String', 'Raw ECG');
set(handles.text13, 'String', 'Time in seconds');
set(handles.text14, 'String', 'mV');
set(handles.uitable1,'Data',[Time,ABP,ICP,ECG])
OrigTable=[Time,ABP,ICP,ECG];

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global PatientData
global PRx
global PacketAvg_Time
global ICP
global ABP
global ECG
global Time
global eventTimes
global critStr
global ICPevent_index

ICPevent_index=[];
tableData=get(handles.uitable1,'Data');
edit1 = get(handles.edit1,'String');
edit2=str2num(char(get(handles.edit2,'String')));
edit3=str2num(char(get(handles.edit3,'String')));
edit4=str2num(char(get(handles.edit4,'String')));
edit5=str2num(char(get(handles.edit5,'String')));
set(handles.edit17,'String',edit1);
critStr1=get(handles.edit2,'String');
critStr2=get(handles.edit3,'String');
critStr3=get(handles.edit4,'String');
critStr4=get(handles.edit5,'String');


edit6=edit5*12;
set(handles.text42,'String','Finding Events')
critStr=sprintf('Patient ID: %s \n ICP Threshold: %s mmHg \n Event Length: %s minutes \n Pre-Event: %s minutes \n Examined: %s minutes', edit1,critStr1,critStr2,critStr3,critStr4);

[PatientData,PRxEventTimes,PTM]=CHARISGUIReportPRxEvents(edit1,edit2,edit3,edit4,edit6,tableData(:,3), tableData(:,1),PRx,PacketAvg_Time);
eventTimes=PatientData.eventTimes;
[row,col]=size(eventTimes);
if row==0
    str=sprintf('Events: %d',row);
    set(handles.text42, 'String', str);
    
else
    cla(handles.axes1)
    cla(handles.axes2)
    cla(handles.axes3)
    cla(handles.axes4)
    set(handles.axes1, 'Xdir', 'normal')
    set(handles.axes2, 'Xdir', 'normal')
    set(handles.axes3, 'Xdir', 'normal')
    set(handles.axes4, 'Xdir', 'normal')
    str=sprintf('Events: %d',row);
    set(handles.text42, 'String', str);
    cla(handles.axes1)
    plot(handles.axes1,Time,ICP);
    set(handles.text1, 'String', 'ICP');
    set(handles.text2, 'String', 'Time in seconds');
    set(handles.text3, 'String', 'mmHg');
    
    for i=1:length(eventTimes)
        if isempty(find(Time==eventTimes(i)))==0
            TimeIndex(i,1)=find(Time==eventTimes(i));
        else
            Time=round(Time(:,1)*(10^2))/(10^2);
            TimeIndex(i,1)=find(Time==eventTimes(i));
            
        end
        ICPevent_index(:,i)=ICP(TimeIndex(i,1)-edit5*60/0.02:TimeIndex(i,1));
    end
    
    ICPindex=ICP(TimeIndex);
    
    hold(handles.axes1,'on')
    h=scatter(handles.axes1,eventTimes,ICPindex,'ro');
    set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
end
cla(handles.axes2)
plot(handles.axes2,Time,ABP);
set(handles.text4, 'String', 'ABP');
set(handles.text10, 'String', 'Time in seconds');
set(handles.text11, 'String', 'mmHg');
if exist('eventTimes')==1
    for i=1:length(eventTimes)
        TimeIndex(i,1)=find(Time==eventTimes(i));
    end
    ABPindex=ABP(TimeIndex);
    hold(handles.axes2,'on')
    h=scatter(handles.axes2,eventTimes,ABPindex,'ro');
    set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
    hold(handles.axes2,'off')
end
cla(handles.axes3)
plot(handles.axes3,Time,ECG);
set(handles.text12, 'String', 'ECG');
set(handles.text13, 'String', 'Time in seconds');
set(handles.text14, 'String', 'mV');
if exist('eventTimes')==1
    for i=1:length(eventTimes)
        TimeIndex(i,1)=find(Time==eventTimes(i));
    end
    ECGindex=ECG(TimeIndex);
    hold(handles.axes3,'on')
    h=scatter(handles.axes3,eventTimes,ECGindex,'ro');
    set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
    hold(handles.axes3,'off')
end
set(handles.text42,'String','Finished Finding Events')
cla(handles.axes4)

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.edit1 = get(hObject,'String');

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
input = str2double(get(hObject,'string'));
if isnan(input)
    errordlg('You must enter a numeric value','Invalid Input','modal')
    uicontrol(hObject)
    return
else
    display(input);
end

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str2double(get(hObject,'String'))

% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ICP
global ABP
global ECG
global Time
global PacketAvg_ICP
global PacketAvg_ABP
global PRx
global count
global eventTimes
global PacketAvg_Time

count=count+1;

tableData=get(handles.uitable1,'Data');

listed=get(handles.listbox2,'String');
selected=get(handles.listbox2,'Value');

graphed=get(handles.listbox3,'Value');
if graphed==1
    graph=handles.axes1;
elseif graphed==2
    graph=handles.axes2;
elseif graphed==3
    graph=handles.axes3;
elseif graphed==4
    graph=handles.axes4;
end

    
if strcmp(listed(selected),'Raw ICP')==1
    
    cla(graph)
    set(graph, 'Xdir', 'normal')
    
    plot(graph,Time,ICP)
    if graphed==1
        %Title
        set(handles.text1, 'String', 'Raw ICP');
        %X values
        set(handles.text2, 'String', 'Time in seconds');
        %Y values
        set(handles.text3, 'String', 'mmHg');
        
    elseif graphed==2
        set(handles.text4, 'String', 'Raw ICP');
        set(handles.text10, 'String', 'Time in seconds');
        set(handles.text11, 'String', 'mmHg');
        
    elseif graphed==3
        set(handles.text12, 'String', 'Raw ICP');
        set(handles.text13, 'String', 'Time in seconds');
        set(handles.text14, 'String', 'mmHg');
        
    elseif graphed==4
        set(handles.text15, 'String', 'Raw ICP');
        set(handles.text16, 'String', 'Time in seconds');
        set(handles.text17, 'String', 'mmHg');
    end
    
    if exist('eventTimes')==1
        for i=1:length(eventTimes)
            TimeIndex(i,1)=find(Time==eventTimes(i));
        end
        ICPindex=ICP(TimeIndex);
        hold(graph,'on')
        h=scatter(graph,eventTimes,ICPindex,'ro');
        set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
        
    end
    
elseif strcmp(listed(selected),'Raw ABP')==1
    
    cla(graph)
    set(graph, 'Xdir', 'normal')
    
    plot(graph,Time,ABP)
    if graphed==1
        %Title
        set(handles.text1, 'String', 'Raw ABP');
        %X values
        set(handles.text2, 'String', 'Time in seconds');
        %Y values
        set(handles.text3, 'String', 'mmHg');
        
    elseif graphed==2
        set(handles.text4, 'String', 'Raw ABP');
        set(handles.text10, 'String', 'Time in seconds');
        set(handles.text11, 'String', 'mmHg');
        
    elseif graphed==3
        set(handles.text12, 'String', 'Raw ABP');
        set(handles.text13, 'String', 'Time in seconds');
        set(handles.text14, 'String', 'mmHg');
        
    elseif graphed==4
        set(handles.text15, 'String', 'Raw ABP');
        set(handles.text16, 'String', 'Time in seconds');
        set(handles.text17, 'String', 'mmHg');
    end
    
    if exist('eventTimes')==1
        for i=1:length(eventTimes)
            TimeIndex(i,1)=find(Time==eventTimes(i));
        end
        ABPindex=ABP(TimeIndex);
        hold(graph,'on')
        h=scatter(graph,eventTimes,ABPindex,'ro');
        set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
    end
    
elseif strcmp(listed(selected),'Raw ECG')==1
    
    cla(graph)
    
    set(graph, 'Xdir', 'normal')
    
    plot(graph,Time,ECG)
    if graphed==1
        %Title
        set(handles.text1, 'String', 'Raw ECG');
        %X values
        set(handles.text2, 'String', 'Time in seconds');
        %Y values
        set(handles.text3, 'String', 'mV');
        
    elseif graphed==2
        set(handles.text4, 'String', 'Raw ECG');
        set(handles.text10, 'String', 'Time in seconds');
        set(handles.text11, 'String', 'mV');
        
    elseif graphed==3
        set(handles.text12, 'String', 'Raw ECG');
        set(handles.text13, 'String', 'Time in seconds');
        set(handles.text14, 'String', 'mV');
        
    elseif graphed==4
        set(handles.text15, 'String', 'Raw ECG');
        set(handles.text16, 'String', 'Time in seconds');
        set(handles.text17, 'String', 'mV');
    end
    
    for i=1:length(eventTimes)
        TimeIndex(i,1)=find(Time==eventTimes(i));
    end
    if exist('eventTimes')==1
        ECGindex=ECG(TimeIndex);
        hold(graph,'on')
        h=scatter(graph,eventTimes,ECGindex,'ro');
        set(h,'MarkerEdgeColor','r','MarkerFaceColor','r')
    end
    
    
elseif strcmp(listed(selected),'Packeted ABP')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')
    % ABP packeted data plot.
    plot(graph, PacketAvg_ABP.*100,'r.-');

    
    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'Packeted ABP');
        %X values
        set(handles.text2, 'String', 'Seconds');
        %Y values
        set(handles.text3, 'String', '');
        
    elseif graphed==2
        set(handles.text4, 'String', 'Packeted ABP');
        set(handles.text10, 'String', 'Seconds');
        set(handles.text11, 'String', '');
        
    elseif graphed==3
        set(handles.text12, 'String', 'Packeted ABP');
        set(handles.text13, 'String', 'Seconds');
        set(handles.text14, 'String', '');
        
    elseif graphed==4
        set(handles.text15, 'String', 'Packeted ABP');
        set(handles.text16, 'String', 'Seconds');
        set(handles.text17, 'String', '');
    end
    
elseif strcmp(listed(selected),'Packeted ICP')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')
    % ICP packeted data plot.
    plot(graph,PacketAvg_ICP.*100,'k.-');

    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'Packeted ICP');
        %X values
        set(handles.text2, 'String', 'Seconds');
        %Y values
        set(handles.text3, 'String', 'mmHg');
        
    elseif graphed==2
        set(handles.text4, 'String', 'Packeted ICP');
        set(handles.text10, 'String', 'Seconds');
        set(handles.text11, 'String', 'mmHg');
        
    elseif graphed==3
        set(handles.text12, 'String', 'Packeted ICP');
        set(handles.text13, 'String', 'Seconds');
        set(handles.text14, 'String', 'mmHg');
        
    elseif graphed==4
        set(handles.text15, 'String', 'Packeted ICP');
        set(handles.text16, 'String', 'Seconds');
        set(handles.text17, 'String', 'mmHg');
    end
    
elseif strcmp(listed(selected),'PRx 5 Minute Packet')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')

    plot(graph,PacketAvg_Time,PRx(:,1), 'b.-'); hold on;

    hold off;
    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'PRx 5 Minute Packet');
        %X values
        set(handles.text2, 'String', '');
        %Y values
        set(handles.text3, 'String', '');
        
    elseif graphed==2
        set(handles.text4, 'String', 'PRx 5 Minute Packet');
        set(handles.text10, 'String', '');
        set(handles.text11, 'String', '');
        
    elseif graphed==3
        set(handles.text12, 'String', 'PRx 5 Minute Packet');
        set(handles.text13, 'String', '');
        set(handles.text14, 'String', '');
        
    elseif graphed==4
        set(handles.text15, 'String', 'PRx 5 Minute Packet');
        set(handles.text16, 'String', '');
        set(handles.text17, 'String', '');
    end
elseif strcmp(listed(selected),'PRx 10 Minute Packet')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')

    plot(graph,PacketAvg_Time,PRx(:,2), 'b.-'); hold on;

    hold off;
    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'PRx 10 Minute Packet');
        %X values
        set(handles.text2, 'String', '');
        %Y values
        set(handles.text3, 'String', '');
        
    elseif graphed==2
        set(handles.text4, 'String', 'PRx 10 Minute Packet');
        set(handles.text10, 'String', '');
        set(handles.text11, 'String', '');
        
    elseif graphed==3
        set(handles.text12, 'String', 'PRx 10 Minute Packet');
        set(handles.text13, 'String', '');
        set(handles.text14, 'String', '');
        
    elseif graphed==4
        set(handles.text15, 'String', 'PRx 10 Minute Packet');
        set(handles.text16, 'String', '');
        set(handles.text17, 'String', '');
    end
elseif strcmp(listed(selected),'PRx 20 Minute Packet')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')

    
    plot(graph,PRx(:,3), 'b.-');  hold on;

    hold off;
    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'PRx 20 Minute Packet');
        %X values
        set(handles.text2, 'String', '');
        %Y values
        set(handles.text3, 'String', '');
        
    elseif graphed==2
        set(handles.text4, 'String', 'PRx 20 Minute Packet');
        set(handles.text10, 'String', '');
        set(handles.text11, 'String', '');
        
    elseif graphed==3
        set(handles.text12, 'String', 'PRx 20 Minute Packet');
        set(handles.text13, 'String', '');
        set(handles.text14, 'String', '');
        
    elseif graphed==4
        set(handles.text15, 'String', 'PRx 20 Minute Packet');
        set(handles.text16, 'String', '');
        set(handles.text17, 'String', '');
    end
    
elseif strcmp(listed(selected),'Event Finder')==1
    cla(graph)
    
    set(graph, 'Xdir', 'normal')

    
    plot(graph,PRx(:,3), 'b.-');  hold on;

    hold off;
    if graphed==1
        
        %Title
        set(handles.text1, 'String', 'PRx 20 Minute Packet');
        %X values
        set(handles.text2, 'String', '');
        %Y values
        set(handles.text3, 'String', '');
        
    elseif graphed==2
        set(handles.text4, 'String', 'PRx 20 Minute Packet');
        set(handles.text10, 'String', '');
        set(handles.text11, 'String', '');
        
    elseif graphed==3
        set(handles.text12, 'String', 'PRx 20 Minute Packet');
        set(handles.text13, 'String', '');
        set(handles.text14, 'String', '');
        
    elseif graphed==4
        set(handles.text15, 'String', 'PRx 20 Minute Packet');
        set(handles.text16, 'String', '');
        set(handles.text17, 'String', '');
    end
end

function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function listbox3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

cla(handles.axes1)
cla(handles.axes2)
cla(handles.axes3)
cla(handles.axes4)
set(handles.text1, 'String', '');
set(handles.text2, 'String', '');
set(handles.text3, 'String', ' ');
set(handles.text4, 'String', '');
set(handles.text10, 'String', '');
set(handles.text11, 'String', ' ');
set(handles.text12, 'String', '');
set(handles.text13, 'String', '');
set(handles.text14, 'String', ' ');
set(handles.text15, 'String', '');
set(handles.text16, 'String', '');
set(handles.text17, 'String', ' ');

function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global PRx
global PacketAvg_Time
global PacketAvg_ABP
global PacketAvg_ICP
global ICP
global ABP
global Time

h=waitbar(0,'Please Wait...');

edit8=str2num(char(get(handles.edit8,'String')));edit9=str2num(char(get(handles.edit9,'String')));
SelectedHours=100000000000000000;
set(handles.text42,'String','Please select LVM folder')
[PRx,PacketAvg_Time,PacketAvg_ABP,PacketAvg_ICP]=CHARISGUIpacketAve2(1,SelectedHours,ABP,ICP,Time,1);
waitbar(1/2)
cla(handles.axes1);
cla(handles.axes2);
cla(handles.axes3);
cla(handles.axes4);

% ABP packeted data plot.
plot(handles.axes1,PacketAvg_Time, PacketAvg_ABP.*100,'r.-');

set(handles.text3,'String','Seconds')
set(handles.text1,'String','Packeted ABP')

% ICP packeted data plot.
plot(handles.axes2,PacketAvg_Time,PacketAvg_ICP.*100,'k.-');

set(handles.text4,'String','Packeted ICP')
set(handles.text11,'String','Seconds')

% 5 minutes 
plot(handles.axes3,PacketAvg_Time,PRx(:,1), 'b.-'); hold on;

hold off
set(handles.text12,'String','PRx 5 Minute Packet')
set(handles.text13,'String','')

% 10 minutes 
plot(handles.axes4,PacketAvg_Time,PRx(:,2), 'b.-'); hold on;

hold off
set(handles.text15,'String','PRx 10 Minute Packet');
set(handles.text16,'String','')
set(handles.text42,'String','Finished Packet Averaging All')
waitbar(1)
close(h)

% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global PRx
global PacketAvg_Time
global PacketAvg_ABP
global PacketAvg_ICP
global ICP
global ABP
global ECG
global Time
global PatientData
global eventTimes
global critStr
global ICPevent_index

set(handles.text42, 'String', 'Please select a folder to save CHARIS Data.mat');
edit17=get(handles.edit17,'String');
dirNewSave = uigetdir('Select a folder to save CHARIS Data.mat');
olddir=pwd;
cd(dirNewSave)
str1=sprintf('CHARIS Data %s (All Data).mat',edit17);
save(str1,'PRx','PacketAvg_Time','PacketAvg_ABP','PacketAvg_ICP','ICP','ABP','ECG','Time','ICPevent_index','eventTimes');

str3=sprintf('Patient Object %s.mat',edit17);
save(str3,'PatientData');

fileStr=sprintf('Patient %s Criteria.txt',edit17);
fid=fopen(fileStr,'wt');
fprintf(fid,critStr);
fclose(fid);

cd(olddir)
set(handles.text42,'String', 'Finished Saving Data')

function SICUNI62101405051824 = importLVMfile(filename, startRow, endRow)

%% Initialize variables.
delimiter = '\t';
if nargin<=2
    startRow = 25;
    endRow = inf;
end

%% Format string for each line of text:
%   column1: double (%f)
%	column2: double (%f)
%   column3: double (%f)
%	column4: double (%f)
%   column5: double (%f)
%	column6: double (%f)
%   column7: double (%f)
%	column8: double (%f)
%   column9: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%f%f%f%*s%*s%*s%*s%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end

%% Close the text file.
fclose(fileID);


%% Create output variable
SICUNI62101405051824 = [dataArray{1:end-1}];