Code covered by the BSD License  

Highlights from
Matlab GUI for Polhemus Digitizer(POLGUI)

image thumbnail
from Matlab GUI for Polhemus Digitizer(POLGUI) by Nitin Bangera
MATLAB GUI interface for polhemus FASTTRAKĀ© used to digitize EEG scalp electrode locations.

polgui_ver5_r14(varargin)
function varargout = polgui_ver5_r14(varargin)
% -------------------------------------------------------------------------
% Copyright (C) 2004 , Nitin B. Bangera  (nitinb2@gmail.com)
% POLGUI Ver 5.0 is distributed under the terms of the open-source BSD License,
% See license.txt for more information.
% -------------------------------------------------------------------------
%                   -- POLGUI Ver 5.0 for Matlab 7.0 (R14) -- 
% -------------------------------------------------------------------------
% Matlab Interface for Polhemus Digitizer (one stylus pen and 0-3 additional receivers)
% Author Nitin B Bangera 
% -------------------------------------------------------------------------
%  Usage :
%    POLGUI_VER5_R14 M-file for polgui_ver5_r14.fig
%      ------ polgui_ver5_r14(no_of_receivers) opens GUI for Polhemus with total
%      number of receivers specified (total includes the stylus pen)
% 
%      ------ polgui_ver5_r14, by itself, creates a new POLGUI_VER5_R14 with
%      default settings of 2 receivers ( stylus pen + 1 receiver)
% 
%      ------ modify line 323 to change EEG Montage 
%      ------ modify line 389 and 391 to set Baud rate euivalent to that on
%      Polhemus (default set to 57600)
%      Baud Rate Settings on switch on back of Polhemus ( 1= UP, 0=Down)
%       Baud rate    SW 1  SW 2   SW 3
%         1200        0      0      0
%         2400        1      0      0
%         4800        0      1      0
%         9600        1      1      0
%         19200       0      0      1
%         38400       1      0      1
%         57600       0      1      1
%         115200      1      1      1
% 
% --------------------------------------------------------------------------
% Update from version 4.0:
% *** Universal Version--
%                       |---which works under Matlab R14 in both Windows and Linux. 
%                       |---Also works with 1 (stylus pen),2 ( stylus pen + 1 additional receiver),...
%                           3 (stylus pen + 2 additional receiver) or 4 (stylus pen + 3 additional receivers) receivers
% -------------------------------------------------------------------------
% Important Variables Saved: in file collected_data.mat
% -------------------------------------------------------------------------
% data_orig_pen --> Original Data including the 3 consecutive points_pen
% collected using stylus pen
% data_orig_rec2 -->Original Data including the 3 consecutive points_pen
% collected using receiver 2
% data_orig_rec3 --->Original Data including the 3 consecutive points_pen
% collected using receiver 3
% data_orig_rec4 --->Original Data including the 3 consecutive points_pen
% collected using receiver 4
% for each location for averaging and pen movement error detection
% data_pen -> Averaged data from pen
% data_rec2 ->  "            "   receiver 2
% data_rec3 ->  "            "   receiver 3 
% data_rec4 ->  "            "   receiver 4 
% data_transf_pen -> Data transformed to head co-ordinate frame from pen
% data_transf_rec2 -> Data transformed to head co-ordinate frame from rec2
% data_transf_rec3 -> Data transformed to head co-ordinate frame from rec3
% data_transf_rec4 -> Data transformed to head co-ordinate frame from rec4
% location -> Names of locations digitized
% count -> Total number of digitized points_pen
% M -> Rotation matrix for transformation
% T -> Translation matrix
% -------------------------------------------------------------------------
% Description of global variables and counters in the code 
% 
% needed_loc LOC DATA  DATA_TRANSF_PEN COUNTER COUNTER1 special_case strmatch_index redigitize additional T M   
% 
% needed_loc  -> chararcter array with the CARDINAL POINT locations
% and EEG montage locations
% LOC-> Set of digitized  locations
% DATA_ORIG_PEN --> Original Data including the 3 consecutive points_pen
% collected using stylus pen
% DATA_ORIG_REC2 -->Original Data including the 3 consecutive points_pen
% collected using receiver 2
% DATA_ORIG_REC3 --->Original Data including the 3 consecutive points_pen
% collected using receiver 3
% DATA_ORIG_REC4 --->Original Data including the 3 consecutive points_pen
% collected using receiver 4
% for each location for averaging and pen movement error detection
% DATA_PEN -> Averaged data from pen
% DATA_REC2 ->  "            "   receiver 2
% DATA_REC3 ->  "            "   receiver 3 
% DATA_REC4 ->  "            "   receiver 4 
% DATA_TRANSF_PEN -> Data after co-ordinate transformation from pen
% DATA_TRANSF_REC2 -> Data after co-ordinate transformation from rec2
% DATA_TRANSF_REC3 -> Data after co-ordinate transformation from rec3
% DATA_TRANSF_REC4 -> Data after co-ordinate transformation from rec4
% COUNTER -> Main Counter
% COUNTER1-> Counter which includes count for 3 successive digitization for each point
% Tag for 4 cases for remove button : 
% special_case
% Case 0-> Initial Condition  , Case 1-> counter <=4
% Case 2-> Counter between 4 and length of needed montage 
% Case 3-> Additional Points
% 
% strmatch_index ->  matches current Vaue in the listbox with needed_loc matrix and returns index 
% redigitize -> Tag to check if redigitization is needed Values: 0 or 1
% additional -> Tag to check if digitization of additional points_pen is needed Values: 0 or 1
% T -> Translation Matrix
% M -> Rotation Matrtix
% --------------------------------------------------------------------------
% Steps for head digitization using POLGUI
% 1.Before starting GUI, edit EEG_MONTAGE variable (on line 323 in this m-file) to set required
% montage to be acquired. 
% 
% 2. Start GUI by typing name of this m-file in MATLAB command line (GUI
% mode) e.g. polgui_ver5_r14 
% 
% 
% Follow these steps in GUI by clicking on respective buttons in sequential
% order
% a. Initialise Device: 
% Serial Object created with specific Baud rate. Change this call back to
% change serial port properties
% 
% To digitize position the tip of the Isotrack Stylus at the desired point
% and click on the dialog box within the GUI. The co-ordinates
% displayed are the averages of three sequential readings taken from the
% digitizer. If they differ from each other by more than 2 mm, you are asked
% to digitize the point again.
% 
% b. Get Test Point:
% Secure one test point
% 
% c. Get Cardinal Points in order LA(left Auricular), RA(Right Auricular)
% and Nasion.
% 
% d. Align Frame from device coordinates to head co-ordinate axes determined 
% by the cardinal points_pen
% Head Coordinate system:
% The head coordinate system is defined as folloew: The x axis passes through the 
% preauricular points_pen with positive values on the right, the y-axis will be
% perpendicular to x axis, passing through the nasion and the positive axis 
% pointing towards the nose, and the z axis will be pointed up, perpendicular to xy plane.
% The auricular points_pen are known as anatomical landmarks or cardinal points_pen.
% 
% Principle: 
% When Align Frame is press, the coordinates of the cardinal locations will 
% change to head frame. Notice the z coordinates of all three points_pen are
% zero. The y coordinates of the auricular points_pen are zero with a positive x
% coordinate on the right and a negative on the left auricular point,
% respectively. The nasion has  a zero x coordinate and its y coordinate is
% positive. The auricular points_pen will be almost symmetrically located: their 
% x coordinates are opposite and about of equal magnitude 
% 
% -Procedure for alignment-------------------------------------------------
% a1. Calculate the new origin coordinates o
% Define three vectors a(nasion-la), b1(ra-la) and b2(la-ra)
% Drop projection of a on b1 and b2 to calculatet1 and t2 respectively.
% find ratio in which projection from b1 (point o) divides vector b1
% o=la+t*b1   o=(Ox,Oy,Oz)
% 
% b1. Define the three new coordinate axes ox(normalised ra-o),oy(normalised
% nasion-o),oz(cross(ox,oy))
% 
% c1. First translate orgin by -o
% Define Translation matrix T as follows:
%     | 1   0   0    0|
%     | 0   1   0    0|
% T = | 0   0   1    0|
%     |-Ox -Oy -Oz   0|
% 
% d1. Then Rotate .Define Rotation Matrix M as folows:
% R=[ox;oy;oz]
% R1=inverse(R) or transpose(R)
%    |"  "  "|
%    |"  R1 "|
% M= |"  "  "|
%    |0  0  0|
% 
% e1. Transform points_pen to new co-ordinate axes with center at o and three
% axes ox,oy and oz
% [xnew ynew znew ]=[xold yold zold 1]*T*M;
% -------------------------------------------------------------------------
% e. Get EEG montage points_pen.
% 
% f. Get additional points_pen
% Fot additional points_pen , click the stylus pen for digitization.
% 
% g. Press stop to close the serial device and delete objects from memory.
% -------------------------------------------------------------------------
% 


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @polgui_ver5_r14_OpeningFcn, ...
                   'gui_OutputFcn',  @polgui_ver5_r14_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 polgui_ver5_r14 is made visible.
