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

% Last Modified by GUIDE v2.5 21-Jan-2004 09:30:17

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

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

handles.baseFigure=varargin{1};
handles.isRatio=1;
% Update handles structure
guidata(hObject, handles);

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

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

% Вычисляем коэффициент Херста матрицы, выделенной в ListBox
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};

if handles.isRatio==1
    [RESX,RESY]=Hurst2(SRC,str2num(get(handles.edit1,'String')), ...
                          str2num(get(handles.edit2,'String')));
else
    [RESX,RESY]=Hurst2(SRC,str2num(get(handles.edit1,'String')));
end

SERIESINDEX=SERIESINDEX+2;
SERIES{SERIESINDEX-1}=RESX;
SERIES{SERIESINDEX}  =RESY;
SERIESTYPE{SERIESINDEX-1}=[char(SERIESTYPE{index}) ';HSTX'];
SERIESTYPE{SERIESINDEX}  =[char(SERIESTYPE{index}) ';HSTY'];
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,:)='Херст  X';
CD(CDm+2,:)='Херст  Y';
set(handles2.listbox1,'String',CD);

if get(handles.checkbox2,'Value')==get(hObject,'Max')
    if get(handles.checkbox3,'Value')==get(hObject,'Max')
        loglog(RESX,RESY,'.');
    else
        semilogx(RESX,RESY,'.');
    end
else
    if get(handles.checkbox3,'Value')==get(hObject,'Max')
        semilogy(RESX,RESY,'.');
    else
        plot(RESX,RESY,'.');
    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
if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    handles.isRatio=1;
else
    handles.isRatio=0;
end
guidata(hObject,handles);

%========================================================================================================================
% --- 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 get(hObject,'Value')==get(hObject,'Max')
    set(handles.axes1,'XScale','log');
else
    set(handles.axes1,'XScale','linear');
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 get(hObject,'Value')==get(hObject,'Max')
    set(handles.axes1,'YScale','log');
else
    set(handles.axes1,'YScale','linear');
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.text7,'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
