function varargout = get_length(varargin)
% GET_LENGTH MATLAB code for get_length.fig
%      GET_LENGTH, by itself, creates a new GET_LENGTH or raises the existing
%      singleton*.
%
%      H = GET_LENGTH returns the handle to a new GET_LENGTH or the handle to
%      the existing singleton*.
%
%      GET_LENGTH('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GET_LENGTH.M with the given input arguments.
%
%      GET_LENGTH('Property','Value',...) creates a new GET_LENGTH or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before get_length_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to get_length_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 get_length

% Last Modified by GUIDE v2.5 26-Sep-2018 15:27:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @get_length_OpeningFcn, ...
                   'gui_OutputFcn',  @get_length_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 get_length is made visible.
function get_length_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 get_length (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);


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

% --- Outputs from this function are returned to the command line.
function varargout = get_length_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;

global image file_path frame_number
frame_number=1;
frameName = strcat(file_path,num2str(frame_number),'.jpg');
%frameName = 'device 1 flow 20k 014.jpg';
image=imread(frameName);
imshow(image);

% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (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 axes1


% --- Executes on button press in set_threshold.
function set_threshold_Callback(hObject, eventdata, handles)
% hObject    handle to set_threshold (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global threshold
threshold = get(handles.edit1,'string');


% --- Executes on button press in start_point.
function start_point_Callback(hObject, eventdata, handles)
% hObject    handle to start_point (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global start_point_x start_point_y
p=get(gca,'CurrentPoint');
a=p(1,1);b=p(1,2);
start_point_x=round(a);
start_point_y=round(b);
c=sprintf('(%.f,%.f)',start_point_x,start_point_y);
set(handles.edit2,'string',c);


% --- Executes on button press in left_end.
function left_end_Callback(hObject, eventdata, handles)
% hObject    handle to left_end (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image start_point_x start_point_y threshold left_end_x left_end_y
left_end_x = start_point_x;
left_end_y = start_point_y;
for i=(start_point_x-15):start_point_x
    for j=(start_point_y-15):(start_point_y+15)
        newgreen=image(j,i,2);
        if i<left_end_x && newgreen>str2num(threshold) 
            left_end_x=i;
            left_end_y=j;
        end
    end
end
c=sprintf('(%.f,%.f)',left_end_x,left_end_y);
set(handles.edit3,'string',c);

% --- Executes on button press in right_end.
function right_end_Callback(hObject, eventdata, handles)
% hObject    handle to right_end (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image start_point_x start_point_y threshold right_end_x right_end_y
right_end_x = start_point_x;
right_end_y = start_point_y;
for i=start_point_x:(start_point_x+35)
    for j=(start_point_y-15):(start_point_y+15)
        newgreen=image(j,i,2);
        if i>right_end_x && newgreen>str2num(threshold) 
            right_end_x=i;
            right_end_y=j;
        end
    end
end
c=sprintf('(%.f,%.f)',right_end_x,right_end_y);
set(handles.edit4,'string',c);

% --- Executes on button press in string_length.
function string_length_Callback(hObject, eventdata, handles)
% hObject    handle to string_length (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global left_end_x right_end_x string_length right_end_y left_end_y
string_length = (right_end_x - left_end_x );
%string_length = ((right_end_x - left_end_x )^2+(right_end_y - left_end_y)^2)^(1/2);
set(handles.edit5,'string',string_length);


% --- Executes on button press in scale_bar_start_point.
function scale_bar_start_point_Callback(hObject, eventdata, handles)
% hObject    handle to scale_bar_start_point (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image threshold left_endx right_endx 

p=get(gca,'CurrentPoint');
a=p(1,1);b=p(1,2);
start_pointx=round(a);
start_pointy=round(b);
c=sprintf('(%.f,%.f)',start_pointx,start_pointy);
set(handles.edit7,'string',c);

right_endx = start_pointx;
for i=start_pointx:(start_pointx+50)
        newgreen=image(start_pointy,i,2);
        if i>right_endx && newgreen>str2num(threshold) 
            right_endx=i;
        end
end

left_endx = start_pointx;
for i=(start_pointx-50):start_pointx    
        newgreen=image(start_pointy,i,2);
        if i<left_endx && newgreen>str2num(threshold) 
            left_endx=i;           
        end  
end


% --- Executes on button press in pixel_size.
function pixel_size_Callback(hObject, eventdata, handles)
% hObject    handle to pixel_size (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global left_endx right_endx 
pixel_size = 5/(right_endx - left_endx);
set(handles.edit6,'string',pixel_size);

% --- Executes on button press in save.
%function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global ExpDate string_length device_number flow_rate molecule_number
%basic_info={'device#',num2str(device_number);'flow rate', num2str(flow_rate); 'molecule#', num2str(molecule_number)};
%xlswrite(strcat(num2str(ExpDate),'.xls'),basic_info,'A1:B3');
%frameinfo={'frame#','string length in pixel'};
%xlswrite(strcat(num2str(ExpDate),'.xls'),frameinfo,'sheet1','A5:B5');
%xlswrite(strcat(num2str(ExpDate),'.xls'),'1','sheet1','A6');
%xlswrite(strcat(num2str(ExpDate),'.xls'),string_length,'sheet1','B6');

% --- Executes on button press in last.
function last_Callback(hObject, eventdata, handles)
% hObject    handle to last (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image frame_number file_path 
frame_number=frame_number - 1;
frameName = strcat(file_path,num2str(frame_number),'.jpg');
image=imread(frameName);
imshow(image);

% --- Executes on button press in next.
function next_Callback(hObject, eventdata, handles)
% hObject    handle to next (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global image frame_number file_path
frame_number=frame_number + 1;
frameName = strcat(file_path,num2str(frame_number),'.jpg');
image=imread(frameName);
imshow(image);

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)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- 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)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- 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)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- 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)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- 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



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)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- 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



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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 close.
function close_Callback(hObject, eventdata, handles)
% hObject    handle to close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcf);
rmdir ('images','s');