function polgui_ver5_r14_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 polgui_ver5_r14 (see VARARGIN)
% No of receivers given by passed varargin
handles.receivers=varargin;
temp_recv=cell2mat(handles.receivers);
% Default case , set it to scalar value for use in switch construct later
if isempty(temp_recv) temp_recv=0; end
% Initiales no of recievers as zero
handles.recv1=0;
handles.recv2=0;
handles.recv3=0;
handles.recv4=0;
resultStr_pen='Not Connected';
resultStr_rec2='Not Connected';
resultStr_rec3='Not Connected';
resultStr_rec4='Not Connected';

% Set No of receivers depending on passed varargin
switch temp_recv
    case 1
        handles.recv1=1;
        resultStr_pen='Connected';
        set(handles.points_pen,'String',resultStr_pen);
        set(handles.points_rec2,'String',resultStr_rec2);
        set(handles.points_rec3,'String',resultStr_rec3);
        set(handles.points_rec4,'String',resultStr_rec4);
    case 2
        handles.recv1=1;
        handles.recv2=1;
        resultStr_pen='Connected';
        resultStr_rec2='Connected';
        set(handles.points_pen,'String',resultStr_pen);
        set(handles.points_rec2,'String',resultStr_rec2);
        set(handles.points_rec3,'String',resultStr_rec3);
        set(handles.points_rec4,'String',resultStr_rec4);
    case 3 
        handles.recv1=1;
        handles.recv2=1;
        handles.recv3=1;
        resultStr_pen='Connected';
        resultStr_rec2='Connected';
        resultStr_rec3='Connected';
        set(handles.points_pen,'String',resultStr_pen);
        set(handles.points_rec2,'String',resultStr_rec2);
        set(handles.points_rec3,'String',resultStr_rec3);
        set(handles.points_rec4,'String',resultStr_rec4);
    case 4
        handles.recv1=1;
        handles.recv2=1;
        handles.recv3=1;
        handles.recv4=1;
        resultStr_pen='Connected';
        resultStr_rec2='Connected';
        resultStr_rec3='Connected';
        resultStr_rec4='Connected';
        set(handles.points_pen,'String',resultStr_pen);
        set(handles.points_rec2,'String',resultStr_rec2);
        set(handles.points_rec3,'String',resultStr_rec3);
        set(handles.points_rec4,'String',resultStr_rec4);
    otherwise
        handles.recv1=1;
        handles.recv2=1;  % default setting is stylus pen + 1 additional receiver 
        resultStr_pen='Connected';
        resultStr_rec2='Connected';
        set(handles.points_pen,'String',resultStr_pen);
        set(handles.points_rec2,'String',resultStr_rec2);
        set(handles.points_rec3,'String',resultStr_rec3);
        set(handles.points_rec4,'String',resultStr_rec4);
end
%handles       
set(hObject, 'Units', 'pixels');

handles.banner = imread('head.png'); % Read the image file banner.jpg
info = imfinfo('head.png'); % Determine the size of the image file
axes(handles.logo);
image(handles.banner)
set(handles.logo, ...
    'Visible', 'off', ...
    'Units', 'pixels');

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes polgui_ver5_r14 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%--------------------------------------------------------------------------

% --- Outputs from this function are returned to the command line.
function varargout = polgui_ver5_r14_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 initialise.
%Sets Baudrate and created serial object
% To change port name and baudrate change int this callback
function initialise_Callback(hObject, eventdata, handles)
% hObject    handle to initialise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% EEG Montage to be aquired . Pts 2,3 and 4 are cardinal points_pen
% Edit EEG_MONTAGE to change EEG Montage

EEG_MONTAGE=strvcat('Fp1','Fp2','F3','F4','C3','C4','P3',...
    'P4','O1','02','F7','F8','T3','T4','T5','T6','FZ','CZ','PZ','OZ','A1','A2','Ref','PO1',...
    'PO2','CP1','CP2','FTC1','FTC2','TCP1','TCP2','X31','X32','X33');

handles.needed_loc=strvcat('test','LA','RA','NASION',EEG_MONTAGE);

global special_case strmatch_index no_add additional redigitize T M k;
handles.location=[];  % Handles structure for location names
handles.data_pen=[];      % Handles Structure for final DATA 
handles.data_rec2=[];
handles.data_rec3=[];
handles.data_rec4=[];
handles.count=1;      % Handles structure for overall count of points digitized

% Plotting the tri file and the three cardinal points obtained from the
% mri(T1 or orig)
% load dummy_surface;
% handles.vertices=vertices_new;
% handles.tri=tri;
% handles.cardinal=[la ;ra ;nasion]
% set(handles.coordinates,'FaceColor','none','LineStyle',':');
% set(handles.coordinates,'BOX','on','XGrid','on','YGrid','on','ZGrid','on'); 
% [OBJECT, FIGURE] = GCBO
set(gcf,'CurrentAxes',handles.coordinates);
set(handles.coordinates,'BOX','on','XGrid','on','YGrid','on','ZGrid','on'); 
rotate3d on
% set(h,'Parent',handles.coordinates)
% set(handles2,'Parent');
% trisurf(handles.tri,handles.vertices(:,1),handles.vertices(:,2),handles.v
% ertices(:,3),'FaceAlpha',0.7,'FaceLighting','gouraud','FaceColor','red','
% LineStyle','none');set(handles.coordinates,'DataAspectRatio',[1 1 1],'Color','none','Box','on');view([-151 22]);
% rotate3d on;
% hold(handles.coordinates,'on');
% 
% plot3(la(1,1),la(1,2),la(1,3),'r*')
% 
% plot3(ra(1,1),ra(1,2),ra(1,3),'r*')
% 
% plot3(nasion(1,1),nasion(1,2),nasion(1,3),'r*')

special_case=0;% Tag for 4 cases for remove button : 
% Case 0-> Initial Condition  , Case 1-> counter <=4
% Case 2-> Counter between 4 and length of needed montage 
% Case 3-> Additional Points

strmatch_index=0;
no_add=0;       
additional=0;   % Tag to get additonal points, Values:0 or 1
redigitize=0;   % Tag to keep track  of redigitization ,Values 0 or 1
k=5;
str1=sprintf('\n\n\nInitialising device \n');
set(handles.points_pen,'String',[]);
if (handles.recv2==1) set(handles.points_rec2,'String',[]); end
if (handles.recv3==1) set(handles.points_rec3,'String',[]); end
if (handles.recv4==1) set(handles.points_rec4,'String',[]); end
set(handles.text1,'String',str1);
set(handles.remove,'Enable','on');
set(handles.get_test,'Enable','on');
set(handles.align,'Enable','off');
set(handles.get_la,'Enable','off');
set(handles.get_ra,'Enable','off');
set(handles.get_nasion,'Enable','off');
set(handles.get_montage,'Enable','off');
set(handles.get_additional,'Enable','off');
set(handles.stop,'Enable','on');
Create serial object and set baud rate 
tmp_pc=ispc;
if (tmp_pc)
    handles.s=serial('COM1','BaudRate',57600);
else
    handles.s=serial('/dev/ttyS0','BaudRate',57600);
end
%handles.s=serial('COM1','BaudRate',57600);
guidata(hObject,handles);
str_text1=sprintf('\nDEVICE READY \nRemove : deletes last digitized point');
str_text2=sprintf('\n\nGet Test Point');
set(handles.text2,'String',str_text2);
set(handles.text1,'String',str_text1);
set(handles.initialise,'Enable','off');
set(handles.text_nasion,'String','No Points Digitized');
set(handles.text_la,'String','No Points Digitized');
set(handles.text_ra,'String','No Points Digitized');


%--------------------------------------------------------------------------
% --- Executes on button press in stop.
% Save data in mat file collected_data
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.initialise,'Enable','on');
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;

