function varargout = localDFAFigure(varargin)
% LOCALDFAFIGURE M-file for localDFAFigure.fig
%      LOCALDFAFIGURE, by itself, creates a new LOCALDFAFIGURE or raises the existing
%      singleton*.
%
%      H = LOCALDFAFIGURE returns the handle to a new LOCALDFAFIGURE or the handle to
%      the existing singleton*.
%
%      LOCALDFAFIGURE('Property','Value',...) creates a new LOCALDFAFIGURE using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to localDFAFigure_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      LOCALDFAFIGURE('CALLBACK') and LOCALDFAFIGURE('CALLBACK',hObject,...) call the
%      local function named CALLBACK in LOCALDFAFIGURE.M with the given input
%      arguments.
%
%      *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 localDFAFigure

% Last Modified by GUIDE v2.5 11-Sep-2009 09:18:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @localDFAFigure_OpeningFcn, ...
                   'gui_OutputFcn',  @localDFAFigure_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin & isstr(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 localDFAFigure is made visible.
function localDFAFigure_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

handles.baseFigure=varargin{1};

% Update handles structure
guidata(hObject, handles);

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

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

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

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

global SERIES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
% Вычисляем по методу DFA
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};
if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    SRC=abs(SRC);
end

[RESX,RESY]=localDFAProgress(SRC,str2num(get(handles.edit1,'String')),str2num(get(handles.edit2,'String')));

SERIESINDEX=SERIESINDEX+2;
SERIES{SERIESINDEX-1}=RESX;
SERIES{SERIESINDEX}  =RESY;
SERIESTYPE{SERIESINDEX-1}=[char(SERIESTYPE{index}) ';LDFAX'];
SERIESTYPE{SERIESINDEX}  =[char(SERIESTYPE{index}) ';LDFAY'];
SERIESSTAT{SERIESINDEX-1}=getStat(RESX);
SERIESSTAT{SERIESINDEX-1}.fileName=SERIESSTAT{index}.fileName;
SERIESSTAT{SERIESINDEX}  =getStat(RESY);
CD=get(handles2.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='LocDFA X';
CD(CDm+2,:)='LocDFA Y';
set(handles2.listbox1,'String',CD);

if get(handles.checkboxLine,'Value')==get(handles.checkboxLine,'Value')
    type='.-';
else
    type='.';
end

if get(handles.checkbox2,'Value')==get(hObject,'Max')
    if get(handles.checkbox3,'Value')==get(hObject,'Max')
        loglog(RESX,RESY,type);
    else
        semilogx(RESX,RESY,type);
    end
else
    if get(handles.checkbox3,'Value')==get(hObject,'Max')
        semilogy(RESX,RESY,type);
    else
        plot(RESX,RESY,type);
    end
end

%========================================================================================================================
% --- 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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%========================================================================================================================
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 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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
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 on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1

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

% Hint: get(hObject,'Value') returns toggle state of checkbox2
if mystrcmp(get(handles.axes1,'XScale'),'log')==0
    set(handles.axes1,'XScale','linear')
else
    set(handles.axes1,'XScale','log')
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox3
if mystrcmp(get(handles.axes1,'YScale'),'log')==0
    set(handles.axes1,'YScale','linear')
else
    set(handles.axes1,'YScale','log')
end

%========================================================================================================================
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
LINES=get(handles.axes1,'Children');
[LINESm,LINESn]=size(LINES);
if LINESm>1
    for i=1:LINESm-1
        delete(LINES(i,1))
    end
end
X=get(LINES(LINESm,1),'XData');
Y=get(LINES(LINESm,1),'YData');
if get(handles.checkbox2,'Value')==get(handles.checkbox2,'Max')
    XNew=log(X);
else
    XNew=X;
end
if get(handles.checkbox3,'Value')==get(handles.checkbox3,'Max')
    YNew=log(Y);
else
    YNew=Y;
end

TEMP=get(handles.pushbutton2,'String');
set(handles.pushbutton2,'String','Выберите на графике 2 точки.');
[mouseX,mouseY]=ginput(2);
set(handles.pushbutton2,'String',TEMP);

[Xm,Xn]=size(X);
Min=abs(X(1,1)-mouseX(1,1));
indexMin1=1;
for i=2:Xn
    if abs(X(1,i)-mouseX(1,1))<Min
        Min=abs(X(1,i)-mouseX(1,1));
        indexMin1=i;
    end
end

Min=abs(X(1,1)-mouseX(2,1));
indexMin2=1;
for i=2:Xn
    if abs(X(1,i)-mouseX(2,1))<Min
        Min=abs(X(1,i)-mouseX(2,1));
        indexMin2=i;
    end
end

hold on
if indexMin1>indexMin2
    TEMP=indexMin1;
    indexMin1=indexMin2;
    indexMin2=TEMP;
end
[a,b]=MinSquare(XNew(1,indexMin1:indexMin2),YNew(1,indexMin1:indexMin2));
set(handles.text6,'String',num2str(a));
set(handles.text8,'String',num2str(b));
% Осталось из старой версии
%plot([X(1,indexMin1) X(1,indexMin2)],[Y(1,indexMin1) Y(1,indexMin2)],'r.-');

XL=get(handles.axes1,'XLim');
YL=get(handles.axes1,'YLim');

YNew2=a*XNew+b;
if get(handles.checkbox3,'Value')==get(handles.checkbox3,'Max')
    YNew2=exp(YNew2);
end
plot(X,YNew2,'r-');
set(handles.axes1,'XLim',XL);
set(handles.axes1,'YLim',YL);
hold off

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

% Hint: get(hObject,'Value') returns toggle state of checkbox5
if get(hObject,'Value')==get(hObject,'Max')
    grid on;
else
    grid off;
end


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

% Hint: get(hObject,'Value') returns toggle state of checkboxLine