data_orig_pen=DATA_ORIG_PEN;
data_orig_rec2=DATA_ORIG_REC2;
data_orig_rec3=DATA_ORIG_REC3;
data_orig_rec4=DATA_ORIG_REC4;
data_pen=DATA_PEN;
data_rec2=DATA_REC2;
data_rec3=DATA_REC3;
data_rec4=DATA_REC4;
data_transf_pen=DATA_TRANSF_PEN;
data_transf_rec2=DATA_TRANSF_REC2;
data_transf_rec3=DATA_TRANSF_REC3;
data_transf_rec4=DATA_TRANSF_REC4;
count=COUNTER-1;
location=LOC;
COUNTER1=[];
COUNTER=[];
DATA_ORIG_PEN=[];
DATA_ORIG_REC2=[];
DATA_ORIG_REC3=[];
DATA_ORIG_REC4=[];
DATA_PEN=[];
DATA_REC2=[];
DATA_REC3=[];
DATA_REC4=[];
DATA_TRANSF_PEN=[];
DATA_TRANSF_REC2=[];
DATA_TRANSF_REC3=[];
DATA_TRANSF_REC4=[];
LOC=[];
save collected_data data_orig_pen  data_orig_rec2 data_orig_rec3 data_orig_rec4 data_pen data_rec2 data_rec3 data_rec4 data_transf_pen data_transf_rec2  data_transf_rec3 data_transf_rec4 count location T M;
hold(handles.coordinates,'off');
fclose(handles.s);
delete(handles.s);
clear handles.s;
clear handles.data_pen;
clear handles.location;
clear handles.T; 
clear handles.M;
guidata(hObject,handles);
str3=sprintf('\n\nPOLGUI Ver 5.0');
str4=sprintf('\n\nPoints Digitized\nPress "Initialise" to Start');
set(handles.text1,'String',str3);
set(handles.text2,'String',str4);
set(handles.remove,'Enable','off');
set(handles.stop,'Enable','off');
set(handles.remove,'Enable','on');
set(handles.get_test,'Enable','on');
set(handles.align,'Enable','on');
set(handles.get_la,'Enable','on');
set(handles.get_ra,'Enable','on');
set(handles.get_nasion,'Enable','on');
set(handles.get_montage,'Enable','on');
set(handles.get_additional,'Enable','on');
clear data_pen data_rec2 data_rec3 data_rec4 data_orig_pen data_orig_rec2 data_orig_rec3 data_orig_rec4 data_transf_pen count location T M;


%--------------------------------------------------------------------------
% --- Executes on button press in get_test.
% Secure test point
function get_test_Callback(hObject, eventdata, handles)
% hObject    handle to get_test (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 T M redigitize;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;

if isempty(COUNTER) COUNTER=1; end
if isempty(COUNTER1) COUNTER1=1; end

temp_recv=cell2mat(handles.receivers);
% Default case , set it to scalar value for use in switch construct later
if isempty(temp_recv) temp_recv=0; end

if (strcmp(handles.s.status,'closed'))
    %handles.s.BytesAvailableFcnCount=48;
   % Changed from 48 to 576 since 4 receivers instead of just 1
   % Set No of receivers depending on passed varargin
    switch temp_recv
        case 1
            handles.s.InputBufferSize=48;
            handles.s.BytesAvailableFcnCount=48;
        case 2
            handles.s.InputBufferSize=432;
            handles.s.BytesAvailableFcnCount=432;   
        case 3 
            handles.s.InputBufferSize=432;
            handles.s.BytesAvailableFcnCount=432;
        case 4
            handles.s.InputBufferSize=576;
            handles.s.BytesAvailableFcnCount=576;
        otherwise 
            handles.s.InputBufferSize=432;
            handles.s.BytesAvailableFcnCount=432;   
    end
% 
%     handles.s.InputBufferSize=576;
%     handles.s.BytesAvailableFcnCount=576;
    % checked no of bytes sent from 3 receivers using the test matlab code
    handles.s.BytesAvailableFcnMode = 'byte';
    handles.s.BytesAvailableFcn = {@bytesavailablegui_callback,handles};
    fopen(handles.s);
end
% This loop send request for three successive digitizations which is
% averaged and checked for pen movement
for i=1:3,
%     i
%     COUNTER1
    fprintf(handles.s,'%s','P');
    pause(0.2);
end
% handles.s;

if ~isequal(redigitize,1)
    str_text2=sprintf('\n\n\nGet Cardinal Point LA');
    set(handles.text2,'String',str_text2);
    set(handles.get_la,'Enable','on');
    set(handles.get_test,'Enable','off')
end
guidata(hObject,handles);

%--------------------------------------------------------------------------
% --- Executes on button press in get_la.
% Secure Cardinal Point LA(Left Auricular)
function get_la_Callback(hObject, eventdata, handles)
% hObject    handle to get_la (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 T M redigitize;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;

% This loop send request for three successive digitizations which is
% averaged and checked for pen movement
for i=1:3,
%     i
%     COUNTER1
    fprintf(handles.s,'%s','P');
    pause(0.2);
end

if ~isequal(redigitize,1)
    str_text2=sprintf('\n\n\nGet Cardinal Point RA');
    set(handles.text2,'String',str_text2);
    set(handles.get_ra,'Enable','on');
    set(handles.get_la,'Enable','off');
end
guidata(hObject,handles);

% --- Executes on button press in get_ra.
% Secure Cardinal Point RA(Right Auriculur)
function get_ra_Callback(hObject, eventdata, handles)
% hObject    handle to get_ra (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 T M redigitize;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;

% This loop send request for three successive digitizations which is
% averaged and checked for pen movement
for i=1:3,
%     i
%     COUNTER1
    fprintf(handles.s,'%s','P');
    pause(0.2);
end
if ~isequal(redigitize,1)
    str_text2=sprintf('\n\n\nGet Cardinal Point NASION');
    set(handles.text2,'String',str_text2);
    set(handles.get_nasion,'Enable','on');
    set(handles.get_ra,'Enable','off');
end
guidata(hObject,handles);

%--------------------------------------------------------------------------
% --- Executes on button press in get_nasion.
% Secure Cardinal Point Nasion
function get_nasion_Callback(hObject, eventdata, handles)
% hObject    handle to get_nasion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 T M redigitize;

global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;
% This loop send request for three successive digitizations which is
% averaged and checked for pen movement
for i=1:3,
%     i
%     COUNTER1
    fprintf(handles.s,'%s','P');
    pause(0.2);
end
if ~isequal(redigitize,1)
    str_nasion=sprintf('\n\nCARDINAL POINTS OBTAINED\n\nALIGN FRAME TO HEAD CO-ORDINATE SYSTEM');   
    set(handles.text2,'String',str_nasion);
    set(handles.align,'Enable','on');
    set(handles.get_nasion,'Enable','off');
end
guidata(hObject,handles);


%--------------------------------------------------------------------------
% --- Executes on button press in get_montage.
% Secure EEG Montage
function get_montage_Callback(hObject, eventdata, handles)
% hObject    handle to get_montage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global LOC DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 T M redigitize k str_modal;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k str_modal;
%global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN DATA_REC2 DATA_REC3 DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_TRANSF_REC2 DATA_TRANSF_REC3 COUNTER1 COUNTER LOC  count T M redigitize k str_modal ;
set(handles.get_montage,'Enable','off');
set(handles.remove,'Enable','Off');

while k<=length(handles.needed_loc) % Check if loop is within the EEG montage points count
    if isequal(k,5)
            str_modal=sprintf('Get Point %s\nPress ESC to exit',handles.needed_loc(5,:));
            str_text2=sprintf('\n\n Get Point %s\nPress ESC to exit',handles.needed_loc(5,:));
            set(handles.text2,'String',str_text2);
    end
            
    pos_size = get(handles.figure1,'Position');
    user_response = montage_modaldlg_r14('Title','Digitize','String',str_modal);
        switch user_response 
            case 'OK'    % Digitize Point
               for i=1:3,
%                i
%                COUNTER1
               fprintf(handles.s,'%s','P');
               pause(0.2);
               end
            case 'REMOVE LAST PT'  % Remove Last digitized Point
                    k=k-1;
                    resultStr_pen= get(handles.points_pen,'String');
                    resultStr_rec2= get(handles.points_rec2,'String');
                    resultStr_rec3= get(handles.points_rec3,'String');
                    resultStr_rec4= get(handles.points_rec4,'String');
                    resultStr_pen(k) =[];
                    if (handles.recv2==1) resultStr_rec2(k) =[]; end
                    if (handles.recv3==1) resultStr_rec3(k) =[]; end
                    if (handles.recv4==1) resultStr_rec4(k) =[]; end
                    DATA_PEN(k,:)=[];
                    DATA_TRANSF_PEN(k,:)=[];
                    if (handles.recv2==1) 
                        DATA_REC2(k,:)=[];
                        DATA_TRANSF_REC2(k,:)=[];
                    end
                    if (handles.recv3==1)
                        DATA_REC3(k,:)=[];
                        DATA_TRANSF_REC3(k,:)=[];
                    end
                    if (handles.recv4==1)
                        DATA_REC4(k,:)=[];
                        DATA_TRANSF_REC4(k,:)=[];
                    end
                    set(handles.points_pen,'String',resultStr_pen);
                    set(handles.points_rec2,'String',resultStr_rec2);
                    set(handles.points_rec3,'String',resultStr_rec3);
                    set(handles.points_rec4,'String',resultStr_rec4);
                    LOC(k,:)=[];
                    redigitize=1;
                    COUNTER1=COUNTER1-3;
                    COUNTER=COUNTER-1;
            case 'No'
               str_text2=sprintf('\n\nPress STOP to finish digitization\nOR\nGet additional Points');
               set(handles.text2,'String',str_text2);
               set(handles.get_additional,'Enable','Off')
               break;
        end
     pause(0.5);
    k=k+1;
    if (redigitize==1) 
        redigitize
        k=k-1;
        str_text2=sprintf('\n\nRedigitize Point %s\nPress ESC to exit',handles.needed_loc(k,:));
        str_modal=sprintf('Redigitize Point %s\nPress ESC to exit',handles.needed_loc(k,:));
        set(handles.text2,'String',str_text2);
        redigitize=0;
    elseif ~isequal(k,length(handles.needed_loc)+1)
        str_text2=sprintf('\n\nGet Point %s\nPress ESC to exit',handles.needed_loc(k,:));
        str_modal=sprintf('Get Point %s\nPress ESC to exit',handles.needed_loc(k,:));
        set(handles.text2,'String',str_text2);
    else
        str_text2=sprintf('\n\nGet Additional Points if needed');
        set(handles.text2,'String',str_text2);
    end
    if k==length(handles.needed_loc)+1
        str_modal1=sprintf('EEG Montage Digitized\nPress OK to Continue or \nRemove Last Point?');
        user_response = montage_modaldlg_r14('Title','Digitize','String',str_modal1);
        switch user_response 
            case 'OK'
              str_text2=sprintf('\nPress STOP to finish digitization\nOR\nGet additional Points');
              set(handles.text2,'String',str_text2);
              set(handles.get_additional,'Enable','On')
              break;
            case 'REMOVE LAST PT'
                    k=k-1;
                    resultStr_pen= get(handles.points_pen,'String');
                    resultStr_rec2= get(handles.points_rec2,'String');
                    resultStr_rec3= get(handles.points_rec3,'String');
                    resultStr_rec4= get(handles.points_rec4,'String');
                    resultStr_pen(k) =[];
                    if (handles.recv2==1) resultStr_rec2(k) =[]; end
                    if (handles.recv3==1) resultStr_rec3(k) =[]; end
                    if (handles.recv4==1) resultStr_rec4(k) =[]; end
%                     resultStr_pen(k) =[];
%                     resultStr_rec2(k) =[];
%                     resultStr_rec3(k) =[];
%                     resultStr_rec4(k) =[];
                    DATA_PEN(k,:)=[];
                    DATA_TRANSF_PEN(k,:)=[];
                    if (handles.recv2==1)
                        DATA_REC2(k,:)=[];
                        DATA_TRANSF_REC2(k,:)=[];
                    end
                    if (handles.recv3==1)
                        DATA_REC3(k,:)=[];
                        DATA_TRANSF_REC3(k,:)=[];
                    end
                    if (handles.recv4==1)
                        DATA_REC4(k,:)=[];
                        DATA_TRANSF_REC4(k,:)=[];
                    end
                    set(handles.points_pen,'String',resultStr_pen);
                    set(handles.points_rec2,'String',resultStr_rec2);
                    set(handles.points_rec3,'String',resultStr_rec3);
                    set(handles.points_rec4,'String',resultStr_rec3);
                    LOC(k,:)=[];
                    redigitize=1;
                    COUNTER1=COUNTER1-3;
                    COUNTER=COUNTER-1;
                    str_modal=sprintf('Redigitize Point %s\nPress ESC to exit',handles.needed_loc(k,:));
            case 'No'
               str_text2=sprintf('\nPress STOP to finish digitization\nOR\nGet additional Points');
               set(handles.text2,'String',str_text2);
               set(handles.get_additional,'Enable','Off')
               break;
        end
    end
end
set(handles.get_additional,'Enable','on');
set(handles.remove,'Enable','On');
guidata(hObject,handles);
% set(handles.get_additional,'Enable','on');
        
%--------------------------------------------------------------------------
% --- Executes on button press in get_additional.
function get_additional_Callback(hObject, eventdata, handles)
% hObject    handle to get_additional (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN COUNTER COUNTER1 additional T M;
% global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN DATA_REC2 DATA_REC3 DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_TRANSF_REC2 DATA_TRANSF_REC3 COUNTER1 COUNTER LOC  count T M additional redigitize;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;
set(handles.get_additional,'Enable','off');
additional=1;
str_additional=sprintf('Press Stylus pen to digitize additional points\n Press OK to continue');
uiwait(msgbox(str_additional,'Get Additional Points','help','modal'));


%--------------------------------------------------------------------------
% --- Executes on button press in align.
% Align from device coordinates to head coordinates
function align_Callback(hObject, eventdata, handles)
% hObject    handle to align (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global DATA_PEN DATA_TRANSF_PEN COUNTER T M LOC;
global DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;
handles.data_pen=DATA_PEN;
if (handles.recv2==1)
    handles.data_rec2=DATA_REC2;
end
if (handles.recv3==1)
    handles.data_rec3=DATA_REC3;
end
if (handles.recv4==1)
    handles.data_rec4=DATA_REC4;
end
hold(handles.coordinates,'off');
la1=handles.data_pen(2,:);
ra2=handles.data_pen(3,:);
nasion=handles.data_pen(4,:);
% rest=data(5:end,:)
a=nasion-la1;
b1=ra2-la1;
b2=la1-ra2;
c=nasion-ra2;
t1=dot(a,b1)/sqrt(dot(b1,b1));
t2=dot(c,b2)/sqrt(dot(b2,b2));
if t1<0 t1=t1*-1;
end
if t2<0 t2=t2*-1;
end
t=t1/(t1+t2);
o=la1+t*b1;

oy=nasion-o;
ox=ra2-o;
ox=ox/sqrt(dot(ox,ox));
oy=oy/sqrt(dot(oy,oy));
oz=cross(ox,oy);
% oz1=o+2*oz

T=[ 1 0 0 0; 0 1 0 0 ;0 0 1 0;-o 0];
R=[ox;oy;oz];
R1=inv(R);
M=[R1 ;0 0 0]; 
% Transformed points_pen from original points_pen
la=[la1 1]*T*M;
ra=[ra2 1]*T*M;
nasion=[nasion 1]*T*M;
% rest_new=rest_new*T*M;

% Another way to write the matrix -> equivalent to above method
% R=[ox 0;oy 0;oz 0; 0 0 0 1];
% T=[1 0 0 -o(1,1);0 1 0 -o(1,2); 0 0 1 -o(1,3); 0 0 0 1];
% T_dev2head=R*T;
% la=T_dev2head*[la1 1]';

% set(handles.coordinates,'BOX','on','XGrid','on','Ygrid','on','ZGrid','on','ZLim',[0 15]);
plot3([0 5],[0 0],[0 0],'->','Parent',handles.coordinates)
hold(handles.coordinates,'on');
plot3([0 0],[0 5],[0 0],'->','Parent',handles.coordinates)
% hold(handles.coordinates,'on');
plot3([0 0],[0 0],[0 5],'->','Parent',handles.coordinates)
% hold(handles.coordinates,'on');
plot3([la(1,1) ra(1,1) nasion(1,1)],[la(1,2) ra(1,2) nasion(1,2)],[la(1,3) ra(1,3) nasion(1,3)],'*','Parent',handles.coordinates);
hold(handles.coordinates,'on');
set(handles.coordinates,'BOX','on','XGrid','on','Ygrid','on','ZGrid','on','ZLim',[-2 15]);
xlabel(handles.coordinates,'X','Fontweight','bold');
ylabel(handles.coordinates,'Y','Fontweight','bold');
zlabel(handles.coordinates,'Z','Fontweight','bold');
sprintf('Align\n');
handles.center=o;
set(handles.align,'Enable','off');
set(handles.get_montage,'Enable','on');
str_align=sprintf('\n\nGet EEG Montage\nGet Point %s',handles.needed_loc(COUNTER,:));
set(handles.text2,'String',str_align);
guidata(hObject,handles);


ResultsStr_pen = get(handles.points_pen,'String');
ResultsStr_rec2 = get(handles.points_rec2,'String');
ResultsStr_rec3 = get(handles.points_rec3,'String');
ResultsStr_rec4 = get(handles.points_rec4,'String');
DATA_TRANSF_PEN(1:4,:)=[DATA_PEN(1,:) 1;DATA_PEN(2,:) 1;DATA_PEN(3,:) 1;DATA_PEN(4,:) 1]*T*M;
if (handles.recv2==1)
    DATA_TRANSF_REC2(1:4,:)=[DATA_REC2(1,:) 1;DATA_REC2(2,:) 1;DATA_REC2(3,:) 1;DATA_REC2(4,:) 1]*T*M;
end
if (handles.recv3==1)   
    DATA_TRANSF_REC3(1:4,:)=[DATA_REC3(1,:) 1;DATA_REC3(2,:) 1;DATA_REC3(3,:) 1;DATA_REC3(4,:) 1]*T*M; 
end
if (handles.recv4==1)
    DATA_TRANSF_REC4(1:4,:)=[DATA_REC4(1,:) 1;DATA_REC4(2,:) 1;DATA_REC4(3,:) 1;DATA_REC4(4,:) 1]*T*M;
end
handles.location=LOC;
[r,c]=find(abs(DATA_TRANSF_PEN)<1e-5);
     for i=1:length(r)
         for j=1:length(c)
             if (i==j)
                 DATA_TRANSF_PEN(r(i),c(i))=0;
             end
         end
     end
     
str_la=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_TRANSF_PEN(2,1),DATA_TRANSF_PEN(2,2),DATA_TRANSF_PEN(2,3)); 
set(handles.text_la,'String',str_la);
str_ra=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_TRANSF_PEN(3,1),DATA_TRANSF_PEN(3,2),DATA_TRANSF_PEN(3,3));
set(handles.text_ra,'String',str_ra); 
str_nasion=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_TRANSF_PEN(4,1),DATA_TRANSF_PEN(4,2),DATA_TRANSF_PEN(4,3));
set(handles.text_nasion,'String',str_nasion);

ResultsStr_pen(1,:)={[handles.location(1,:),'   :   ',num2str(DATA_TRANSF_PEN(1,1)),'   ',num2str(DATA_TRANSF_PEN(1,2)),'   ',num2str(DATA_TRANSF_PEN(1,3))]};
ResultsStr_pen(2,:)={[handles.location(2,:),'   :   ',num2str(DATA_TRANSF_PEN(2,1)),'   ',num2str(DATA_TRANSF_PEN(2,2)),'   ',num2str(DATA_TRANSF_PEN(2,3))]};
ResultsStr_pen(3,:)={[handles.location(3,:),'   :   ',num2str(DATA_TRANSF_PEN(3,1)),'   ',num2str(DATA_TRANSF_PEN(3,2)),'   ',num2str(DATA_TRANSF_PEN(3,3))]};
ResultsStr_pen(4,:)={[handles.location(4,:),'   :   ',num2str(DATA_TRANSF_PEN(4,1)),'   ',num2str(DATA_TRANSF_PEN(4,2)),'   ',num2str(DATA_TRANSF_PEN(4,3))]};

if (handles.recv2==1) 
    ResultsStr_rec2(1,:)={[handles.location(1,:),'   :   ',num2str(DATA_TRANSF_REC2(1,1)),'   ',num2str(DATA_TRANSF_REC2(1,2)),'   ',num2str(DATA_TRANSF_REC2(1,3))]};
    ResultsStr_rec2(2,:)={[handles.location(2,:),'   :   ',num2str(DATA_TRANSF_REC2(2,1)),'   ',num2str(DATA_TRANSF_REC2(2,2)),'   ',num2str(DATA_TRANSF_REC2(2,3))]};
    ResultsStr_rec2(3,:)={[handles.location(3,:),'   :   ',num2str(DATA_TRANSF_REC2(3,1)),'   ',num2str(DATA_TRANSF_REC2(3,2)),'   ',num2str(DATA_TRANSF_REC2(3,3))]};
    ResultsStr_rec2(4,:)={[handles.location(4,:),'   :   ',num2str(DATA_TRANSF_REC2(4,1)),'   ',num2str(DATA_TRANSF_REC2(4,2)),'   ',num2str(DATA_TRANSF_REC2(4,3))]};
end

if (handles.recv3==1)
    ResultsStr_rec3(1,:)={[handles.location(1,:),'   :   ',num2str(DATA_TRANSF_REC3(1,1)),'   ',num2str(DATA_TRANSF_REC3(1,2)),'   ',num2str(DATA_TRANSF_REC3(1,3))]};
    ResultsStr_rec3(2,:)={[handles.location(2,:),'   :   ',num2str(DATA_TRANSF_REC3(2,1)),'   ',num2str(DATA_TRANSF_REC3(2,2)),'   ',num2str(DATA_TRANSF_REC3(2,3))]};
    ResultsStr_rec3(3,:)={[handles.location(3,:),'   :   ',num2str(DATA_TRANSF_REC3(3,1)),'   ',num2str(DATA_TRANSF_REC3(3,2)),'   ',num2str(DATA_TRANSF_REC3(3,3))]};
    ResultsStr_rec3(4,:)={[handles.location(4,:),'   :   ',num2str(DATA_TRANSF_REC3(4,1)),'   ',num2str(DATA_TRANSF_REC3(4,2)),'   ',num2str(DATA_TRANSF_REC3(4,3))]};
end 

if (handles.recv4==1)
    ResultsStr_rec4(1,:)={[handles.location(1,:),'   :   ',num2str(DATA_TRANSF_REC4(1,1)),'   ',num2str(DATA_TRANSF_REC4(1,2)),'   ',num2str(DATA_TRANSF_REC4(1,3))]};
    ResultsStr_rec4(2,:)={[handles.location(2,:),'   :   ',num2str(DATA_TRANSF_REC4(2,1)),'   ',num2str(DATA_TRANSF_REC4(2,2)),'   ',num2str(DATA_TRANSF_REC4(2,3))]};
    ResultsStr_rec4(3,:)={[handles.location(3,:),'   :   ',num2str(DATA_TRANSF_REC4(3,1)),'   ',num2str(DATA_TRANSF_REC4(3,2)),'   ',num2str(DATA_TRANSF_REC4(3,3))]};
    ResultsStr_rec4(4,:)={[handles.location(4,:),'   :   ',num2str(DATA_TRANSF_REC4(4,1)),'   ',num2str(DATA_TRANSF_REC4(4,2)),'   ',num2str(DATA_TRANSF_REC4(4,3))]};
end

handles.translation=T;
handles.rotation=M;
set(handles.points_pen,'String',ResultsStr_pen);
if (handles.recv2==1) set(handles.points_rec2,'String',ResultsStr_rec2); end
if (handles.recv3==1) set(handles.points_rec3,'String',ResultsStr_rec3); end
if (handles.recv4==1) set(handles.points_rec4,'String',ResultsStr_rec4); end
guidata(hObject,handles);




%--------------------------------------------------------------------------
% % --- Executes on selection change in points_pen.
% function points_pen_Callback(hObject, eventdata, handles)
% % hObject    handle to points_pen (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: contents = get(hObject,'String') returns points_pen contents as cell array
% %        contents{get(hObject,'Value')} returns selected item from points_pen


% --- Executes during object creation, after setting all properties.
function points_pen_CreateFcn(hObject, eventdata, handles)
% hObject    handle to points_pen (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%--------------------------------------------------------------------------
% --- Executes on button press in remove.
% Removes additional points_pen and last digitized points_pen only
function remove_Callback(hObject, eventdata, handles)
% hObject    handle to remove (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global special_case strmatch_index no_add DATA_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_PEN DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_PEN  DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4 COUNTER1 COUNTER LOC  count T M redigitize additional k;
% global special_case strmatch_index no_add LOC DATA_PEN DATA_TRANSF_PEN COUNTER COUNTER1;
currentVal=get(handles.points_pen,'Value');
handles.data_pen=DATA_TRANSF_PEN;
handles.data_rec2=DATA_TRANSF_REC2;
handles.data_rec3=DATA_TRANSF_REC3;
handles.data_rec4=DATA_TRANSF_REC4;

handles.location=LOC;
resultStr_pen= get(handles.points_pen,'String');
resultStr_rec2= get(handles.points_rec2,'String');
resultStr_rec3= get(handles.points_rec3,'String');
resultStr_rec4= get(handles.points_rec4,'String');
numResults = size(resultStr_pen,1);
strmatch_index=strmatch(handles.location(currentVal,:),handles.needed_loc,'exact');
if currentVal<numResults 
    uiwait(msgbox('User Can remove only the last digitized point','Error','error','modal'));
else 
    if strmatch(handles.location(currentVal,:),handles.needed_loc,'exact') & (numResults<=4)
        special_case=1;
        str6=sprintf('\n\n\nGET POINT %s',handles.location(currentVal,:));
        set(handles.text2,'String',str6);
        resultStr_pen(currentVal) =[];
        if (handles.recv2==1) resultStr_rec2(currentVal) =[]; end
        if (handles.recv3==1) resultStr_rec3(currentVal) =[]; end
        if (handles.recv4==1) resultStr_rec4(currentVal) =[]; end
        handles.data_pen(currentVal,:)=[];
        if (handles.recv2==1) handles.data_rec2(currentVal,:)=[]; end
        if (handles.recv3==1) handles.data_rec3(currentVal,:)=[]; end
        if (handles.recv4==1) handles.data_rec4(currentVal,:)=[]; end
      %  handles.data_rec(currentVal,:)=[];
        handles.location(currentVal,:)=[];
        DATA_PEN(currentVal,:)=[];
        if (handles.recv2==1) DATA_REC2(currentVal,:)=[]; end
        if (handles.recv3==1) DATA_REC3(currentVal,:)=[]; end
        if (handles.recv4==1) DATA_REC4(currentVal,:)=[]; end
        LOC(currentVal,:)=[];
        COUNTER1=COUNTER1-3;
        switch strmatch_index
            case 1
                set(handles.get_test,'Enable','on');
            case 2
                set(handles.get_la,'Enable','on');
            case 3
                set(handles.get_ra,'Enable','on');
            case 4
                set(handles.get_nasion,'Enable','on'); 
        end
elseif  strmatch(handles.location(currentVal,:),handles.needed_loc,'exact') & (numResults>4) & (numResults<=length(handles.needed_loc))
    special_case==2;
    set(handles.remove,'Enable','Off');
else
    pos_size = get(handles.figure1,'Position');
    % Call remove_modaldlg_r14 with the argument 'Position'.
        user_response = remove_modaldlg_r14('Title','Redigitize');
        switch user_response
        case {'No'}
             special_case=3;
             resultStr_pen(currentVal) =[];
             if (handles.recv2==1) resultStr_rec2(currentVal) =[]; end
             if (handles.recv3==1) resultStr_rec3(currentVal) =[]; end
             if (handles.recv4==1) resultStr_rec4(currentVal) =[]; end
             handles.data_pen(currentVal,:)=[];
             if (handles.recv2==1) handles.data_rec2(currentVal,:)=[]; end
             if (handles.recv3==1) handles.data_rec3(currentVal,:)=[]; end
             if (handles.recv4==1) handles.data_rec4(currentVal,:)=[]; end
             handles.location(currentVal,:)=[];
             LOC(currentVal,:)=[];
             if isequal(no_add,0) 
                  no_add=1;
             else
                 no_add=no_add+1;
             end


     % take no action
        case 'Yes'
     % 
             str7=sprintf('GET POINT %s',handles.location(currentVal,:));
             set(handles.text2,'String',str7);
             resultStr_pen(currentVal) =[];
             if (handles.recv2==1) resultStr_rec2(currentVal) =[]; end 
             if (handles.recv3==1) resultStr_rec3(currentVal) =[]; end
             if (handles.recv4==1) resultStr_rec4(currentVal) =[]; end
             
             handles.data_pen(currentVal,:)=[];
             if (handles.recv2==1) handles.data_rec2(currentVal,:)=[]; end
             if (handles.recv3==1) handles.data_rec3(currentVal,:)=[]; end
             if (handles.recv4==1) handles.data_rec4(currentVal,:)=[]; end
             handles.location(currentVal,:)=[];
            
             LOC(currentVal,:)=[];
        end
    end
    COUNTER=COUNTER-1;   
    % % If there are no other entries, disable the Remove and Plot 
    % button
    % and change the list sting to <empty>
    if isequal(numResults,length(currentVal)),
        resultsStr = {'<empty>'};
        currentVal = 1;
        set(handles.remove,'Enable','off')
    end
    % % Ensure that list box Value is valid, then reset Value and String
    currentVal = min(currentVal,size(resultStr_pen,1));
    set(handles.points_pen,'Value',currentVal,'String',resultStr_pen)
    if (handles.recv2==1) set(handles.points_rec2,'Value',currentVal,'String',resultStr_rec2); end
    if (handles.recv3==1) set(handles.points_rec3,'Value',currentVal,'String',resultStr_rec3); end
    if (handles.recv4==1) set(handles.points_rec4,'Value',currentVal,'String',resultStr_rec4); end
    % handles.location
    % LOC
    % handles.count
    % Store the new ResultsData
    guidata(hObject, handles);
end
guidata(hObject,handles);

%--------------------------------------------------------------------------
% --- Executes on button press in plot.
% Plot data on seperate figure 
function plot_Callback(hObject, eventdata, handles)
% hObject    handle to plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global DATA_TRANSF_PEN;
status=get(handles.stop,'Enable');
if isequal(status,'off')
    load collected_data.mat;
    DATA_TRANSF_PEN=data_transf_pen;
end 
plotColor = {'b','g','r','c','m','y','k'};

Plotdata_x1=DATA_TRANSF_PEN(2:end,1);
Plotdata_y1=DATA_TRANSF_PEN(2:end,2);
Plotdata_z1=DATA_TRANSF_PEN(2:end,3);

if ~isfield(handles,'PlotFigure') | ~ishandle(handles.PlotFigure),
    handles.PlotFigure = figure('Name','Polhemus Digitizer',...
        'Visible','off','NumberTitle','off',...
        'HandleVisibility','off','IntegerHandle','off');
    handles.PlotAxes = axes('Parent',handles.PlotFigure);
    guidata(hObject, handles)
end
% Plot data
pHandles = plot3(Plotdata_x1,Plotdata_y1,Plotdata_z1,'o','Parent',handles.PlotAxes);
set(handles.PlotAxes,'BOX','on','XGrid','on','YGrid','on','ZGrid','on');
xlabel(handles.PlotAxes,'X','Fontweight','bold','Fontsize',12);
ylabel(handles.PlotAxes,'Y','Fontweight','bold','Fontsize',12);
zlabel(handles.PlotAxes,'Z','Fontweight','bold','Fontsize',12);
clear DATA_TRANSF_PEN

% Make the figure visible and bring it forward
figure(handles.PlotFigure)

%--------------------------------------------------------------------------
% Bytesavailable callback executes when the byte count reaches the one
% specified in the handles.s.BytesAvailableFcnCount variable.
function bytesavailablegui_callback(s,bytesavailable,handles)
% global DATA_PEN DATA_ORIG DATA_TRANSF_PEN COUNTER1 COUNTER additional redigitize  ;
global DATA_PEN DATA_ORIG_PEN DATA_TRANSF_PEN DATA_REC2 DATA_REC3 DATA_REC4 DATA_ORIG_REC2 DATA_ORIG_REC3 DATA_ORIG_REC4 DATA_TRANSF_REC2 DATA_TRANSF_REC3 DATA_TRANSF_REC4  COUNTER1 COUNTER LOC  count T M redigitize additional;
global LOC;
global T M;
global special_case strmatch_index no_add ; 
set(handles.points_pen,'Value',1);
if (handles.recv2==1) set(handles.points_rec2,'Value',1); end
if (handles.recv3==1) set(handles.points_rec3,'Value',1); end
if (handles.recv4==1) set(handles.points_rec4,'Value',1); end

% total Number of receivers
temp_recv=cell2mat(handles.receivers);
% Default case , set it to scalar value for use in switch construct later
if isempty(temp_recv) temp_recv=0; end
% Each command of 'P' sends back 3 rows of data from 4
% receivers . e.g. total of 12 rows for 4 consecutive digitisation for 4 recv
switch temp_recv
    case 1
        total_rows=3;
    case 2
        total_rows=6;
    case 3
        total_rows=9;
    case 4
        total_rows=12;
    otherwise
        total_rows=6;
end
     


for j=1:total_rows, 
%     data_str=fgetl(s); 
%     t=str2num(data_str);
    tmp=fgetl(s);
    t=str2num(tmp);
    if size(t,2)~=7
        tmp1=find(tmp=='+' | tmp=='-');
        for tmp2=1:size(tmp1,2)
            if (tmp((tmp1(tmp2))-1))~=' '
                tmp(((tmp1(tmp2))+1):((size(tmp,2))+1))=tmp((tmp1(tmp2)):size(tmp,2));
                tmp(tmp1(tmp2))=' ';
                for tmp3=(tmp2+1):size(tmp1,1),
                    tmp1(tmp3)=tmp1(tmp3)+1
                end  
            end
        end
    end
    data_num(j,:)=str2num(tmp);
    clear  t tmp1 tmp2 tmp3 tmp; 
end
% Obtains data from digitizer when bytes-count
% reaches the limit specified by BytesAvailableFcnCount
% Format of data obtained for the current settings
% 1 2 3 4 5 6 7
% 1 ->01 Header
% 2 ->X position in cms
% 3-> Y position in cms
% 4-> Z position in cms
% 5-> Azimuth attitude in degrees
% 6-> Elevation attitude in degrees
% 7-> Roll attitude in degrees
% Change for your specific device settings
data_num(1,2:4); % Picks the X,Y,Z data

if isequal(additional,1) & COUNTER>4 % For additional points
    DATA_PEN(COUNTER,1:3)=data_num(1,2:4);
    if (handles.recv2==1) DATA_REC2(COUNTER,1:3)=data_num(2,2:4); end
    if (handles.recv3==1) DATA_REC3(COUNTER,1:3)=data_num(3,2:4); end
    if (handles.recv4==1) DATA_REC4(COUNTER,1:3)=data_num(4,2:4); end
    handles.count=COUNTER;
    DATA_TRANSF_PEN(COUNTER,1:3)=[data_num(1,2:4) 1]*T*M;
    if (handles.recv2==1) DATA_TRANSF_REC2(COUNTER,1:3)=[data_num(2,2:4) 1]*T*M; end
    if (handles.recv3==1) DATA_TRANSF_REC3(COUNTER,1:3)=[data_num(3,2:4) 1]*T*M; end
    if (handles.recv4==1) DATA_TRANSF_REC4(COUNTER,1:3)=[data_num(4,2:4) 1]*T*M; end
    LOC=strvcat(LOC,num2str(COUNTER));
    handles.data_pen=DATA_TRANSF_PEN;
    if (handles.recv2==1) handles.data_rec2=DATA_TRANSF_REC2; end
    if (handles.recv3==1) handles.data_rec3=DATA_TRANSF_REC3; end
    if (handles.recv4==1) handles.data_rec4=DATA_TRANSF_REC4; end
    handles.location=LOC;
    Plotdata_x=handles.data_pen(handles.count,1);
    Plotdata_y=handles.data_pen(handles.count,2);
    Plotdata_z=handles.data_pen(handles.count,3);
    if ~isequal(handles.count,1) 
         plot3(Plotdata_x,Plotdata_y,Plotdata_z,'p','Parent',handles.coordinates);
         set(handles.coordinates,'BOX','on','XGrid','on','Ygrid','on','ZGrid','on');
         hold(handles.coordinates,'on');
         xlabel(handles.coordinates,'X','Fontweight','bold');
         ylabel(handles.coordinates,'Y','Fontweight','bold');
         zlabel(handles.coordinates,'Z','Fontweight','bold');
    end
    ResultsStr_pen = get(handles.points_pen,'String');
    ResultsStr_rec2 = get(handles.points_rec2,'String');
    ResultsStr_rec3 = get(handles.points_rec3,'String');
    ResultsStr_rec4 = get(handles.points_rec4,'String');
    ResultsStr_pen = [ResultsStr_pen;{[handles.location(handles.count,:),'   :    ',...
                num2str(handles.data_pen(handles.count,1)),'   ',num2str(handles.data_pen(handles.count,2)),'   ',num2str(handles.data_pen(handles.count,3))]}];
    if (handles.recv2==1)
                ResultsStr_rec2 = [ResultsStr_rec2;{[handles.location(handles.count,:),'   :    ',...
                num2str(handles.data_rec2(handles.count,1)),'   ',num2str(handles.data_rec2(handles.count,2)),'   ',num2str(handles.data_rec2(handles.count,3))]}];   
    end
    if (handles.recv3==1)
                ResultsStr_rec3 = [ResultsStr_rec3;{[handles.location(handles.count,:),'   :    ',...
                num2str(handles.data_rec3(handles.count,1)),'   ',num2str(handles.data_rec3(handles.count,2)),'   ',num2str(handles.data_rec3(handles.count,3))]}]; 
    end
    if (handles.recv4==1)
                ResultsStr_rec4 = [ResultsStr_rec4;{[handles.location(handles.count,:),'   :    ',...
                num2str(handles.data_rec4(handles.count,1)),'   ',num2str(handles.data_rec4(handles.count,2)),'   ',num2str(handles.data_rec4(handles.count,3))]}]; 
    end
    set(handles.points_pen,'String',ResultsStr_pen);
    if (handles.recv2==1) set(handles.points_rec2,'String',ResultsStr_rec2); end
    if (handles.recv3==1) set(handles.points_rec3,'String',ResultsStr_rec3); end
    if (handles.recv4==1) set(handles.points_rec4,'String',ResultsStr_rec4); end
    str_text2=sprintf('\nPress Stylus Button to digitize additional points \nOR\nPress STOP to finish digitization');
    set(handles.text2,'String',str_text2);
    COUNTER=COUNTER+1;
%     handles.data_pen
    hObject=handles.get_additional;
    guidata(hObject,handles);
else 
%     (COUNTER1,1:3)=data_num(1,2:4);
%     DATA_ORIG_REC2(COUNTER1,1:3)=data_num(2,2:4);
%     DATA_ORIG_REC3(COUNTER1,1:3)=data_num(3,2:4);
    DATA_ORIG_PEN(COUNTER1,1:3)=data_num(1,2:4);
    DATA_ORIG_PEN(COUNTER1+1,1:3)=data_num(5,2:4);
    DATA_ORIG_PEN(COUNTER1+2,1:3)=data_num(9,2:4);
    if (handles.recv2==1) 
        DATA_ORIG_REC2(COUNTER1,1:3)=data_num(2,2:4);
        DATA_ORIG_REC2(COUNTER1+1,1:3)=data_num(6,2:4);
        DATA_ORIG_REC2(COUNTER1+2,1:3)=data_num(10,2:4);
    end
    if (handles.recv3==1) 
        DATA_ORIG_REC3(COUNTER1,1:3)=data_num(3,2:4);
        DATA_ORIG_REC3(COUNTER1+1,1:3)=data_num(7,2:4);
        DATA_ORIG_REC3(COUNTER1+2,1:3)=data_num(11,2:4);
    end
    if (handles.recv4==1) 
        DATA_ORIG_REC4(COUNTER1,1:3)=data_num(4,2:4);
        DATA_ORIG_REC4(COUNTER1+1,1:3)=data_num(8,2:4);
        DATA_ORIG_REC4(COUNTER1+2,1:3)=data_num(12,2:4);
    end
    if COUNTER<=length(handles.needed_loc) 
          if (distance(DATA_ORIG_PEN(COUNTER1,:),DATA_ORIG_PEN((COUNTER1+1),:))>0.2)|(distance(DATA_ORIG_PEN(COUNTER1,:),DATA_ORIG_PEN((COUNTER1+2),:))>0.2)|(distance(DATA_ORIG_PEN((COUNTER1+1),:),DATA_ORIG_PEN((COUNTER1+2),:))>0.2)
            redigitize=1; % For redigitization
            COUNTER1=COUNTER1-3;
            if COUNTER<5 % For test and cardinal points
                errorstring=sprintf('Distance between succesive digitization greater than 2 mm\nRedigitize point %s\nPress OK to continue',handles.needed_loc(COUNTER,:));
                uiwait(msgbox(errorstring,'Error','error','modal'));
                switch COUNTER 
                 case 1
                     set(handles.get_test,'Enable','on');
                     set(handles.get_la,'Enable','off');
                     str_text2=sprintf('\n\nGet Test Point');
                     set(handles.text2,'String',str_text2);
                 case 2
                     set(handles.get_la,'Enable','on');
                     set(handles.get_ra,'Enable','off');
                     str_text2=sprintf('\n\nGet Cardinal Point LA');
                     set(handles.text2,'String',str_text2);
                 case 3
                     set(handles.get_ra,'Enable','on');
                     set(handles.get_nasion,'Enable','off');
                     str_text2=sprintf('\n\nGet Cardinal Point RA');
                     set(handles.text2,'String',str_text2);
                 case 4
                     set(handles.get_nasion,'Enable','on');
                     set(handles.get_montage,'Enable','off');
                     str_text2=sprintf('\n\nGet Cardinal Point NASION');
                     set(handles.text2,'String',str_text2);
                end   
                DATA_PEN;
            end
        else 
             redigitize=0; % For all EEG Montage points
             DATA_PEN(COUNTER,:)=(DATA_ORIG_PEN(COUNTER1,:)+DATA_ORIG_PEN((COUNTER1+1),:)+DATA_ORIG_PEN((COUNTER1+2),:))/3;
             if (handles.recv2==1) 
                DATA_REC2(COUNTER,:)=(DATA_ORIG_REC2(COUNTER1,:)+DATA_ORIG_REC2((COUNTER1+1),:)+DATA_ORIG_REC2((COUNTER1+2),:))/3;
             end
             if (handles.recv3==1) 
                DATA_REC3(COUNTER,:)=(DATA_ORIG_REC3(COUNTER1,:)+DATA_ORIG_REC3((COUNTER1+1),:)+DATA_ORIG_REC3((COUNTER1+2),:))/3;
             end
             if (handles.recv4==1) 
                DATA_REC4(COUNTER,:)=(DATA_ORIG_REC4(COUNTER1,:)+DATA_ORIG_REC4((COUNTER1+1),:)+DATA_ORIG_REC4((COUNTER1+2),:))/3;
             end
             switch COUNTER
                   case 2
                   str_la=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_PEN(2,1),DATA_PEN(2,2),DATA_PEN(2,3)); 
                  set(handles.text_la,'String',str_la);
                    case 3
                    str_ra=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_PEN(3,1),DATA_PEN(3,2),DATA_PEN(3,3));
                    set(handles.text_ra,'String',str_ra); 
                    case 4
                    str_nasion=sprintf('X = %2.3g cm\nY = %2.3g cm\nZ = %2.3g cm',DATA_PEN(4,1),DATA_PEN(4,2),DATA_PEN(4,3));
                    set(handles.text_nasion,'String',str_nasion);
             end
              handles.count=COUNTER;
                if isequal(handles.count,1)
                  if special_case==1
                    LOC=strvcat(LOC,handles.needed_loc(strmatch_index,:));
                    special_case=0;
                  else
                  LOC=strvcat(LOC,handles.needed_loc(COUNTER,:));
                  end
                elseif handles.count<size(handles.needed_loc,1)
                if special_case==1
                LOC=strvcat(LOC,handles.needed_loc(strmatch_index,:));
                special_case=0;
                else 
                LOC=strvcat(LOC,handles.needed_loc(COUNTER,:));
                end
                elseif handles.count==size(handles.needed_loc,1)
                if special_case==1
                LOC=strvcat(LOC,handles.needed_loc(strmatch_index,:));
                special_case=0;
                else 
                LOC=strvcat(LOC,handles.needed_loc(COUNTER,:)); 
                end
                elseif special_case==3
                LOC=strvcat(LOC,num2str(COUNTER+no_add));
                 else
                 LOC=strvcat(LOC,num2str(COUNTER));
                end


                handles.location=LOC;
                handles.data_pen=DATA_PEN;
                if (handles.recv2==1) 
                    handles.data_rec2=DATA_REC2;
                end
                if (handles.recv3==1) 
                handles.data_rec3=DATA_REC3;
                end
                if (handles.recv4==1)
                    handles.data_rec4=DATA_REC4;
                end
                if COUNTER==length(handles.needed_loc)
                    str_text2=sprintf('\nGet Additional Points if needed');
                    set(handles.text2,'String',str_text2);
                end
  
                if handles.count>4
                DATA_TRANSF_PEN(COUNTER,1:3)=[data_num(1,2:4) 1]*T*M;
                if (handles.recv2==1)
                    DATA_TRANSF_REC2(COUNTER,1:3)=[data_num(2,2:4) 1]*T*M;
                end
                if (handles.recv3==1)
                    DATA_TRANSF_REC3(COUNTER,1:3)=[data_num(3,2:4) 1]*T*M;
                end
                if (handles.recv4==1)
                    DATA_TRANSF_REC4(COUNTER,1:3)=[data_num(4,2:4) 1]*T*M;
                end
                handles.data_pen=DATA_TRANSF_PEN;
                if (handles.recv2==1) handles.data_rec2=DATA_TRANSF_REC2; end
                if (handles.recv3==1) handles.data_rec3=DATA_TRANSF_REC3; end
                if (handles.recv4==1) handles.data_rec4=DATA_TRANSF_REC4; end
                end


                Plotdata_x=handles.data_pen(handles.count,1);
                Plotdata_y=handles.data_pen(handles.count,2);
                Plotdata_z=handles.data_pen(handles.count,3);
                if ~isequal(handles.count,1)
                    plot3(Plotdata_x,Plotdata_y,Plotdata_z,'p','Parent',handles.coordinates);
                    set(handles.coordinates,'BOX','on','XGrid','on','Ygrid','on','ZGrid','on');
                    hold(handles.coordinates,'on');
                    xlabel(handles.coordinates,'X','Fontweight','bold');
                    ylabel(handles.coordinates,'Y','Fontweight','bold');
                    zlabel(handles.coordinates,'Z','Fontweight','bold');
                end


                COUNTER=COUNTER+1;
                ResultsStr_pen = get(handles.points_pen,'String');
                ResultsStr_rec2 = get(handles.points_rec2,'String');
                ResultsStr_rec3 = get(handles.points_rec3,'String');
                ResultsStr_rec4 = get(handles.points_rec4,'String');
                if isequal(handles.count,1)
                    ResultsStr_pen = {[handles.location(handles.count,:),'   :   ',num2str(handles.data_pen(1,1)),'   ',num2str(handles.data_pen(1,2)),'   ',num2str(handles.data_pen(1,3))]};
                    if (handles.recv2==1) 
                        ResultsStr_rec2 = {[handles.location(handles.count,:),'   :   ',num2str(handles.data_rec2(1,1)),'   ',num2str(handles.data_rec2(1,2)),'   ',num2str(handles.data_rec2(1,3))]};
                    end
                    if (handles.recv3==1) 
                        ResultsStr_rec3 = {[handles.location(handles.count,:),'   :   ',num2str(handles.data_rec3(1,1)),'   ',num2str(handles.data_rec3(1,2)),'   ',num2str(handles.data_rec3(1,3))]};
                    end
                    if (handles.recv4==1)
                        ResultsStr_rec4 = {[handles.location(handles.count,:),'   :   ',num2str(handles.data_rec4(1,1)),'   ',num2str(handles.data_rec4(1,2)),'   ',num2str(handles.data_rec4(1,3))]};
                    end
                else
                    ResultsStr_pen = [ResultsStr_pen;{[handles.location(handles.count,:),'   :    ',...
                    num2str(handles.data_pen(handles.count,1)),'   ',num2str(handles.data_pen(handles.count,2)),'   ',num2str(handles.data_pen(handles.count,3))]}];
                    if (handles.recv2==1)
                        ResultsStr_rec2 = [ResultsStr_rec2;{[handles.location(handles.count,:),'   :    ',...
                        num2str(handles.data_rec2(handles.count,1)),'   ',num2str(handles.data_rec2(handles.count,2)),'   ',num2str(handles.data_rec2(handles.count,3))]}];
                    end
                    if (handles.recv3==1)
                        ResultsStr_rec3 = [ResultsStr_rec3;{[handles.location(handles.count,:),'   :    ',...
                        num2str(handles.data_rec3(handles.count,1)),'   ',num2str(handles.data_rec3(handles.count,2)),'   ',num2str(handles.data_rec3(handles.count,3))]}];
                    end
                    if (handles.recv4==1)
                        ResultsStr_rec4 = [ResultsStr_rec4;{[handles.location(handles.count,:),'   :    ',...
                        num2str(handles.data_rec4(handles.count,1)),'   ',num2str(handles.data_rec4(handles.count,2)),'   ',num2str(handles.data_rec4(handles.count,3))]}];
                    end
                end   
                set(handles.points_pen,'String',ResultsStr_pen);
                if (handles.recv2==1) set(handles.points_rec2,'String',ResultsStr_rec2); end
                if (handles.recv3==1) set(handles.points_rec3,'String',ResultsStr_rec3); end
                if (handles.recv4==1) set(handles.points_rec4,'String',ResultsStr_rec4); end
          end
    end


    COUNTER1=COUNTER1+3;
    handles.count=COUNTER;
    handles.location=LOC;
    hObject=handles.get_montage;
    guidata(hObject,handles);
    %LOC
end

%--------------------------------------------------------------------------
% Function to Calculate euclidean distance between digitized points_pen
function d=distance(X,Y)
d=sqrt(sum((X-Y).^2));

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

% Hint: place code in OpeningFcn to populate logo

%--------------------------------------------------------------------------


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

% Hints: contents = get(hObject,'String') returns points_rec2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from points_rec2


% --- Executes during object creation, after setting all properties.
function points_rec2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to points_rec2 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


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

% Hints: contents = get(hObject,'String') returns points_rec3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from points_rec3


% --- Executes during object creation, after setting all properties.
function points_rec3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to points_rec3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


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

% Hints: contents = get(hObject,'String') returns points_rec4 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from points_rec4


% --- Executes during object creation, after setting all properties.
function points_rec4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to points_rec4 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% % --- Executes on slider movement.
% function slider1_Callback(hObject, eventdata, handles)
% % hObject    handle to slider1 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider1_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider1 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider3_Callback(hObject, eventdata, handles)
% % hObject    handle to slider3 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider3_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider3 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider4_Callback(hObject, eventdata, handles)
% % hObject    handle to slider4 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider4_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider4 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider5_Callback(hObject, eventdata, handles)
% % hObject    handle to slider5 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider5_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider5 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider7_Callback(hObject, eventdata, handles)
% % hObject    handle to slider7 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider7_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider7 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider8_Callback(hObject, eventdata, handles)
% % hObject    handle to slider8 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider8_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider8 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end
% 
% 
% % --- Executes on slider movement.
% function slider9_Callback(hObject, eventdata, handles)
% % hObject    handle to slider9 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: get(hObject,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% 
% 
% % --- Executes during object creation, after setting all properties.
% function slider9_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to slider9 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: slider controls usually have a light gray background, change
% %       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
% usewhitebg = 1;
% if usewhitebg
%     set(hObject,'BackgroundColor',[.9 .9 .9]);
% else
%     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
% end


Contact us at files@mathworks.com